Skip to content

How to type transition input in @xstate/store #5017

Answered by with-heart
qiushiyan asked this question in General
Discussion options

You must be logged in to vote

I opened #5020 to implement the type EventFromStore:

export type EventFromStore<TStore extends Store<any, any>> =
  TStore extends Store<any, infer TEvent> ? TEvent : never;

If that's merged, here's how you could use it to implement what you're looking for:

import { createStore, type EventFromStore, type Store } from '@xstate/store';

/**
 * Extract the event where `Type` matches the event's `type` from the given
 * `Store`.
 */
type EventByType<
  TStore extends Store<any, any>,
  Type extends EventFromStore<TStore>['type']
> = Extract<EventFromStore<TStore>, { type: Type }>;

/**
 * Extract a specific store event's "input" type (the event type without the
 * `type` property).
 */
type E…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@qiushiyan
Comment options

Comment options

You must be logged in to vote
1 reply
@qiushiyan
Comment options

Comment options

You must be logged in to vote
1 reply
@qiushiyan
Comment options

Answer selected by qiushiyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants