Skip to content

Commit

Permalink
Fixed an issue with nested state.matches calls causing state to be …
Browse files Browse the repository at this point in the history
…narrowed to `never` when the typegen was involved (#3077)
  • Loading branch information
Andarist committed Feb 22, 2022
1 parent 555b7df commit 2c76eca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-rabbits-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with nested `state.matches` calls when the typegen was involved. The `state` ended up being `never` and thus not usable.
10 changes: 8 additions & 2 deletions packages/core/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,12 @@ export class State<
public matches<
TSV extends TResolvedTypesMeta extends TypegenEnabled
? Prop<TResolvedTypesMeta, 'matchesStates'>
: TTypestate['value']
: never
>(parentStateValue: TSV): boolean;
public matches<
TSV extends TResolvedTypesMeta extends TypegenDisabled
? TTypestate['value']
: never
>(
parentStateValue: TSV
): this is State<
Expand All @@ -332,7 +337,8 @@ export class State<
TStateSchema,
TTypestate,
TResolvedTypesMeta
> & { value: TSV } {
> & { value: TSV };
public matches(parentStateValue: StateValue): any {
return matchesState(parentStateValue as StateValue, this.value);
}

Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/typegenTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,27 @@ describe('typegen types', () => {
}
});

it("shouldn't end up with `never` within a branch after two `state.matches` calls", () => {
interface TypesMeta extends TypegenMeta {
matchesStates: 'a' | 'a.b';
}

const machine = createMachine({
tsTypes: {} as TypesMeta,
schema: {
context: {} as {
foo: string;
}
}
});

const state = machine.initialState;

if (state.matches('a') && state.matches('a.b')) {
((_accept: string) => {})(state.context.foo);
}
});

it('should be possible to pass typegen-less machines to functions expecting a machine argument that do not utilize the typegen information', () => {
const machine = createMachine({});

Expand Down

0 comments on commit 2c76eca

Please sign in to comment.