xstate
Version:
Finite State Machines and Statecharts for the Modern Web.
28 lines (27 loc) • 862 B
TypeScript
import { EventObject } from "./types.js";
/**
* Asserts that the given event object is of the specified type or types. Throws
* an error if the event object is not of the specified types.
*
* @example
*
* ```ts
* // ...
* entry: ({ event }) => {
* assertEvent(event, 'doNothing');
* // event is { type: 'doNothing' }
* },
* // ...
* exit: ({ event }) => {
* assertEvent(event, 'greet');
* // event is { type: 'greet'; message: string }
*
* assertEvent(event, ['greet', 'notify']);
* // event is { type: 'greet'; message: string }
* // or { type: 'notify'; message: string; level: 'info' | 'error' }
* },
* ```
*/
export declare function assertEvent<TEvent extends EventObject, TAssertedType extends TEvent['type']>(event: TEvent, type: TAssertedType | TAssertedType[]): asserts event is TEvent & {
type: TAssertedType;
};