Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TagsFrom helper type #3835

Merged
merged 5 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .changeset/mean-oranges-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'xstate': minor
---

The new `TagsFrom` helper type extracts the type of `tags` from a state machine when typegen is enabled:

```ts
const machine = createMachine({
tags: ['a', 'b'],
states: {
c: { tags: 'c' }
},
// `tags` attached to machine via typegen
tsTypes: {} as import('./machine.typegen').Typegen0
Andarist marked this conversation as resolved.
Show resolved Hide resolved
});

type Tags = TagsFrom<typeof machine>; // 'a' | 'b' | 'c'
```

If typegen is not enabled, `TagsFrom` returns `string`:

```ts
const machine = createMachine({
tags: ['a', 'b'],
states: {
c: { tags: 'c' }
Andarist marked this conversation as resolved.
Show resolved Hide resolved
}
});

type Tags = TagsFrom<typeof machine>; // string
```
28 changes: 17 additions & 11 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ type SimpleActionsOf<T extends BaseActionObject> = ActionObject<
/**
* Events that do not require payload
*/
export type SimpleEventsOf<TEvent extends EventObject> =
ExtractWithSimpleSupport<TEvent>;
export type SimpleEventsOf<
TEvent extends EventObject
> = ExtractWithSimpleSupport<TEvent>;

export type BaseAction<
TContext,
Expand Down Expand Up @@ -1984,18 +1985,23 @@ type Matches<TypegenEnabledArg, TypegenDisabledArg> = {
(stateValue: TypegenDisabledArg): any;
};

export type StateValueFrom<TMachine extends AnyStateMachine> =
StateFrom<TMachine>['matches'] extends Matches<
infer TypegenEnabledArg,
infer TypegenDisabledArg
>
? TMachine['__TResolvedTypesMeta'] extends TypegenEnabled
? TypegenEnabledArg
: TypegenDisabledArg
: never;
export type StateValueFrom<
TMachine extends AnyStateMachine
> = StateFrom<TMachine>['matches'] extends Matches<
infer TypegenEnabledArg,
infer TypegenDisabledArg
>
? TMachine['__TResolvedTypesMeta'] extends TypegenEnabled
? TypegenEnabledArg
: TypegenDisabledArg
: never;

export type PredictableActionArgumentsExec = (
action: ActionObject<unknown, EventObject>,
context: unknown,
_event: SCXML.Event<EventObject>
) => void;

export type TagsFrom<TMachine extends AnyStateMachine> = Parameters<
StateFrom<TMachine>['hasTag']
>[0];
43 changes: 42 additions & 1 deletion packages/core/test/typeHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
EventFrom,
interpret,
MachineOptionsFrom,
StateValueFrom
StateValueFrom,
TagsFrom
} from '../src';
import { createModel } from '../src/model';
import { TypegenMeta } from '../src/typegenTypes';
Expand Down Expand Up @@ -428,3 +429,43 @@ describe('EmittedFrom', () => {
acceptState("isn't any");
});
});

describe('tags', () => {
it('derives tags from StateMachine when typegen is enabled', () => {
const machine = createMachine({
tsTypes: ({} as unknown) as {
'@@xstate/typegen': true;
tags: 'a' | 'b' | 'c';
}
});
Andarist marked this conversation as resolved.
Show resolved Hide resolved

type Tags = TagsFrom<typeof machine>;

const acceptTag = (_tag: Tags) => {};

acceptTag('a');
acceptTag('b');
acceptTag('c');
// @ts-expect-error d is not a valid tag
acceptTag('d');
});

it('derives string from StateMachine without typegen', () => {
const machine = createMachine({
tsTypes: ({} as unknown) as {
'@@xstate/typegen': false;
tags: 'a' | 'b' | 'c';
}
});
Andarist marked this conversation as resolved.
Show resolved Hide resolved

type Tags = TagsFrom<typeof machine>;

const acceptTag = (_tag: Tags) => {};

acceptTag('a');
acceptTag('b');
acceptTag('c');
// d is a valid tag, as is any string
acceptTag('d');
});
});