-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…5055) * `useActor` required input when defined * `useMachine` required input when defined * required input in svelte and vue * `useActorRef` input required (but optional `options`) * `useActionRef` options type * rename type to `RequiredActorInstanceOptions` * type tests for required/not required input * required input in `xstate-solid` * added changeset * move machines inside tests * rename type and split changeset --------- Co-authored-by: Mateusz Burzyński <[email protected]>
- Loading branch information
1 parent
ec156a3
commit ad38c35
Showing
19 changed files
with
360 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'xstate': patch | ||
--- | ||
|
||
Exported `RequiredActorOptionsKeys` type meant to be used by integration packages like `@xstate/react` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
'@xstate/svelte': patch | ||
'@xstate/react': patch | ||
'@xstate/solid': patch | ||
'@xstate/vue': patch | ||
--- | ||
|
||
Updated types of `useActor`, `useMachine`, and `useActorRef` to require `input` when defined inside `types/input`. | ||
|
||
Previously even when `input` was defined inside `types`, `useActor`, `useMachine`, and `useActorRef` would **not** make the input required: | ||
|
||
```tsx | ||
const machine = setup({ | ||
types: { | ||
input: {} as { value: number } | ||
} | ||
}).createMachine({}); | ||
|
||
function App() { | ||
// Event if `input` is not defined, `useMachine` works at compile time, but risks crashing at runtime | ||
const _ = useMachine(machine); | ||
return <></>; | ||
} | ||
``` | ||
|
||
With this change the above code will show a type error, since `input` is now required: | ||
|
||
```tsx | ||
const machine = setup({ | ||
types: { | ||
input: {} as { value: number } | ||
} | ||
}).createMachine({}); | ||
|
||
function App() { | ||
const _ = useMachine(machine, { | ||
input: { value: 1 } // Now input is required at compile time! | ||
}); | ||
return <></>; | ||
} | ||
``` | ||
|
||
This avoids runtime errors when forgetting to pass `input` when defined inside `types`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
import { Actor, ActorOptions, AnyStateMachine, StateFrom } from 'xstate'; | ||
import { | ||
Actor, | ||
ActorOptions, | ||
AnyStateMachine, | ||
StateFrom, | ||
type ConditionalRequired, | ||
type IsNotNever, | ||
type RequiredActorOptionsKeys | ||
} from 'xstate'; | ||
import { useActor } from './useActor.ts'; | ||
|
||
/** @alias useActor */ | ||
export function useMachine<TMachine extends AnyStateMachine>( | ||
machine: TMachine, | ||
options: ActorOptions<TMachine> = {} | ||
...[options]: ConditionalRequired< | ||
[ | ||
options?: ActorOptions<TMachine> & { | ||
[K in RequiredActorOptionsKeys<TMachine>]: unknown; | ||
} | ||
], | ||
IsNotNever<RequiredActorOptionsKeys<TMachine>> | ||
> | ||
): [StateFrom<TMachine>, Actor<TMachine>['send'], Actor<TMachine>] { | ||
return useActor(machine, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.