-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autocomplete for service.send() (#232)
* Add autocomplete for service.send() This makes it so you get proper auto-complete when using `service.send('event')`. * small cleanup * Add changeset * Add tests
- Loading branch information
Showing
7 changed files
with
166 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"robot3": minor | ||
--- | ||
|
||
Autocomplete for service.send() | ||
|
||
This makes it so that the event name in `service.send(event)` is inferred from the transitions used to create the machine. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expectTypeOf } from 'expect-type'; | ||
import { test } from 'node:test'; | ||
import { | ||
type Service, | ||
createMachine, | ||
transition, | ||
state, | ||
} from 'robot3'; | ||
|
||
test('send(event) is typed', () => { | ||
const machine = createMachine({ | ||
one: state(transition('go-two', 'two')), | ||
two: state(transition('go-one', 'one')), | ||
three: state() | ||
}); | ||
|
||
type Params = Parameters<Service<typeof machine>['send']>; | ||
type EventParam = Params[0]; | ||
type StringParams = Extract<EventParam, string>; | ||
expectTypeOf<StringParams>().toEqualTypeOf<'go-one' | 'go-two'>(); | ||
|
||
type ObjectParams = Extract<EventParam, { type: string; }>; | ||
expectTypeOf<ObjectParams['type']>().toEqualTypeOf<'go-one' | 'go-two'>(); | ||
}); |
Oops, something went wrong.