From d1e6d4e96e13eddaf165c004fc093c38b7f9a630 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Tue, 15 Oct 2019 19:38:53 +0200 Subject: [PATCH 1/4] fix(createClice) fix a bug with TS 3.6 (see #212) --- src/createSlice.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createSlice.ts b/src/createSlice.ts index 42d437023e..c2e160dda5 100644 --- a/src/createSlice.ts +++ b/src/createSlice.ts @@ -73,7 +73,7 @@ export interface CreateSliceOptions< * functions. These reducers should have existing action types used * as the keys, and action creators will _not_ be generated. */ - extraReducers?: CaseReducers + extraReducers?: CaseReducers, any> } type PayloadActions = Record< From e56ec314f05d090f1f65f4a0762e5df2af53476d Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Tue, 15 Oct 2019 19:40:39 +0200 Subject: [PATCH 2/4] fix(types) fix feature detection for TS<3.5, make IsEmptyObj more accurate --- src/tsHelpers.ts | 10 +++------- type-tests/files/createAction.typetest.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/tsHelpers.ts b/src/tsHelpers.ts index b23dcd4207..611cc1353f 100644 --- a/src/tsHelpers.ts +++ b/src/tsHelpers.ts @@ -13,8 +13,8 @@ export type IsUnknown = unknown extends T : False export type IsEmptyObj = T extends any - ? {} extends T - ? IsUnknown> + ? keyof T extends never + ? IsUnknown : False : never @@ -24,11 +24,7 @@ export type IsEmptyObj = T extends any * * versions below 3.5 will return `{}` for unresolvable interference * * versions above will return `unknown` * */ -export type AtLeastTS35 = IsUnknown< - ReturnType<() => T>, - True, - False -> +export type AtLeastTS35 = [True, False][IsUnknown() => T>, 0, 1>] export type IsUnknownOrNonInferrable = AtLeastTS35< IsUnknown, diff --git a/type-tests/files/createAction.typetest.ts b/type-tests/files/createAction.typetest.ts index b4755c462d..232ae547c9 100644 --- a/type-tests/files/createAction.typetest.ts +++ b/type-tests/files/createAction.typetest.ts @@ -172,3 +172,16 @@ function expectType(p: T): T { // typings:expect-error expectType(strLenMetaAction('test').meta) } + +/* + * regression test for https://github.com/reduxjs/redux-starter-kit/issues/214 + */ +{ + const action = createAction<{ input?: string }>('ACTION') + const t: string|undefined = action({input: ""}).payload.input; + + // typings:expect-error + const u: number = action({input: ""}).payload.input; + // typings:expect-error + const v: number = action({input: 3}).payload.input; +} \ No newline at end of file From 6cfd327b957c0ab6306a2beab1d10030ca48a2a3 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Tue, 15 Oct 2019 20:16:33 +0200 Subject: [PATCH 3/4] chore: any-cast NonInferred type --- src/createSlice.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createSlice.ts b/src/createSlice.ts index c2e160dda5..c7b1804e17 100644 --- a/src/createSlice.ts +++ b/src/createSlice.ts @@ -230,7 +230,7 @@ export function createSlice< }) const finalCaseReducers = { ...extraReducers, ...sliceCaseReducersByType } - const reducer = createReducer(initialState, finalCaseReducers) + const reducer = createReducer(initialState, finalCaseReducers as any) return { name, From 5db52aef98197935d0cf519c2564341e825501a9 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Tue, 15 Oct 2019 21:24:52 -0400 Subject: [PATCH 4/4] Formatting --- src/tsHelpers.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/tsHelpers.ts b/src/tsHelpers.ts index 611cc1353f..7b0f517bbd 100644 --- a/src/tsHelpers.ts +++ b/src/tsHelpers.ts @@ -24,7 +24,11 @@ export type IsEmptyObj = T extends any * * versions below 3.5 will return `{}` for unresolvable interference * * versions above will return `unknown` * */ -export type AtLeastTS35 = [True, False][IsUnknown() => T>, 0, 1>] +export type AtLeastTS35 = [True, False][IsUnknown< + ReturnType<() => T>, + 0, + 1 +>] export type IsUnknownOrNonInferrable = AtLeastTS35< IsUnknown,