Skip to content

Commit c56b76b

Browse files
phryneasmarkerikson
authored andcommitted
Fix #212 #214 (#215)
* fix(createClice) fix a bug with TS 3.6 (see #212) * fix(types) fix feature detection for TS<3.5, make IsEmptyObj more accurate * chore: any-cast NonInferred type * Formatting
1 parent 7a44a59 commit c56b76b

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/createSlice.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface CreateSliceOptions<
7373
* functions. These reducers should have existing action types used
7474
* as the keys, and action creators will _not_ be generated.
7575
*/
76-
extraReducers?: CaseReducers<State, any>
76+
extraReducers?: CaseReducers<NoInfer<State>, any>
7777
}
7878

7979
type PayloadActions<Types extends keyof any = string> = Record<
@@ -230,7 +230,7 @@ export function createSlice<
230230
})
231231

232232
const finalCaseReducers = { ...extraReducers, ...sliceCaseReducersByType }
233-
const reducer = createReducer(initialState, finalCaseReducers)
233+
const reducer = createReducer(initialState, finalCaseReducers as any)
234234

235235
return {
236236
name,

src/tsHelpers.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export type IsUnknown<T, True, False = never> = unknown extends T
1313
: False
1414

1515
export type IsEmptyObj<T, True, False = never> = T extends any
16-
? {} extends T
17-
? IsUnknown<T, False, IsAny<T, False, True>>
16+
? keyof T extends never
17+
? IsUnknown<T, False, True>
1818
: False
1919
: never
2020

@@ -24,11 +24,11 @@ export type IsEmptyObj<T, True, False = never> = T extends any
2424
* * versions below 3.5 will return `{}` for unresolvable interference
2525
* * versions above will return `unknown`
2626
* */
27-
export type AtLeastTS35<True, False> = IsUnknown<
27+
export type AtLeastTS35<True, False> = [True, False][IsUnknown<
2828
ReturnType<<T>() => T>,
29-
True,
30-
False
31-
>
29+
0,
30+
1
31+
>]
3232

3333
export type IsUnknownOrNonInferrable<T, True, False> = AtLeastTS35<
3434
IsUnknown<T, True, False>,

type-tests/files/createAction.typetest.ts

+13
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,16 @@ function expectType<T>(p: T): T {
172172
// typings:expect-error
173173
expectType<string>(strLenMetaAction('test').meta)
174174
}
175+
176+
/*
177+
* regression test for https://github.com/reduxjs/redux-starter-kit/issues/214
178+
*/
179+
{
180+
const action = createAction<{ input?: string }>('ACTION')
181+
const t: string|undefined = action({input: ""}).payload.input;
182+
183+
// typings:expect-error
184+
const u: number = action({input: ""}).payload.input;
185+
// typings:expect-error
186+
const v: number = action({input: 3}).payload.input;
187+
}

0 commit comments

Comments
 (0)