Skip to content

Commit

Permalink
Fixed an issue with typegen types not being able to provide events th…
Browse files Browse the repository at this point in the history
…at had a union of strings as their `type` (#3230)

* Fixed an issue with typegen types not being able to provide events that had a union of strings as their `type`

* Remove `typesVersions` attempt from the core
  • Loading branch information
Andarist committed Apr 19, 2022
1 parent fe5f0e6 commit 780458c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fast-ways-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with typegen types not being able to provide events that had a union of strings as their `type` (such as `{ type: 'INC' | 'DEC'; value: number; }`).
5 changes: 4 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ export type Compute<A extends any> = { [K in keyof A]: A[K] } & unknown;
export type Prop<T, K> = K extends keyof T ? T[K] : never;
export type Values<T> = T[keyof T];
export type Merge<M, N> = Omit<M, keyof N> & N;
// TODO: replace in v5 with:
// export type IndexByType<T extends { type: string }> = { [E in T as E['type']]: E; };
export type IndexByType<T extends { type: string }> = {
[K in T['type']]: Extract<T, { type: K }>;
[K in T['type']]: T extends any ? (K extends T['type'] ? T : never) : never;
};

export type Equals<A1 extends any, A2 extends any> = (<A>() => A extends A2
? true
: false) extends <A>() => A extends A1 ? true : false
Expand Down
30 changes: 30 additions & 0 deletions packages/core/test/typegenTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,34 @@ describe('typegen types', () => {
}
);
});

it('should be able to provide events that use string unions as their type', () => {
interface TypesMeta extends TypegenMeta {
eventsCausingActions: {
increment: 'INC';
decrement: 'DEC';
};
}

createMachine(
{
tsTypes: {} as TypesMeta,
schema: {
context: {} as {
count: number;
},
events: {} as { type: 'INC' | 'DEC'; value: number }
}
},
{
actions: {
increment: assign((ctx, ev) => {
return {
count: ctx.count + ev.value
};
})
}
}
);
});
});

0 comments on commit 780458c

Please sign in to comment.