diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index ca8e34bf82..0a925d106e 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -28,6 +28,7 @@ "types": "dist/index.d.ts", "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", diff --git a/packages/toolkit/src/tests/createAsyncThunk.typetest.ts b/packages/toolkit/src/tests/createAsyncThunk.typetest.ts index 2bfc8c058e..dee99554ec 100644 --- a/packages/toolkit/src/tests/createAsyncThunk.typetest.ts +++ b/packages/toolkit/src/tests/createAsyncThunk.typetest.ts @@ -17,6 +17,7 @@ import type { AsyncThunkFulfilledActionCreator, AsyncThunkRejectedActionCreator, } from '@internal/createAsyncThunk' +import type { TSVersion } from '@phryneas/ts-version' const ANY = {} as any const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction> @@ -287,8 +288,22 @@ const anyAction = { type: 'foo' } as AnyAction // 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(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') diff --git a/yarn.lock b/yarn.lock index 633251fb06..591e946ccb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6421,6 +6421,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" @@ -6554,6 +6561,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