Skip to content

Commit 5da0fc7

Browse files
Fix store function types (#84)
* Add type tests * Handle edge-cases
1 parent 0341842 commit 5da0fc7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

public-types/reflect.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type BindFromProps<Props> = {
2020
| ((...args: Parameters<Props[K]>) => ReturnType<Props[K]>)
2121
// Edge-case: allow to pass an event listener without any parameters (e.g. onClick: () => ...)
2222
| (() => ReturnType<Props[K]>)
23+
// Edge-case: allow to pass an Store, which contains a function
24+
| Store<Props[K]>
2325
: Store<Props[K]> | Props[K];
2426
};
2527

type-tests/types-reflect.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,50 @@ import { expectType } from 'tsd';
182182
expectType<React.FC>(ReflectedInput);
183183
}
184184

185+
// should allow store with a function as a callback value
186+
{
187+
const Input: React.FC<{
188+
value: string;
189+
onChange: (newValue: string) => void;
190+
}> = () => null;
191+
const $changed = createStore<(newValue: string) => void>(() => {});
192+
193+
const ReflectedInput = reflect({
194+
view: Input,
195+
bind: {
196+
value: 'plain string',
197+
onChange: $changed,
198+
},
199+
});
200+
201+
expectType<React.FC>(ReflectedInput);
202+
}
203+
204+
function localize<T extends 'b'>(value: T): { lol: boolean };
205+
function localize<T extends 'a'>(value: T): { kek: boolean };
206+
function localize(value: string): unknown {
207+
return value;
208+
}
209+
210+
// should allow store with generics
211+
{
212+
const Input: React.FC<{
213+
value: string;
214+
onChange: typeof localize;
215+
}> = () => null;
216+
const $changed = createStore<typeof localize>(localize);
217+
218+
const ReflectedInput = reflect({
219+
view: Input,
220+
bind: {
221+
value: 'plain string',
222+
onChange: $changed,
223+
},
224+
});
225+
226+
expectType<React.FC>(ReflectedInput);
227+
}
228+
185229
// should support useUnit configuration
186230
{
187231
const Input: React.FC<{

0 commit comments

Comments
 (0)