Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around known TS bug with type inference #3761

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"devDependencies": {
"@microsoft/api-extractor": "^7.13.2",
"@phryneas/ts-version": "^1.0.2",
"@size-limit/preset-small-lib": "^4.11.0",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.1.5",
Expand Down
19 changes: 17 additions & 2 deletions packages/toolkit/src/tests/createAsyncThunk.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
AsyncThunkFulfilledActionCreator,
AsyncThunkRejectedActionCreator,
} from '@internal/createAsyncThunk'
import type { TSVersion } from '@phryneas/ts-version'

const ANY = {} as any
const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, UnknownAction>
Expand Down Expand Up @@ -291,8 +292,22 @@ const unknownAction = { type: 'foo' } as UnknownAction
// in that case, we have to forbid this behaviour or it will make arguments optional everywhere
{
const asyncThunk = createAsyncThunk('test', (arg?: number) => 0)
expectType<(arg?: number) => any>(asyncThunk)
asyncThunk()

// Per https://github.com/reduxjs/redux-toolkit/issues/3758#issuecomment-1742152774 , this is a bug in
// TS 5.1 and 5.2, that is fixed in 5.3. Conditionally run the TS assertion here.
type IsTS51Or52 = TSVersion.Major extends 5
? TSVersion.Minor extends 1 | 2
? true
: false
: false

type expectedType = IsTS51Or52 extends true
? (arg: number) => any
: (arg?: number) => any
expectType<expectedType>(asyncThunk)
// We _should_ be able to call this with no arguments, but we run into that error in 5.1 and 5.2.
// Disabling this for now.
// asyncThunk()
asyncThunk(5)
// @ts-expect-error
asyncThunk('string')
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6808,6 +6808,13 @@ __metadata:
languageName: node
linkType: hard

"@phryneas/ts-version@npm:^1.0.2":
version: 1.0.2
resolution: "@phryneas/ts-version@npm:1.0.2"
checksum: d51914a8ea35ff8b686a9379b9e9fe6d5b5feaf2e7511b880d2835015736e33bc82952bbc369918f251d4a755f32f4a9c4a34b0ec4dfdbc3e87a41d26401105c
languageName: node
linkType: hard

"@pmmmwh/react-refresh-webpack-plugin@npm:^0.5.3":
version: 0.5.7
resolution: "@pmmmwh/react-refresh-webpack-plugin@npm:0.5.7"
Expand Down Expand Up @@ -6981,6 +6988,7 @@ __metadata:
resolution: "@reduxjs/toolkit@workspace:packages/toolkit"
dependencies:
"@microsoft/api-extractor": ^7.13.2
"@phryneas/ts-version": ^1.0.2
"@size-limit/preset-small-lib": ^4.11.0
"@testing-library/react": ^13.3.0
"@testing-library/user-event": ^13.1.5
Expand Down
Loading