Skip to content

Commit da2fad9

Browse files
committed
Add overloads to make EntityAdapter methods createSlice-compatible
The overloads that had `TypeOrPayloadAction<T>` were resulting in a payload of `undefined` for the associated action creator when passed directly as a case reducer to `createSlice`. Adding overloads that explicitly reference `PayloadAction<T>` allows the inference to work correctly so that action payloads are detected.
1 parent d13ffde commit da2fad9

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/entities/models.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,33 @@ export interface EntityDefinition<T> {
6969

7070
export interface EntityStateAdapter<T> {
7171
addOne<S extends EntityState<T>>(state: S, entity: TypeOrPayloadAction<T>): S
72+
addOne<S extends EntityState<T>>(state: S, action: PayloadAction<T>): S
73+
7274
addMany<S extends EntityState<T>>(
7375
state: S,
7476
entities: TypeOrPayloadAction<T[]>
7577
): S
78+
addMany<S extends EntityState<T>>(state: S, entities: PayloadAction<T[]>): S
7679

7780
setAll<S extends EntityState<T>>(
7881
state: S,
7982
entities: TypeOrPayloadAction<T[]>
8083
): S
84+
setAll<S extends EntityState<T>>(state: S, entities: PayloadAction<T[]>): S
8185

8286
removeOne<S extends EntityState<T>>(
8387
state: S,
84-
key: TypeOrPayloadAction<string>
85-
): S
86-
removeOne<S extends EntityState<T>>(
87-
state: S,
88-
key: TypeOrPayloadAction<number>
88+
key: TypeOrPayloadAction<EntityId>
8989
): S
90+
removeOne<S extends EntityState<T>>(state: S, key: PayloadAction<EntityId>): S
9091

9192
removeMany<S extends EntityState<T>>(
9293
state: S,
93-
keys: TypeOrPayloadAction<string[]>
94+
keys: TypeOrPayloadAction<EntityId[]>
9495
): S
9596
removeMany<S extends EntityState<T>>(
9697
state: S,
97-
keys: TypeOrPayloadAction<number[]>
98+
keys: PayloadAction<number[]>
9899
): S
99100

100101
removeAll<S extends EntityState<T>>(state: S): S
@@ -103,28 +104,44 @@ export interface EntityStateAdapter<T> {
103104
state: S,
104105
update: TypeOrPayloadAction<Update<T>>
105106
): S
107+
updateOne<S extends EntityState<T>>(
108+
state: S,
109+
update: PayloadAction<Update<T>>
110+
): S
111+
106112
updateMany<S extends EntityState<T>>(
107113
state: S,
108114
updates: TypeOrPayloadAction<Update<T>[]>
109115
): S
116+
updateMany<S extends EntityState<T>>(
117+
state: S,
118+
updates: PayloadAction<Update<T>[]>
119+
): S
110120

111121
upsertOne<S extends EntityState<T>>(
112122
state: S,
113123
entity: TypeOrPayloadAction<T>
114124
): S
125+
upsertOne<S extends EntityState<T>>(state: S, entity: PayloadAction<T>): S
126+
115127
upsertMany<S extends EntityState<T>>(
116128
state: S,
117129
entities: TypeOrPayloadAction<T[]>
118130
): S
131+
upsertMany<S extends EntityState<T>>(
132+
state: S,
133+
entities: PayloadAction<T[]>
134+
): S
119135

120136
map<S extends EntityState<T>>(
121137
state: S,
122138
map: TypeOrPayloadAction<EntityMap<T>>
123139
): S
140+
map<S extends EntityState<T>>(state: S, map: PayloadAction<EntityMap<T>>): S
124141
}
125142

126143
export interface EntitySelectors<T, V> {
127-
selectIds: (state: V) => string[] | number[]
144+
selectIds: (state: V) => EntityId[]
128145
selectEntities: (state: V) => Dictionary<T>
129146
selectAll: (state: V) => T[]
130147
selectTotal: (state: V) => number

0 commit comments

Comments
 (0)