Created
June 18, 2021 12:24
-
-
Save matthewp/e121f17ddcee1dcf5cd3d97da35e8878 to your computer and use it in GitHub Desktop.
lucy typescript notes
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
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