From 3b0606109ab5463281c4c50696932b393f44ab08 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Tue, 25 Oct 2022 12:04:48 +0000 Subject: [PATCH 1/4] wrap type in --- src/hooks/useSelector.ts | 4 ++-- src/types.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSelector.ts b/src/hooks/useSelector.ts index 034b2d216..626b0b483 100644 --- a/src/hooks/useSelector.ts +++ b/src/hooks/useSelector.ts @@ -2,7 +2,7 @@ import { useContext, useDebugValue } from 'react' import { useReduxContext as useDefaultReduxContext } from './useReduxContext' import { ReactReduxContext } from '../components/Context' -import type { EqualityFn } from '../types' +import type { EqualityFn, NoInfer } from '../types' import type { uSESWS } from '../utils/useSyncExternalStore' import { notInitialized } from '../utils/useSyncExternalStore' @@ -32,7 +32,7 @@ export function createSelectorHook( return function useSelector( selector: (state: TState) => Selected, - equalityFn: EqualityFn = refEquality + equalityFn: EqualityFn> = refEquality ): Selected { if (process.env.NODE_ENV !== 'production') { if (!selector) { diff --git a/src/types.ts b/src/types.ts index 5a8017d1e..4e424c994 100644 --- a/src/types.ts +++ b/src/types.ts @@ -168,3 +168,5 @@ export interface TypedUseSelectorHook { equalityFn?: EqualityFn ): TSelected } + +export type NoInfer = [T][T extends any ? 0 : never] From 401250ebee03435bdf23e8b55bf490fd56567438 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Tue, 25 Oct 2022 19:54:11 +0000 Subject: [PATCH 2/4] also update TypedUseSelectorHook --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 4e424c994..90ecebe8d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -165,7 +165,7 @@ export type ResolveThunks = TDispatchProps extends { export interface TypedUseSelectorHook { ( selector: (state: TState) => TSelected, - equalityFn?: EqualityFn + equalityFn?: EqualityFn> ): TSelected } From fc3954a680649d5aae74df03155147b363dafd40 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Thu, 3 Nov 2022 22:03:44 -0400 Subject: [PATCH 3/4] Update test matrix to Node 16 and TS 4.9 --- .github/workflows/test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f26313ce0..776d7d979 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Node uses: actions/setup-node@v2 with: - node-version: 14.x + node-version: 16.x cache: 'yarn' - name: Install dependencies @@ -39,8 +39,8 @@ jobs: strategy: fail-fast: false matrix: - node: ['14.x'] - ts: ['4.0', '4.1', '4.2', '4.3', '4.4', '4.5', '4.6', 'next'] + node: ['16.x'] + ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8', '4.9.2-rc'] steps: - name: Checkout repo uses: actions/checkout@v2 From bafe55eab475589efc2c1aaee89bde244e2fb44e Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Thu, 3 Nov 2022 22:16:31 -0400 Subject: [PATCH 4/4] Add typetests --- test/typetests/hooks.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/typetests/hooks.tsx b/test/typetests/hooks.tsx index 84475e961..9bbce00e4 100644 --- a/test/typetests/hooks.tsx +++ b/test/typetests/hooks.tsx @@ -34,7 +34,7 @@ import { fetchCount, } from './counterApp' -import { expectType } from '../typeTestHelpers' +import { expectType, expectExactType } from '../typeTestHelpers' function preTypedHooksSetup() { // Standard hooks setup @@ -87,6 +87,20 @@ function testShallowEqual() { shallowEqual({ test: 'test' }, { test: 'test' }) shallowEqual({ test: 'test' }, 'a') const x: boolean = shallowEqual('a', 'a') + + type TestState = { stateProp: string } + + // Additionally, it should infer its type from arguments and not become `any` + const selected1 = useSelector( + (state: TestState) => state.stateProp, + shallowEqual + ) + expectExactType(selected1) + + const useAppSelector: TypedUseSelectorHook = useSelector + + const selected2 = useAppSelector((state) => state.stateProp, shallowEqual) + expectExactType(selected2) } function testUseDispatch() {