Skip to content

Commit

Permalink
Fix store function types (#84)
Browse files Browse the repository at this point in the history
* Add type tests

* Handle edge-cases
  • Loading branch information
AlexandrHoroshih authored Feb 6, 2024
1 parent 0341842 commit 5da0fc7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions public-types/reflect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type BindFromProps<Props> = {
| ((...args: Parameters<Props[K]>) => ReturnType<Props[K]>)
// Edge-case: allow to pass an event listener without any parameters (e.g. onClick: () => ...)
| (() => ReturnType<Props[K]>)
// Edge-case: allow to pass an Store, which contains a function
| Store<Props[K]>
: Store<Props[K]> | Props[K];
};

Expand Down
44 changes: 44 additions & 0 deletions type-tests/types-reflect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,50 @@ import { expectType } from 'tsd';
expectType<React.FC>(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<React.FC>(ReflectedInput);
}

function localize<T extends 'b'>(value: T): { lol: boolean };
function localize<T extends 'a'>(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<typeof localize>(localize);

const ReflectedInput = reflect({
view: Input,
bind: {
value: 'plain string',
onChange: $changed,
},
});

expectType<React.FC>(ReflectedInput);
}

// should support useUnit configuration
{
const Input: React.FC<{
Expand Down

0 comments on commit 5da0fc7

Please sign in to comment.