From 6539503c09880ce039c5a5112efd6d361aeb449d Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Sun, 1 Oct 2023 14:59:04 -0400 Subject: [PATCH 1/2] Add phryneas/ts-version --- packages/toolkit/package.json | 1 + yarn.lock | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index 21cd8f046e..7b83807e26 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index cd80d41265..e74fb26bf6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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 From 083546fcfce66337a65f76315edccfbf31b4c937 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Sun, 1 Oct 2023 15:02:43 -0400 Subject: [PATCH 2/2] Work around known TS bug with type inference --- .../src/tests/createAsyncThunk.typetest.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/toolkit/src/tests/createAsyncThunk.typetest.ts b/packages/toolkit/src/tests/createAsyncThunk.typetest.ts index e5e90a8e40..05cf4fd604 100644 --- a/packages/toolkit/src/tests/createAsyncThunk.typetest.ts +++ b/packages/toolkit/src/tests/createAsyncThunk.typetest.ts @@ -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> @@ -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(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')