Skip to content

Commit

Permalink
Fix setInterstate does not infer the type of the parameter in a sette…
Browse files Browse the repository at this point in the history
…r function (schema interface)
  • Loading branch information
turtleflyer committed Apr 5, 2021
1 parent 9feb7e9 commit 3eb27d7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.1.1-alpha.2 (Apr 4, 2021)

### Fix

- Fix setInterstate does not infer the type of the parameter in a setter function (schema interface)

## 0.1.1-alpha.1 (Mar 30, 2021)

### Change
Expand Down
2 changes: 2 additions & 0 deletions src/UseInterstateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export type SetInterstate<M extends Interstate = never> = [M] extends [never]
<K extends OnlyStringKeys<M>>(key: K, set: SetInterstateParam<M[K]>): void;

<K extends ExtraStringKeys<M>>(set: SetInterstateSchemaParam<M, K> & EliminateNever<K>): void;

<K extends ExtraStringKeys<M>>(set: SetInterstateSchemaParamFn<M, K>): void;
};

export type SetInterstateParam<T> = Exclude<T, (...x: any) => any> | ((prevValue: T) => T);
Expand Down
4 changes: 4 additions & 0 deletions src/test_suites/post/checkTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ describe('Check types', () => {
go: [state.a],
}));
setInterstateDefined({ a: (x: string) => `${x}new` });
setInterstateDefined((state) => ({
77: undefined,
go: [state.a],
}));

// @ts-expect-error
setInterstateDefined((state: { a: number }) => ({
Expand Down
4 changes: 4 additions & 0 deletions src/test_suites/pre/checkTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ describe('Check types', () => {
go: [state.a],
}));
setInterstateDefined({ a: (x: string) => `${x}new` });
setInterstateDefined((state) => ({
77: undefined,
go: [state.a],
}));

// @ts-expect-error
setInterstateDefined((state: { a: number }) => ({
Expand Down

0 comments on commit 3eb27d7

Please sign in to comment.