diff --git a/.changeset/serious-chairs-try.md b/.changeset/serious-chairs-try.md new file mode 100644 index 0000000000..05bd579a84 --- /dev/null +++ b/.changeset/serious-chairs-try.md @@ -0,0 +1,5 @@ +--- +'xstate': patch +--- + +Fix types to allow for string state name in service onDone/onError config (#34) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 42aed4d5ea..979d8f1078 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -514,13 +514,13 @@ export interface InvokeConfig { */ onDone?: | string - | SingleOrArray>>; + | SingleOrArray>>; /** * The transition to take upon the invoked child machine sending an error event. */ onError?: | string - | SingleOrArray>>; + | SingleOrArray>>; /** * Meta data related to this invocation */ diff --git a/packages/core/test/types.test.ts b/packages/core/test/types.test.ts index 4708242bb8..49d3315cac 100644 --- a/packages/core/test/types.test.ts +++ b/packages/core/test/types.test.ts @@ -668,3 +668,50 @@ describe('spawn', () => { }); }); }); + +describe('service-targets', () => { + it('should work with a service that uses strings for both targets', () => { + const machine = createMachine({ + invoke: { + src: () => new Promise((resolve) => resolve(1)), + onDone: ['a', 'b'] + }, + states: { + a: {}, + b: {} + } + }); + noop(machine); + expect(true).toBeTruthy(); + }); + + it('should work with a service that uses TransitionConfigs for both targets', () => { + const machine = createMachine({ + invoke: { + src: () => new Promise((resolve) => resolve(1)), + onDone: [{ target: 'a' }, { target: 'b' }] + }, + states: { + a: {}, + b: {} + } + }); + noop(machine); + expect(true).toBeTruthy(); + }); + + it('should work with a service that uses a string for one target and a TransitionConfig for another', () => { + const machine = createMachine({ + invoke: { + src: () => new Promise((resolve) => resolve(1)), + onDone: [{ target: 'a' }, 'b'] + }, + states: { + a: {}, + b: {} + } + }); + noop(machine); + expect(true).toBeTruthy(); + }); +});