Skip to content

Commit 59b2de7

Browse files
markeriksonjulian-ford
authored andcommittedOct 4, 2023
Work around known TS bug with type inference
1 parent cab1807 commit 59b2de7

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed
 

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
AsyncThunkFulfilledActionCreator,
1818
AsyncThunkRejectedActionCreator,
1919
} from '@internal/createAsyncThunk'
20+
import type { TSVersion } from '@phryneas/ts-version'
2021

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

0 commit comments

Comments
 (0)
Please sign in to comment.