Skip to content

Commit 083546f

Browse files
committed
Work around known TS bug with type inference
1 parent 6539503 commit 083546f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/toolkit/src/tests/createAsyncThunk.typetest.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
AsyncThunkFulfilledActionCreator,
2222
AsyncThunkRejectedActionCreator,
2323
} from '@internal/createAsyncThunk'
24+
import type { TSVersion } from '@phryneas/ts-version'
2425

2526
const ANY = {} as any
2627
const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, UnknownAction>
@@ -291,8 +292,22 @@ const unknownAction = { type: 'foo' } as UnknownAction
291292
// in that case, we have to forbid this behaviour or it will make arguments optional everywhere
292293
{
293294
const asyncThunk = createAsyncThunk('test', (arg?: number) => 0)
294-
expectType<(arg?: number) => any>(asyncThunk)
295-
asyncThunk()
295+
296+
// Per https://github.com/reduxjs/redux-toolkit/issues/3758#issuecomment-1742152774 , this is a bug in
297+
// TS 5.1 and 5.2, that is fixed in 5.3. Conditionally run the TS assertion here.
298+
type IsTS51Or52 = TSVersion.Major extends 5
299+
? TSVersion.Minor extends 1 | 2
300+
? true
301+
: false
302+
: false
303+
304+
type expectedType = IsTS51Or52 extends true
305+
? (arg: number) => any
306+
: (arg?: number) => any
307+
expectType<expectedType>(asyncThunk)
308+
// We _should_ be able to call this with no arguments, but we run into that error in 5.1 and 5.2.
309+
// Disabling this for now.
310+
// asyncThunk()
296311
asyncThunk(5)
297312
// @ts-expect-error
298313
asyncThunk('string')

0 commit comments

Comments
 (0)