@@ -46,6 +46,122 @@ new ComputeKit(options?: ComputeKitOptions)
4646
4747---
4848
49+ ## Typed Registry
50+
51+ ComputeKit supports ** type-safe function registration** using TypeScript's declaration merging. This provides:
52+
53+ - ** Autocomplete** for registered function names
54+ - ** Type inference** for input and output types
55+ - ** Compile-time errors** for incorrect usage
56+
57+ ### Setting Up the Typed Registry
58+
59+ Extend the ` ComputeFunctionRegistry ` interface to define your function signatures:
60+
61+ ``` typescript
62+ // In a .d.ts file or at the top of your main file
63+ declare module ' @computekit/core' {
64+ interface ComputeFunctionRegistry {
65+ fibonacci: { input: number ; output: number };
66+ sum: { input: number []; output: number };
67+ processData: { input: { items: string [] }; output: { count: number } };
68+ }
69+ }
70+ ```
71+
72+ ### Using DefineFunction Helper
73+
74+ For cleaner syntax, use the ` DefineFunction ` type helper:
75+
76+ ``` typescript
77+ import type { DefineFunction } from ' @computekit/core' ;
78+
79+ declare module ' @computekit/core' {
80+ interface ComputeFunctionRegistry {
81+ fibonacci: DefineFunction <number , number >;
82+ sum: DefineFunction <number [], number >;
83+ processData: DefineFunction <{ items: string [] }, { count: number }>;
84+ }
85+ }
86+ ```
87+
88+ ### Benefits
89+
90+ Once you've extended the registry, you get full type safety:
91+
92+ ``` typescript
93+ import { ComputeKit } from ' @computekit/core' ;
94+
95+ const kit = new ComputeKit ();
96+
97+ // ✅ Types are inferred - no need for generics!
98+ kit .register (' fibonacci' , (n ) => {
99+ // n is inferred as number
100+ if (n <= 1 ) return n ;
101+ let a = 0 ,
102+ b = 1 ;
103+ for (let i = 2 ; i <= n ; i ++ ) {
104+ [a , b ] = [b , a + b ];
105+ }
106+ return b ; // return type must be number
107+ });
108+
109+ // ✅ Input type is enforced
110+ const result = await kit .run (' fibonacci' , 50 ); // result is number
111+
112+ // ❌ TypeScript error: 'unknownFunc' is not in the registry
113+ await kit .run (' unknownFunc' , 42 );
114+
115+ // ❌ TypeScript error: Argument of type 'string' is not assignable to 'number'
116+ await kit .run (' fibonacci' , ' not a number' );
117+ ```
118+
119+ ### With React Hooks
120+
121+ The typed registry works seamlessly with React hooks:
122+
123+ ``` tsx
124+ import { useCompute , useComputeCallback } from ' @computekit/react' ;
125+
126+ function FibonacciCalculator() {
127+ // ✅ Types are inferred from the registry
128+ const { data, loading, run } = useCompute (' fibonacci' );
129+ // data is number | null
130+ // run expects (input: number) => void
131+
132+ return (
133+ <button onClick = { () => run (50 )} >
134+ { loading ? ' Computing...' : ` Result: ${data } ` }
135+ </button >
136+ );
137+ }
138+ ```
139+
140+ ### Registry Type Helpers
141+
142+ | Type | Description |
143+ | ------------------------- | ---------------------------------------------------------- |
144+ | ` ComputeFunctionRegistry ` | The base interface to extend with your functions |
145+ | ` RegisteredFunctionName ` | Union of all registered function names |
146+ | ` FunctionInput<Name> ` | Get the input type for a function |
147+ | ` FunctionOutput<Name> ` | Get the output type for a function |
148+ | ` DefineFunction<I, O> ` | Helper to define ` { input: I; output: O } ` |
149+ | ` ComputeFn<I, O> ` | Type for compute functions ` (input: I) => O \| Promise<O> ` |
150+ | ` InferComputeFn<Name> ` | Infer the full function type from a name |
151+ | ` HasRegisteredFunctions ` | Boolean type indicating if registry has entries |
152+
153+ ### Fallback Behavior
154+
155+ If you don't extend the registry, ComputeKit falls back to the original behavior with explicit generics:
156+
157+ ``` typescript
158+ // Still works without registry extension
159+ const { data, run } = useCompute <number , number >(' fibonacci' );
160+ const result = await kit .run <number , number >(' fibonacci' , 50 );
161+ ```
162+
163+ ---
164+
49165## Methods
50166
51167### initialize()
0 commit comments