Type helpers
XState makes several type helpers available to you for composing types in TypeScript. You can use these helpers for creating custom functions or typing various integrations.
StateFrom
​
StateFrom
can be used to extract the State
from a machine.
ts
import {createMachine ,StateFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},});ÂconstmyFunction = (state :StateFrom <typeofmachine >) => {constcontext =state .context ;};
ts
import {createMachine ,StateFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},});ÂconstmyFunction = (state :StateFrom <typeofmachine >) => {constcontext =state .context ;};
ContextFrom
​
ContextFrom
can be used to extract the context
from a machine.
ts
import {createMachine ,ContextFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},});ÂconstmyFunction = (context :ContextFrom <typeofmachine >) => {console .log (context );};
ts
import {createMachine ,ContextFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},});ÂconstmyFunction = (context :ContextFrom <typeofmachine >) => {console .log (context );};
EventFrom
​
EventFrom
can be used to extract the event
types from a machine
ts
import {createMachine ,EventFrom } from 'xstate';Âconstmachine =createMachine ({schema : {events : {} as| {type : 'FOO';}| {type : 'BAR';value : string;},},});ÂconstmyFunction = (event :EventFrom <typeofmachine >) => {console .log (event );};
ts
import {createMachine ,EventFrom } from 'xstate';Âconstmachine =createMachine ({schema : {events : {} as| {type : 'FOO';}| {type : 'BAR';value : string;},},});ÂconstmyFunction = (event :EventFrom <typeofmachine >) => {console .log (event );};
You can also extract specific events by passing the type to the second generic slot.
ts
constmyFunction = (event :EventFrom <typeofmachine , 'BAR'>) => {console .log (event );};
ts
constmyFunction = (event :EventFrom <typeofmachine , 'BAR'>) => {console .log (event );};
InterpreterFrom
​
InterpreterFrom
gives you the type of the actor returned from interpret
or useInterpret
.
ts
import {createMachine ,InterpreterFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},});ÂconstmyFunction = (actor :InterpreterFrom <typeofmachine >) => {constcontext =actor .state .context ;};
ts
import {createMachine ,InterpreterFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},});ÂconstmyFunction = (actor :InterpreterFrom <typeofmachine >) => {constcontext =actor .state .context ;};
ActorRefFrom
​
ActorRefFrom
is especially useful when spawning actors because it types the reference created by spawn
when assigning to context.
ts
import {createMachine ,ActorRefFrom ,spawn ,assign } from 'xstate';ÂconstchildMachine =createMachine ({});Âconstmachine =createMachine ({schema : {context : {} as {spawnedChild :ActorRefFrom <typeofchildMachine >;},},entry : [assign ((context ,event ) => {return {spawnedChild :spawn (childMachine ),};}),],});
ts
import {createMachine ,ActorRefFrom ,spawn ,assign } from 'xstate';ÂconstchildMachine =createMachine ({});Âconstmachine =createMachine ({schema : {context : {} as {spawnedChild :ActorRefFrom <typeofchildMachine >;},},entry : [assign ((context ,event ) => {return {spawnedChild :spawn (childMachine ),};}),],});
MachineOptionsFrom
​
You can use MachineOptionsFrom
in combination with typegen to get a strongly typed version of the machine’s options.
ts
import {createMachine ,MachineOptionsFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},entry : ['sayHello', 'sayHelloAgain'],tsTypes : {} as import('./myMachine.typegen').Typegen0 ,});Âconstoptions :MachineOptionsFrom <typeofmachine > = {actions : {sayHello : (context ) => {},},};
ts
import {createMachine ,MachineOptionsFrom } from 'xstate';Âconstmachine =createMachine ({schema : {context : {} as {count : number;},},entry : ['sayHello', 'sayHelloAgain'],tsTypes : {} as import('./myMachine.typegen').Typegen0 ,});Âconstoptions :MachineOptionsFrom <typeofmachine > = {actions : {sayHello : (context ) => {},},};
You can also pass true
to the second element of the generic to ensure that all missing implementations are passed. In the example below, sayGoodbye
is missing, so must be passed when you pass true
to MachineOptionsFrom
.
ts
import {createMachine ,MachineOptionsFrom } from 'xstate';Âconstmachine =createMachine ({entry : ['sayHello'],exit : ['sayGoodbye'],tsTypes : {} as import('./myMachine.typegen').Typegen0 ,},{actions : {sayHello : () => {console .log ('Hello');},},});Âconstoptions :MachineOptionsFrom <typeofmachine , true> = {actions : {/*** This MUST be passed because of the `true` passed in above*/sayGoodbye : () => {},},};
ts
import {createMachine ,MachineOptionsFrom } from 'xstate';Âconstmachine =createMachine ({entry : ['sayHello'],exit : ['sayGoodbye'],tsTypes : {} as import('./myMachine.typegen').Typegen0 ,},{actions : {sayHello : () => {console .log ('Hello');},},});Âconstoptions :MachineOptionsFrom <typeofmachine , true> = {actions : {/*** This MUST be passed because of the `true` passed in above*/sayGoodbye : () => {},},};