From 1c1d195b057fd855954969d6029ec05c9ce65704 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 21:00:17 +0100 Subject: [PATCH 01/18] Use Record instead of Dictionary --- docs/api/createEntityAdapter.mdx | 21 ++++--------------- packages/toolkit/src/entities/models.ts | 18 ++-------------- .../toolkit/src/entities/state_selectors.ts | 10 +++------ 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/docs/api/createEntityAdapter.mdx b/docs/api/createEntityAdapter.mdx index 0cfee25b9e..10eb7afbe1 100644 --- a/docs/api/createEntityAdapter.mdx +++ b/docs/api/createEntityAdapter.mdx @@ -118,19 +118,11 @@ export type Comparer = (a: T, b: T) => number export type IdSelector = (model: T) => EntityId -export interface DictionaryNum { - [id: number]: T | undefined -} - -export interface Dictionary extends DictionaryNum { - [id: string]: T | undefined -} - export type Update = { id: EntityId; changes: Partial } export interface EntityState { ids: EntityId[] - entities: Dictionary + entities: Record } export interface EntityDefinition { @@ -183,7 +175,7 @@ export interface EntityStateAdapter { export interface EntitySelectors { selectIds: (state: V) => EntityId[] - selectEntities: (state: V) => Dictionary + selectEntities: (state: V) => Record selectAll: (state: V) => T[] selectTotal: (state: V) => number selectById: (state: V, id: EntityId) => T | undefined @@ -228,7 +220,6 @@ All three options will insert _new_ entities into the list. However they differ ::: - Each method has a signature that looks like: ```ts no-transpile @@ -358,12 +349,8 @@ const booksSlice = createSlice({ }, }) -const { - bookAdded, - booksLoading, - booksReceived, - bookUpdated, -} = booksSlice.actions +const { bookAdded, booksLoading, booksReceived, bookUpdated } = + booksSlice.actions const store = configureStore({ reducer: { diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index e57c4aee9a..67ed9cb31e 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -16,20 +16,6 @@ export type Comparer = (a: T, b: T) => number */ export type IdSelector = (model: T) => EntityId -/** - * @public - */ -export interface DictionaryNum { - [id: number]: T | undefined -} - -/** - * @public - */ -export interface Dictionary extends DictionaryNum { - [id: string]: T | undefined -} - /** * @public */ @@ -40,7 +26,7 @@ export type Update = { id: EntityId; changes: Partial } */ export interface EntityState { ids: EntityId[] - entities: Dictionary + entities: Record } /** @@ -150,7 +136,7 @@ export interface EntityStateAdapter { */ export interface EntitySelectors { selectIds: (state: V) => EntityId[] - selectEntities: (state: V) => Dictionary + selectEntities: (state: V) => Record selectAll: (state: V) => T[] selectTotal: (state: V) => number selectById: (state: V, id: EntityId) => T | undefined diff --git a/packages/toolkit/src/entities/state_selectors.ts b/packages/toolkit/src/entities/state_selectors.ts index 46f59d3d9e..1a14d12017 100644 --- a/packages/toolkit/src/entities/state_selectors.ts +++ b/packages/toolkit/src/entities/state_selectors.ts @@ -1,11 +1,6 @@ import type { Selector } from 'reselect' import { createDraftSafeSelector } from '../createDraftSafeSelector' -import type { - EntityState, - EntitySelectors, - Dictionary, - EntityId, -} from './models' +import type { EntityState, EntitySelectors, EntityId } from './models' export function createSelectorsFactory() { function getSelectors(): EntitySelectors> @@ -27,7 +22,8 @@ export function createSelectorsFactory() { const selectId = (_: unknown, id: EntityId) => id - const selectById = (entities: Dictionary, id: EntityId) => entities[id] + const selectById = (entities: Record, id: EntityId) => + entities[id] const selectTotal = createDraftSafeSelector(selectIds, (ids) => ids.length) From d2100de7ba2b9e4c2a19fcbed4682a79bb897c11 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 21:06:16 +0100 Subject: [PATCH 02/18] rm Dictionary export --- packages/toolkit/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/toolkit/src/index.ts b/packages/toolkit/src/index.ts index dc1f333730..aa8754ff31 100644 --- a/packages/toolkit/src/index.ts +++ b/packages/toolkit/src/index.ts @@ -100,7 +100,6 @@ export { MiddlewareArray, EnhancerArray } from './utils' export { createEntityAdapter } from './entities/create_adapter' export type { - Dictionary, EntityState, EntityAdapter, EntitySelectors, From 616be07293901046359a3ff05da94075c62de992 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 21:16:09 +0100 Subject: [PATCH 03/18] use workaround to match UncheckedIndexedAccess setting --- packages/toolkit/src/entities/index.ts | 1 - packages/toolkit/src/entities/models.ts | 4 ++-- packages/toolkit/src/tsHelpers.ts | 10 ++++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/toolkit/src/entities/index.ts b/packages/toolkit/src/entities/index.ts index 9e2292a24c..e258527474 100644 --- a/packages/toolkit/src/entities/index.ts +++ b/packages/toolkit/src/entities/index.ts @@ -1,6 +1,5 @@ export { createEntityAdapter } from './create_adapter' export type { - Dictionary, EntityState, EntityAdapter, Update, diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index 67ed9cb31e..7f9f5b4df5 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -1,5 +1,5 @@ import type { PayloadAction } from '../createAction' -import type { IsAny } from '../tsHelpers' +import type { IsAny, UncheckedIndexedAccess } from '../tsHelpers' /** * @public @@ -139,7 +139,7 @@ export interface EntitySelectors { selectEntities: (state: V) => Record selectAll: (state: V) => T[] selectTotal: (state: V) => number - selectById: (state: V, id: EntityId) => T | undefined + selectById: (state: V, id: EntityId) => UncheckedIndexedAccess } /** diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index 1b331c9962..7fa472c4cd 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -207,3 +207,13 @@ export type Id = { [K in keyof T]: T[K] } & {} export type Tail = T extends [any, ...infer Tail] ? Tail : never + +declare const record: Record + +const value = record[0] + +export type UncheckedIndexedAccess = IfMaybeUndefined< + typeof value, + T | undefined, + T +> From e8dc3ade1aea9e218c27579f88351f0f5a6a2432 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 21:20:28 +0100 Subject: [PATCH 04/18] code golf --- packages/toolkit/src/tsHelpers.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index 7fa472c4cd..419465eb36 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -208,9 +208,7 @@ export type Tail = T extends [any, ...infer Tail] ? Tail : never -declare const record: Record - -const value = record[0] +const value = ({} as Record)[0] export type UncheckedIndexedAccess = IfMaybeUndefined< typeof value, From 5a10068144521f55278602c7f8bf73fdc7e1ff19 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 22:18:19 +0100 Subject: [PATCH 05/18] Revert "code golf" This reverts commit e8dc3ade1aea9e218c27579f88351f0f5a6a2432. --- packages/toolkit/src/tsHelpers.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index 419465eb36..7fa472c4cd 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -208,7 +208,9 @@ export type Tail = T extends [any, ...infer Tail] ? Tail : never -const value = ({} as Record)[0] +declare const record: Record + +const value = record[0] export type UncheckedIndexedAccess = IfMaybeUndefined< typeof value, From 5a28ae832850471b527736455eb010bdc920c39a Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 22:21:42 +0100 Subject: [PATCH 06/18] give record a runtime value --- packages/toolkit/src/tsHelpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index 7fa472c4cd..c33cab19bd 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -208,7 +208,7 @@ export type Tail = T extends [any, ...infer Tail] ? Tail : never -declare const record: Record +const record: Record = {} const value = record[0] From dbd789862160fde089892f27ffb333e3506f2124 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 22:31:51 +0100 Subject: [PATCH 07/18] remove UncheckedIndexedAccess hack, as it doesn't work after compilation --- packages/toolkit/src/entities/models.ts | 4 ++-- packages/toolkit/src/tsHelpers.ts | 10 ---------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index 7f9f5b4df5..67ed9cb31e 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -1,5 +1,5 @@ import type { PayloadAction } from '../createAction' -import type { IsAny, UncheckedIndexedAccess } from '../tsHelpers' +import type { IsAny } from '../tsHelpers' /** * @public @@ -139,7 +139,7 @@ export interface EntitySelectors { selectEntities: (state: V) => Record selectAll: (state: V) => T[] selectTotal: (state: V) => number - selectById: (state: V, id: EntityId) => UncheckedIndexedAccess + selectById: (state: V, id: EntityId) => T | undefined } /** diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index c33cab19bd..1b331c9962 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -207,13 +207,3 @@ export type Id = { [K in keyof T]: T[K] } & {} export type Tail = T extends [any, ...infer Tail] ? Tail : never - -const record: Record = {} - -const value = record[0] - -export type UncheckedIndexedAccess = IfMaybeUndefined< - typeof value, - T | undefined, - T -> From 2ac6598ea6229d3dfa334c68a2956c26aa00bcb2 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 23:38:11 +0100 Subject: [PATCH 08/18] try some weirdness --- packages/toolkit/src/entities/models.ts | 5 ++++- packages/toolkit/src/tsHelpers.ts | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index 67ed9cb31e..beed70adc8 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -139,7 +139,10 @@ export interface EntitySelectors { selectEntities: (state: V) => Record selectAll: (state: V) => T[] selectTotal: (state: V) => number - selectById: (state: V, id: EntityId) => T | undefined + selectById: ( + state: V, + id: EntityId + ) => import('@reduxjs/toolkit/dist/tsHelpers').UncheckedIndexedAccess } /** diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index 1b331c9962..cf1527ac48 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -207,3 +207,11 @@ export type Id = { [K in keyof T]: T[K] } & {} export type Tail = T extends [any, ...infer Tail] ? Tail : never + +const value = ({} as Record).a + +export type UncheckedIndexedAccess = IfMaybeUndefined< + typeof value, + T | undefined, + T +> From 5a1831ef482c1e306d82456287e1494afe8ba167 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 23:39:59 +0100 Subject: [PATCH 09/18] use a function --- packages/toolkit/src/tsHelpers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index cf1527ac48..de4d988279 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -208,10 +208,10 @@ export type Tail = T extends [any, ...infer Tail] ? Tail : never -const value = ({} as Record).a +const getValue = () => (({} as Record).a) export type UncheckedIndexedAccess = IfMaybeUndefined< - typeof value, + ReturnType, T | undefined, T > From fec6339c6433303566fe0a2119bed5429a8e5457 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 7 May 2023 23:45:22 +0100 Subject: [PATCH 10/18] src not dist --- packages/toolkit/src/entities/models.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index beed70adc8..ad46f59741 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -142,7 +142,7 @@ export interface EntitySelectors { selectById: ( state: V, id: EntityId - ) => import('@reduxjs/toolkit/dist/tsHelpers').UncheckedIndexedAccess + ) => import('@reduxjs/toolkit/src/tsHelpers').UncheckedIndexedAccess } /** From 947c5a9741eec30ac7796b84a23d6274657c8592 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 8 May 2023 00:06:28 +0100 Subject: [PATCH 11/18] move UncheckedIndexedAccess to separate file --- packages/toolkit/src/entities/models.ts | 2 +- packages/toolkit/src/tsHelpers.ts | 8 -------- packages/toolkit/src/userland.ts | 11 +++++++++++ 3 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 packages/toolkit/src/userland.ts diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index ad46f59741..607bbf30dd 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -142,7 +142,7 @@ export interface EntitySelectors { selectById: ( state: V, id: EntityId - ) => import('@reduxjs/toolkit/src/tsHelpers').UncheckedIndexedAccess + ) => import('@reduxjs/toolkit/src/userland').UncheckedIndexedAccess } /** diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index de4d988279..1b331c9962 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -207,11 +207,3 @@ export type Id = { [K in keyof T]: T[K] } & {} export type Tail = T extends [any, ...infer Tail] ? Tail : never - -const getValue = () => (({} as Record).a) - -export type UncheckedIndexedAccess = IfMaybeUndefined< - ReturnType, - T | undefined, - T -> diff --git a/packages/toolkit/src/userland.ts b/packages/toolkit/src/userland.ts new file mode 100644 index 0000000000..eecdf98fa2 --- /dev/null +++ b/packages/toolkit/src/userland.ts @@ -0,0 +1,11 @@ +import type { IfMaybeUndefined } from '@reduxjs/toolkit/dist/tsHelpers' + +// helpers that should be imported like `import('@reduxjs/toolkit/src/userland')` + +const getValue = () => (({} as Record).a) + +export type UncheckedIndexedAccess = IfMaybeUndefined< + ReturnType, + T | undefined, + T +> From 7a0d01e4f91cdb0e8679fbac669f4f6e5afd0c99 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 8 May 2023 11:22:19 +0100 Subject: [PATCH 12/18] inline IfMaybeUndefined instead of importing from dist --- packages/toolkit/src/userland.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/toolkit/src/userland.ts b/packages/toolkit/src/userland.ts index eecdf98fa2..76f76f9d52 100644 --- a/packages/toolkit/src/userland.ts +++ b/packages/toolkit/src/userland.ts @@ -1,11 +1,14 @@ -import type { IfMaybeUndefined } from '@reduxjs/toolkit/dist/tsHelpers' +/* +these helpers should be imported like `import('@reduxjs/toolkit/src/userland').Helper` +because they depend on remaining as a .ts file instead of being in a .d.ts +*/ -// helpers that should be imported like `import('@reduxjs/toolkit/src/userland')` +type IfMaybeUndefined = [undefined] extends [P] ? True : False -const getValue = () => (({} as Record).a) +const value = ({} as Record).a export type UncheckedIndexedAccess = IfMaybeUndefined< - ReturnType, + typeof value, T | undefined, T > From 8c07160098ecff30ed034251c6d3364fcdfce8f9 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 8 May 2023 11:33:03 +0100 Subject: [PATCH 13/18] guard against any --- packages/toolkit/src/entities/models.ts | 9 ++++++--- packages/toolkit/src/tsHelpers.ts | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index 607bbf30dd..b07042117e 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -1,5 +1,5 @@ import type { PayloadAction } from '../createAction' -import type { IsAny } from '../tsHelpers' +import type { CastAny } from '../tsHelpers' /** * @public @@ -37,7 +37,7 @@ export interface EntityDefinition { sortComparer: false | Comparer } -export type PreventAny = IsAny, S> +export type PreventAny = CastAny> /** * @public @@ -142,7 +142,10 @@ export interface EntitySelectors { selectById: ( state: V, id: EntityId - ) => import('@reduxjs/toolkit/src/userland').UncheckedIndexedAccess + ) => CastAny< + import('@reduxjs/toolkit/src/userland').UncheckedIndexedAccess, + T + > } /** diff --git a/packages/toolkit/src/tsHelpers.ts b/packages/toolkit/src/tsHelpers.ts index 1b331c9962..ecf67c0242 100644 --- a/packages/toolkit/src/tsHelpers.ts +++ b/packages/toolkit/src/tsHelpers.ts @@ -18,6 +18,8 @@ export type IsAny = // test if we are going the left AND right path in the condition true | false extends (T extends never ? true : false) ? True : False +export type CastAny = IsAny + /** * return True if T is `unknown`, otherwise return False * taken from /~https://github.com/joonhocho/tsdef From 56ba355192f79dd8914294bc1987dd657614457a Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 8 May 2023 12:11:17 +0100 Subject: [PATCH 14/18] test using external package --- packages/toolkit/package.json | 3 ++- packages/toolkit/src/entities/models.ts | 9 ++------- yarn.lock | 8 ++++++++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index 6aec87a2e3..57d215df66 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -107,7 +107,8 @@ "immer": "^10.0.1", "redux": "5.0.0-alpha.5", "redux-thunk": "3.0.0-alpha.3", - "reselect": "^4.1.8" + "reselect": "^4.1.8", + "uncheckedindexed": "/~https://github.com/EskiMojo14/uncheckedindexed" }, "peerDependencies": { "react": "^16.9.0 || ^17.0.0 || ^18", diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index b07042117e..3d03c0aaf0 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -1,3 +1,4 @@ +import type { UncheckedIndexedAccess } from 'uncheckedindexed' import type { PayloadAction } from '../createAction' import type { CastAny } from '../tsHelpers' @@ -139,13 +140,7 @@ export interface EntitySelectors { selectEntities: (state: V) => Record selectAll: (state: V) => T[] selectTotal: (state: V) => number - selectById: ( - state: V, - id: EntityId - ) => CastAny< - import('@reduxjs/toolkit/src/userland').UncheckedIndexedAccess, - T - > + selectById: (state: V, id: EntityId) => CastAny, T> } /** diff --git a/yarn.lock b/yarn.lock index e1040f2f6b..5025a4fdd9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6793,6 +6793,7 @@ __metadata: tsup: ^6.7.0 tsx: ^3.12.2 typescript: ~4.9 + uncheckedindexed: "/~https://github.com/EskiMojo14/uncheckedindexed" vitest: ^0.30.1 yargs: ^15.3.1 peerDependencies: @@ -28408,6 +28409,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"uncheckedindexed@/~https://github.com/EskiMojo14/uncheckedindexed": + version: 0.0.1 + resolution: "uncheckedindexed@/~https://github.com/EskiMojo14/uncheckedindexed.git#commit=d08490cc8efa3fcf3fac05f55377681e0823c6e3" + checksum: 1cdc6ab2963da63582603e481b4fbf834ba533d8f83e3c4d2aeb682be6866327b585a1440497fd41e13c9d5196f84316dba27c8ac8168ed31d2b1f46d232c395 + languageName: node + linkType: hard + "unherit@npm:^1.0.4": version: 1.1.3 resolution: "unherit@npm:1.1.3" From 66b3f7914f64c2baaef6a84118bdc3a70d9f85bb Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 8 May 2023 12:26:31 +0100 Subject: [PATCH 15/18] remove any guard and use Id helper --- packages/toolkit/src/entities/models.ts | 4 ++-- packages/toolkit/src/userland.ts | 14 -------------- 2 files changed, 2 insertions(+), 16 deletions(-) delete mode 100644 packages/toolkit/src/userland.ts diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index 3d03c0aaf0..a3556dcd5b 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -1,6 +1,6 @@ import type { UncheckedIndexedAccess } from 'uncheckedindexed' import type { PayloadAction } from '../createAction' -import type { CastAny } from '../tsHelpers' +import type { CastAny, Id } from '../tsHelpers' /** * @public @@ -140,7 +140,7 @@ export interface EntitySelectors { selectEntities: (state: V) => Record selectAll: (state: V) => T[] selectTotal: (state: V) => number - selectById: (state: V, id: EntityId) => CastAny, T> + selectById: (state: V, id: EntityId) => Id> } /** diff --git a/packages/toolkit/src/userland.ts b/packages/toolkit/src/userland.ts deleted file mode 100644 index 76f76f9d52..0000000000 --- a/packages/toolkit/src/userland.ts +++ /dev/null @@ -1,14 +0,0 @@ -/* -these helpers should be imported like `import('@reduxjs/toolkit/src/userland').Helper` -because they depend on remaining as a .ts file instead of being in a .d.ts -*/ - -type IfMaybeUndefined = [undefined] extends [P] ? True : False - -const value = ({} as Record).a - -export type UncheckedIndexedAccess = IfMaybeUndefined< - typeof value, - T | undefined, - T -> From c3cd7e965f376a1ac09a9a111e66669a7529eff9 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 8 May 2023 14:30:42 +0100 Subject: [PATCH 16/18] install from NPM --- packages/toolkit/package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index 57d215df66..5caf09ff3f 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -108,7 +108,7 @@ "redux": "5.0.0-alpha.5", "redux-thunk": "3.0.0-alpha.3", "reselect": "^4.1.8", - "uncheckedindexed": "/~https://github.com/EskiMojo14/uncheckedindexed" + "uncheckedindexed": "^0.0.1" }, "peerDependencies": { "react": "^16.9.0 || ^17.0.0 || ^18", diff --git a/yarn.lock b/yarn.lock index 5025a4fdd9..1f01006828 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6793,7 +6793,7 @@ __metadata: tsup: ^6.7.0 tsx: ^3.12.2 typescript: ~4.9 - uncheckedindexed: "/~https://github.com/EskiMojo14/uncheckedindexed" + uncheckedindexed: ^0.0.1 vitest: ^0.30.1 yargs: ^15.3.1 peerDependencies: @@ -28409,10 +28409,10 @@ fsevents@^1.2.7: languageName: node linkType: hard -"uncheckedindexed@/~https://github.com/EskiMojo14/uncheckedindexed": +"uncheckedindexed@npm:^0.0.1": version: 0.0.1 - resolution: "uncheckedindexed@/~https://github.com/EskiMojo14/uncheckedindexed.git#commit=d08490cc8efa3fcf3fac05f55377681e0823c6e3" - checksum: 1cdc6ab2963da63582603e481b4fbf834ba533d8f83e3c4d2aeb682be6866327b585a1440497fd41e13c9d5196f84316dba27c8ac8168ed31d2b1f46d232c395 + resolution: "uncheckedindexed@npm:0.0.1" + checksum: 66cf7c42df36eeb8520de61ed16df0b1506bb40ede645d9c146672799d2392a3ad8c7b54131b07ff764414dcc12bd74eca90c3fdf13f19631181e6198f916075 languageName: node linkType: hard From 7ed2439b0d747a13b346243673d43b8f79726ff3 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 8 May 2023 21:08:11 +0100 Subject: [PATCH 17/18] inline uncheckedindexed --- packages/toolkit/package.json | 3 +-- packages/toolkit/src/entities/models.ts | 2 +- packages/toolkit/src/uncheckedindexed.ts | 16 ++++++++++++++++ packages/toolkit/tsup.config.ts | 9 +++++++++ yarn.lock | 8 -------- 5 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 packages/toolkit/src/uncheckedindexed.ts diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index 5caf09ff3f..6aec87a2e3 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -107,8 +107,7 @@ "immer": "^10.0.1", "redux": "5.0.0-alpha.5", "redux-thunk": "3.0.0-alpha.3", - "reselect": "^4.1.8", - "uncheckedindexed": "^0.0.1" + "reselect": "^4.1.8" }, "peerDependencies": { "react": "^16.9.0 || ^17.0.0 || ^18", diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts index a3556dcd5b..502d4c5d8b 100644 --- a/packages/toolkit/src/entities/models.ts +++ b/packages/toolkit/src/entities/models.ts @@ -1,4 +1,4 @@ -import type { UncheckedIndexedAccess } from 'uncheckedindexed' +import type { UncheckedIndexedAccess } from '../uncheckedindexed' import type { PayloadAction } from '../createAction' import type { CastAny, Id } from '../tsHelpers' diff --git a/packages/toolkit/src/uncheckedindexed.ts b/packages/toolkit/src/uncheckedindexed.ts new file mode 100644 index 0000000000..628ff5dc43 --- /dev/null +++ b/packages/toolkit/src/uncheckedindexed.ts @@ -0,0 +1,16 @@ +// inlined from /~https://github.com/EskiMojo14/uncheckedindexed +// relies on remaining as a TS file, not .d.ts +type IfMaybeUndefined = [undefined] extends [T] ? True : False + +const testAccess = ({} as Record).a + +export type IfUncheckedIndexedAccess = IfMaybeUndefined< + typeof testAccess, + True, + False +> + +export type UncheckedIndexedAccess = IfUncheckedIndexedAccess< + T | undefined, + T +> diff --git a/packages/toolkit/tsup.config.ts b/packages/toolkit/tsup.config.ts index a7195c0f73..107f0e440e 100644 --- a/packages/toolkit/tsup.config.ts +++ b/packages/toolkit/tsup.config.ts @@ -196,6 +196,15 @@ export default defineConfig((options) => { if (format === 'cjs' && name === 'production.min') { writeCommonJSEntry(outputFolder, prefix) } else if (generateTypedefs) { + if (folder === '') { + // we need to delete the declaration file and replace it with the original source file + fs.rmSync(path.join(outputFolder, 'uncheckedindexed.d.ts')) + + fs.copyFileSync( + 'src/uncheckedindexed.ts', + path.join(outputFolder, 'uncheckedindexed.ts') + ) + } // TODO Copy/generate `.d.mts` files? // const inputTypedefsFile = `${outputFilename}.d.ts` // const outputTypedefsFile = `${outputFilename}.d.mts` diff --git a/yarn.lock b/yarn.lock index 1f01006828..e1040f2f6b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6793,7 +6793,6 @@ __metadata: tsup: ^6.7.0 tsx: ^3.12.2 typescript: ~4.9 - uncheckedindexed: ^0.0.1 vitest: ^0.30.1 yargs: ^15.3.1 peerDependencies: @@ -28409,13 +28408,6 @@ fsevents@^1.2.7: languageName: node linkType: hard -"uncheckedindexed@npm:^0.0.1": - version: 0.0.1 - resolution: "uncheckedindexed@npm:0.0.1" - checksum: 66cf7c42df36eeb8520de61ed16df0b1506bb40ede645d9c146672799d2392a3ad8c7b54131b07ff764414dcc12bd74eca90c3fdf13f19631181e6198f916075 - languageName: node - linkType: hard - "unherit@npm:^1.0.4": version: 1.1.3 resolution: "unherit@npm:1.1.3" From 6a11af6db0e4bcb14aeec7da84a8ff5cd3e7a8c9 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Mon, 29 May 2023 23:15:00 +0100 Subject: [PATCH 18/18] {} as Record --- packages/toolkit/src/entities/entity_state.ts | 2 +- packages/toolkit/src/entities/sorted_state_adapter.ts | 2 +- packages/toolkit/src/entities/unsorted_state_adapter.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/toolkit/src/entities/entity_state.ts b/packages/toolkit/src/entities/entity_state.ts index 8a9c6a895c..62ab918442 100644 --- a/packages/toolkit/src/entities/entity_state.ts +++ b/packages/toolkit/src/entities/entity_state.ts @@ -6,7 +6,7 @@ export function getInitialEntityState(): EntityState< > { return { ids: [], - entities: {}, + entities: {} as Record, } } diff --git a/packages/toolkit/src/entities/sorted_state_adapter.ts b/packages/toolkit/src/entities/sorted_state_adapter.ts index 96bb75da4a..7d73513671 100644 --- a/packages/toolkit/src/entities/sorted_state_adapter.ts +++ b/packages/toolkit/src/entities/sorted_state_adapter.ts @@ -61,7 +61,7 @@ export function createSortedStateAdapter( state: R ): void { newEntities = ensureEntitiesArray(newEntities) - state.entities = {} + state.entities = {} as Record state.ids = [] addManyMutably(newEntities, state) diff --git a/packages/toolkit/src/entities/unsorted_state_adapter.ts b/packages/toolkit/src/entities/unsorted_state_adapter.ts index 9c3295dc43..77697e1618 100644 --- a/packages/toolkit/src/entities/unsorted_state_adapter.ts +++ b/packages/toolkit/src/entities/unsorted_state_adapter.ts @@ -67,7 +67,7 @@ export function createUnsortedStateAdapter( newEntities = ensureEntitiesArray(newEntities) state.ids = [] - state.entities = {} + state.entities = {} as Record addManyMutably(newEntities, state) }