Skip to content

Commit

Permalink
Support typed events in hook send()
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Jan 14, 2025
1 parent 99037e3 commit 11f39ae
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/robot-hooks/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
declare module 'robot-hooks' {
import type {Machine, SendFunction, Service} from 'robot3';
import type {Machine, SendFunction, Service, GetMachineTransitions} from 'robot3';
function useMachine<M extends Machine>(
machine: M,
initialContext?: M['context']
): [
M['state'] & {context: M['context']},
SendFunction,
SendFunction<GetMachineTransitions<M>>,
Service<typeof machine>
];

Expand Down
12 changes: 12 additions & 0 deletions packages/robot-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
],
"scripts": {
"test": "wireit",
"test:browser": "wireit",
"test:types": "wireit",
"build": "wireit"
},
"repository": {
Expand All @@ -33,6 +35,16 @@
},
"wireit": {
"test": {
"dependencies": [
"test:types",
"test:browser"
]
},
"test:types": {
"command": "tsc -p test/types/tsconfig.json",
"files": []
},
"test:browser": {
"command": "node-qunit-puppeteer http://localhost:1965/packages/robot-hooks/test/test.html 10000",
"dependencies": [
"../..:server"
Expand Down
29 changes: 29 additions & 0 deletions packages/robot-hooks/test/types/send.ts
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'>();
});
33 changes: 33 additions & 0 deletions packages/robot-hooks/test/types/tsconfig.json
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"]
}
}

0 comments on commit 11f39ae

Please sign in to comment.