Skip to content

Instantly share code, notes, and snippets.

@matthewp
Created June 18, 2021 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewp/e121f17ddcee1dcf5cd3d97da35e8878 to your computer and use it in GitHub Desktop.
Save matthewp/e121f17ddcee1dcf5cd3d97da35e8878 to your computer and use it in GitHub Desktop.
lucy typescript notes
import { EventObject, Guard, InvokeCreator, StateMachine } from 'xstate'
type EventNames = 'GO_BACK' | 'GO_FORWARD' | 'GO_SIDEWAYS';
type KnownContextKeys = 'user';
type LucyKnownContext<Keys extends string> = Record<Keys, any>
function createMachine<TContext extends LucyKnownContext<KnownContextKeys>, TEvent extends { type: EventNames } = any>() {
return {} as any
}
createMachine<{
otherThing: {},
user: {}
}>()
type MyEventShape = {
type: 'GO_BACK'
} | {
type: 'GO_FORWARD'
y: number
} | {
type: 'GO_SIDEWAYS';
x: number
} | {
type: 'done.invoke.fetchUsers';
data: {
user: {
name: string;
}
}
}
interface CreateMachineOptions<TContext, TEvent extends EventObject> {
guards: {
checkAfterGoBack: Guard<
TContext,
TEvent extends Extract<TEvent, { type: 'GO_BACK' }> ? Extract<TEvent, { type: 'GO_BACK' }> : TEvent
>;
}
services: {
fetchUsers: InvokeCreator<
TContext,
Extract<TEvent,
| { type: 'SUBMIT' }
>
,
Extract<
TEvent,
{ type: 'done.invoke.fetchUsers' }> extends { 'data': infer T } ? T : any
> | StateMachine<any, any, any>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment