From 5da0fc7a6172ed27ae6e67a8049caf8cd50671de Mon Sep 17 00:00:00 2001 From: Alexander Khoroshikh <32790736+AlexandrHoroshih@users.noreply.github.com> Date: Tue, 6 Feb 2024 14:49:16 +0700 Subject: [PATCH] Fix store function types (#84) * Add type tests * Handle edge-cases --- public-types/reflect.d.ts | 2 ++ type-tests/types-reflect.tsx | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/public-types/reflect.d.ts b/public-types/reflect.d.ts index 360b1eb..10d1e38 100644 --- a/public-types/reflect.d.ts +++ b/public-types/reflect.d.ts @@ -20,6 +20,8 @@ type BindFromProps = { | ((...args: Parameters) => ReturnType) // Edge-case: allow to pass an event listener without any parameters (e.g. onClick: () => ...) | (() => ReturnType) + // Edge-case: allow to pass an Store, which contains a function + | Store : Store | Props[K]; }; diff --git a/type-tests/types-reflect.tsx b/type-tests/types-reflect.tsx index 7a6b15f..232df8b 100644 --- a/type-tests/types-reflect.tsx +++ b/type-tests/types-reflect.tsx @@ -182,6 +182,50 @@ import { expectType } from 'tsd'; expectType(ReflectedInput); } +// should allow store with a function as a callback value +{ + const Input: React.FC<{ + value: string; + onChange: (newValue: string) => void; + }> = () => null; + const $changed = createStore<(newValue: string) => void>(() => {}); + + const ReflectedInput = reflect({ + view: Input, + bind: { + value: 'plain string', + onChange: $changed, + }, + }); + + expectType(ReflectedInput); +} + +function localize(value: T): { lol: boolean }; +function localize(value: T): { kek: boolean }; +function localize(value: string): unknown { + return value; +} + +// should allow store with generics +{ + const Input: React.FC<{ + value: string; + onChange: typeof localize; + }> = () => null; + const $changed = createStore(localize); + + const ReflectedInput = reflect({ + view: Input, + bind: { + value: 'plain string', + onChange: $changed, + }, + }); + + expectType(ReflectedInput); +} + // should support useUnit configuration { const Input: React.FC<{