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

Fix: update types for invoked service target to allow string #3745

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .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)
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Expand Up @@ -514,13 +514,13 @@ export interface InvokeConfig<TContext, TEvent extends EventObject> {
*/
onDone?:
| string
| SingleOrArray<TransitionConfig<TContext, DoneInvokeEvent<any>>>;
| SingleOrArray<string | TransitionConfig<TContext, DoneInvokeEvent<any>>>;
viglucci marked this conversation as resolved.
Show resolved Hide resolved
/**
* The transition to take upon the invoked child machine sending an error event.
*/
onError?:
| string
| SingleOrArray<TransitionConfig<TContext, DoneInvokeEvent<any>>>;
| SingleOrArray<string | TransitionConfig<TContext, DoneInvokeEvent<any>>>;
/**
* Meta data related to this invocation
*/
Expand Down
47 changes: 47 additions & 0 deletions packages/core/test/types.test.ts
Expand Up @@ -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();
});
});