-
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.
Support typed events in hook send() (#235)
* Support typed events in hook send() * Add changeset
- Loading branch information
Showing
6 changed files
with
99 additions
and
9 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,16 @@ | ||
--- | ||
"robot-hooks": minor | ||
"haunted-robot": minor | ||
"preact-robot": minor | ||
"react-robot": minor | ||
--- | ||
|
||
Typed event for send() in hooks | ||
|
||
This adds the same typed event support for `send()` from hooks, for ex in React: | ||
|
||
```ts | ||
const [state, send] = useMachine(machine); | ||
|
||
send('this-is-typed'); | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { expectTypeOf } from 'expect-type'; | ||
import { test } from 'node:test'; | ||
import { | ||
createMachine, | ||
transition, | ||
state, | ||
} from 'robot3'; | ||
import { | ||
createUseMachine | ||
} from 'robot-hooks'; | ||
|
||
test('send(event) is typed', () => { | ||
const useMachine = createUseMachine(null, null); | ||
const machine = createMachine({ | ||
one: state(transition('go-two', 'two')), | ||
two: state(transition('go-one', 'one')), | ||
three: state() | ||
}); | ||
|
||
const [_state, send] = useMachine(machine); | ||
|
||
type Params = Parameters<typeof 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'>(); | ||
}); |
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,33 @@ | ||
{ | ||
"include": ["../..", "."], | ||
"exclude": ["../../node_modules"], | ||
"compilerOptions": { | ||
/* Base Options: */ | ||
"esModuleInterop": true, | ||
"skipLibCheck": false, | ||
"target": "es2022", | ||
"allowJs": false, | ||
"moduleDetection": "force", | ||
"isolatedModules": true, | ||
"moduleResolution": "nodenext", | ||
|
||
/* Strictness */ | ||
"strict": true, | ||
"noUncheckedIndexedAccess": true, | ||
"noImplicitOverride": true, | ||
|
||
/* AND if you're building for a library: */ | ||
"declaration": true, | ||
|
||
/* AND if you're building for a library in a monorepo: */ | ||
"composite": true, | ||
"declarationMap": true, | ||
|
||
/* If NOT transpiling with TypeScript: */ | ||
"module": "nodenext", | ||
"noEmit": true, | ||
|
||
/* If your code runs in the DOM: */ | ||
"lib": ["es2022", "dom", "dom.iterable"] | ||
} | ||
} |