From af2d15d5b7cc78ce7d68118e62b2c2b54af8da62 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sat, 23 Dec 2023 11:11:06 +0000 Subject: [PATCH 1/3] Preserve nullable store state type by avoiding intersection with {} --- packages/toolkit/src/configureStore.ts | 4 +++- .../toolkit/src/tests/configureStore.typetest.ts | 13 ++++++++++++- packages/toolkit/src/tsHelpers.ts | 2 ++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/toolkit/src/configureStore.ts b/packages/toolkit/src/configureStore.ts index 93bf59017a..e1b1a491e1 100644 --- a/packages/toolkit/src/configureStore.ts +++ b/packages/toolkit/src/configureStore.ts @@ -26,6 +26,7 @@ import type { ExtractDispatchExtensions, ExtractStoreExtensions, ExtractStateExtensions, + UnknownIfNonSpecific, } from './tsHelpers' import type { Tuple } from './utils' import type { GetDefaultEnhancers } from './getDefaultEnhancers' @@ -103,7 +104,8 @@ export type EnhancedStore< S = any, A extends Action = UnknownAction, E extends Enhancers = Enhancers -> = ExtractStoreExtensions & Store, A> +> = ExtractStoreExtensions & + Store>> /** * A friendly abstraction over the standard Redux `createStore()` function. diff --git a/packages/toolkit/src/tests/configureStore.typetest.ts b/packages/toolkit/src/tests/configureStore.typetest.ts index d141f85df2..041f1141a9 100644 --- a/packages/toolkit/src/tests/configureStore.typetest.ts +++ b/packages/toolkit/src/tests/configureStore.typetest.ts @@ -13,7 +13,7 @@ import type { PayloadAction, ConfigureStoreOptions } from '@reduxjs/toolkit' import { configureStore, createSlice, Tuple } from '@reduxjs/toolkit' import type { ThunkMiddleware, ThunkAction, ThunkDispatch } from 'redux-thunk' import { thunk } from 'redux-thunk' -import { expectNotAny, expectType } from './helpers' +import { expectExactType, expectNotAny, expectType } from './helpers' const _anyMiddleware: any = () => () => () => {} @@ -138,6 +138,17 @@ const _anyMiddleware: any = () => () => () => {} }) } +/** + * Test: nullable state is preserved + */ + +{ + const store = configureStore({ + reducer: (): string | null => null, + }) + expectExactType(null)(store.getState()) +} + /* * Test: configureStore() accepts store Tuple, but not plain array */ diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index f0ed92a6e1..bd42de858e 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -205,3 +205,5 @@ export type Id = { [K in keyof T]: T[K] } & {} export type Tail = T extends [any, ...infer Tail] ? Tail : never + +export type UnknownIfNonSpecific = {} extends T ? unknown : T From 6c45d3202c885f47c5b86dc1fc46aff93f28b9b0 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sat, 23 Dec 2023 11:15:36 +0000 Subject: [PATCH 2/3] temporarily use build from PR to prove it works --- packages/toolkit/package.json | 2 +- yarn.lock | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index 0d4f383f14..baf10b6f5f 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -112,7 +112,7 @@ ], "dependencies": { "immer": "^10.0.3", - "redux": "^5.0.0", + "redux": "https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz", "redux-thunk": "^3.1.0", "reselect": "^5.0.1" }, diff --git a/yarn.lock b/yarn.lock index aa0d44b25a..94411f0d04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7075,7 +7075,7 @@ __metadata: node-fetch: ^2.6.1 prettier: ^2.2.1 query-string: ^7.0.1 - redux: ^5.0.0 + redux: "https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz" redux-thunk: ^3.1.0 reselect: ^5.0.1 rimraf: ^3.0.2 @@ -25134,6 +25134,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"redux@https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz": + version: 5.0.0 + resolution: "redux@https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz" + checksum: 9ea2a3a8b387e6c299bd5ac4ec5b26f207ff368546c8a5c226655e57476ee5f87f52724ab2427f31e87e6cdc21566e8132f1dce1db2f5d2003b85d822c350cdc + languageName: node + linkType: hard + "redux@npm:^4.1.2": version: 4.1.2 resolution: "redux@npm:4.1.2" @@ -25152,13 +25159,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"redux@npm:^5.0.0": - version: 5.0.0 - resolution: "redux@npm:5.0.0" - checksum: be49160d4bd01e10108c425ade999f1b456204895c4bdd0c7825ab09efffded51955c5c242847406a7b3f273e9011a9c102848c512a099a75617b97b13d2cca8 - languageName: node - linkType: hard - "reftools@npm:^1.1.9": version: 1.1.9 resolution: "reftools@npm:1.1.9" From cf7dd4302b54dd35caab2d539455eeca1a9c06cb Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sat, 23 Dec 2023 16:56:31 +0000 Subject: [PATCH 3/3] bump to redux 5.0.1 --- packages/toolkit/package.json | 2 +- yarn.lock | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index baf10b6f5f..073b7541a1 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -112,7 +112,7 @@ ], "dependencies": { "immer": "^10.0.3", - "redux": "https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz", + "redux": "^5.0.1", "redux-thunk": "^3.1.0", "reselect": "^5.0.1" }, diff --git a/yarn.lock b/yarn.lock index 94411f0d04..e4e1afe60b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7075,7 +7075,7 @@ __metadata: node-fetch: ^2.6.1 prettier: ^2.2.1 query-string: ^7.0.1 - redux: "https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz" + redux: ^5.0.1 redux-thunk: ^3.1.0 reselect: ^5.0.1 rimraf: ^3.0.2 @@ -25134,13 +25134,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"redux@https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz": - version: 5.0.0 - resolution: "redux@https://pkg.csb.dev/reduxjs/redux/commit/3e9e484f/redux/_pkg.tgz" - checksum: 9ea2a3a8b387e6c299bd5ac4ec5b26f207ff368546c8a5c226655e57476ee5f87f52724ab2427f31e87e6cdc21566e8132f1dce1db2f5d2003b85d822c350cdc - languageName: node - linkType: hard - "redux@npm:^4.1.2": version: 4.1.2 resolution: "redux@npm:4.1.2" @@ -25159,6 +25152,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"redux@npm:^5.0.1": + version: 5.0.1 + resolution: "redux@npm:5.0.1" + checksum: e74affa9009dd5d994878b9a1ce30d6569d986117175056edb003de2651c05b10fe7819d6fa94aea1a94de9a82f252f986547f007a2fbeb35c317a2e5f5ecf2c + languageName: node + linkType: hard + "reftools@npm:^1.1.9": version: 1.1.9 resolution: "reftools@npm:1.1.9"