Skip to content

Commit

Permalink
fix(typescript): Setter<T> being assignable to any Setter
Browse files Browse the repository at this point in the history
closes #1818
  • Loading branch information
otonashixav committed Jul 26, 2023
1 parent 039cf60 commit 91db2a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ export function createRoot<T>(fn: RootFunction<T>, detachedOwner?: typeof Owner)

export type Accessor<T> = () => T;

export type Setter<T> = (undefined extends T ? () => undefined : {}) &
(<U extends T>(value: (prev: T) => U) => U) &
(<U extends T>(value: Exclude<U, Function>) => U) &
(<U extends T>(value: Exclude<U, Function> | ((prev: T) => U)) => U);
export type Setter<T> = (undefined extends T ? () => undefined : {}) & {
<U extends T>(value: (prev: T) => U): U;
<U extends T>(value: Exclude<U, Function>): U;
<U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U;
};

export type Signal<T> = [get: Accessor<T>, set: Setter<T>];

Expand Down
8 changes: 8 additions & 0 deletions packages/solid/test/signals.type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,3 +1051,11 @@ createEffect<number>((v: number | string) => 123, "asd");
createComputed<number>((v: number | string) => 123, "asd");
// @ts-expect-error second generic is not inferred and remains as number
createRenderEffect<number>((v: number | string) => 123, "asd");


//////////////////////////////////////////////////////////////////////////
// test setter invariance ////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
declare const s1: Setter<number>;
// @ts-expect-error should be invariant
const s2: Setter<bigint> = s1;

0 comments on commit 91db2a5

Please sign in to comment.