Skip to content

Commit 11f39ae

Browse files
committed
Support typed events in hook send()
1 parent 99037e3 commit 11f39ae

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

Diff for: package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/robot-hooks/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
declare module 'robot-hooks' {
2-
import type {Machine, SendFunction, Service} from 'robot3';
2+
import type {Machine, SendFunction, Service, GetMachineTransitions} from 'robot3';
33
function useMachine<M extends Machine>(
44
machine: M,
55
initialContext?: M['context']
66
): [
77
M['state'] & {context: M['context']},
8-
SendFunction,
8+
SendFunction<GetMachineTransitions<M>>,
99
Service<typeof machine>
1010
];
1111

Diff for: packages/robot-hooks/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
],
1212
"scripts": {
1313
"test": "wireit",
14+
"test:browser": "wireit",
15+
"test:types": "wireit",
1416
"build": "wireit"
1517
},
1618
"repository": {
@@ -33,6 +35,16 @@
3335
},
3436
"wireit": {
3537
"test": {
38+
"dependencies": [
39+
"test:types",
40+
"test:browser"
41+
]
42+
},
43+
"test:types": {
44+
"command": "tsc -p test/types/tsconfig.json",
45+
"files": []
46+
},
47+
"test:browser": {
3648
"command": "node-qunit-puppeteer http://localhost:1965/packages/robot-hooks/test/test.html 10000",
3749
"dependencies": [
3850
"../..:server"

Diff for: packages/robot-hooks/test/types/send.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expectTypeOf } from 'expect-type';
2+
import { test } from 'node:test';
3+
import {
4+
createMachine,
5+
transition,
6+
state,
7+
} from 'robot3';
8+
import {
9+
createUseMachine
10+
} from 'robot-hooks';
11+
12+
test('send(event) is typed', () => {
13+
const useMachine = createUseMachine(null, null);
14+
const machine = createMachine({
15+
one: state(transition('go-two', 'two')),
16+
two: state(transition('go-one', 'one')),
17+
three: state()
18+
});
19+
20+
const [_state, send] = useMachine(machine);
21+
22+
type Params = Parameters<typeof send>;
23+
type EventParam = Params[0];
24+
type StringParams = Extract<EventParam, string>;
25+
expectTypeOf<StringParams>().toEqualTypeOf<'go-one' | 'go-two'>();
26+
27+
type ObjectParams = Extract<EventParam, { type: string; }>;
28+
expectTypeOf<ObjectParams['type']>().toEqualTypeOf<'go-one' | 'go-two'>();
29+
});

Diff for: packages/robot-hooks/test/types/tsconfig.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"include": ["../..", "."],
3+
"exclude": ["../../node_modules"],
4+
"compilerOptions": {
5+
/* Base Options: */
6+
"esModuleInterop": true,
7+
"skipLibCheck": false,
8+
"target": "es2022",
9+
"allowJs": false,
10+
"moduleDetection": "force",
11+
"isolatedModules": true,
12+
"moduleResolution": "nodenext",
13+
14+
/* Strictness */
15+
"strict": true,
16+
"noUncheckedIndexedAccess": true,
17+
"noImplicitOverride": true,
18+
19+
/* AND if you're building for a library: */
20+
"declaration": true,
21+
22+
/* AND if you're building for a library in a monorepo: */
23+
"composite": true,
24+
"declarationMap": true,
25+
26+
/* If NOT transpiling with TypeScript: */
27+
"module": "nodenext",
28+
"noEmit": true,
29+
30+
/* If your code runs in the DOM: */
31+
"lib": ["es2022", "dom", "dom.iterable"]
32+
}
33+
}

0 commit comments

Comments
 (0)