Skip to content

Commit a23d037

Browse files
authored
Fix/entity types (#477)
* Export additional Entity types and updated type status flags * Tweak code block CSS * Add export tag for more types
1 parent 55253e8 commit a23d037

File tree

6 files changed

+97
-22
lines changed

6 files changed

+97
-22
lines changed

etc/redux-toolkit.api.md

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export type CaseReducerWithPrepare<State, Action extends PayloadAction> = {
8080
prepare: PrepareAction<Action['payload']>;
8181
};
8282

83-
// @alpha (undocumented)
83+
// @public (undocumented)
8484
export type Comparer<T> = (a: T, b: T) => number;
8585

8686
// @public
@@ -104,7 +104,7 @@ export function createAction<P = void, T extends string = string>(type: T): Payl
104104
// @public
105105
export function createAction<PA extends PrepareAction<any>, T extends string = string>(type: T, prepareAction: PA): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>;
106106

107-
// @alpha (undocumented)
107+
// @public (undocumented)
108108
export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}>(type: string, payloadCreator: (arg: ThunkArg, thunkAPI: GetThunkAPI<ThunkApiConfig>) => Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>> | Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>): ((arg: ThunkArg) => (dispatch: GetDispatch<ThunkApiConfig>, getState: () => GetState<ThunkApiConfig>, extra: GetExtra<ThunkApiConfig>) => Promise<PayloadAction<Returned, string, {
109109
arg: ThunkArg;
110110
requestId: string;
@@ -130,7 +130,7 @@ export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig exten
130130
}>;
131131
};
132132

133-
// @alpha (undocumented)
133+
// @public (undocumented)
134134
export function createEntityAdapter<T>(options?: {
135135
selectId?: IdSelector<T>;
136136
sortComparer?: false | Comparer<T>;
@@ -163,7 +163,7 @@ export interface CreateSliceOptions<State = any, CR extends SliceCaseReducers<St
163163
reducers: ValidateSliceCaseReducers<State, CR>;
164164
}
165165

166-
// @alpha (undocumented)
166+
// @public (undocumented)
167167
export interface Dictionary<T> extends DictionaryNum<T> {
168168
// (undocumented)
169169
[id: string]: T | undefined;
@@ -176,7 +176,7 @@ export interface EnhancedStore<S = any, A extends Action = AnyAction, M extends
176176
dispatch: DispatchForMiddlewares<M> & Dispatch<A>;
177177
}
178178

179-
// @alpha (undocumented)
179+
// @public (undocumented)
180180
export interface EntityAdapter<T> extends EntityStateAdapter<T> {
181181
// (undocumented)
182182
getInitialState(): EntityState<T>;
@@ -192,14 +192,73 @@ export interface EntityAdapter<T> extends EntityStateAdapter<T> {
192192
sortComparer: false | Comparer<T>;
193193
}
194194

195-
// @alpha (undocumented)
195+
// @public (undocumented)
196+
export type EntityId = number | string;
197+
198+
// @public (undocumented)
199+
export interface EntitySelectors<T, V> {
200+
// (undocumented)
201+
selectAll: (state: V) => T[];
202+
// (undocumented)
203+
selectById: (state: V, id: EntityId) => T | undefined;
204+
// (undocumented)
205+
selectEntities: (state: V) => Dictionary<T>;
206+
// (undocumented)
207+
selectIds: (state: V) => EntityId[];
208+
// (undocumented)
209+
selectTotal: (state: V) => number;
210+
}
211+
212+
// @public (undocumented)
196213
export interface EntityState<T> {
197214
// (undocumented)
198215
entities: Dictionary<T>;
199216
// (undocumented)
200217
ids: EntityId[];
201218
}
202219

220+
// @public (undocumented)
221+
export interface EntityStateAdapter<T> {
222+
// (undocumented)
223+
addMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: T[] | Record<EntityId, T>): S;
224+
// (undocumented)
225+
addMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<T[] | Record<EntityId, T>>): S;
226+
// (undocumented)
227+
addOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
228+
// (undocumented)
229+
addOne<S extends EntityState<T>>(state: PreventAny<S, T>, action: PayloadAction<T>): S;
230+
// (undocumented)
231+
removeAll<S extends EntityState<T>>(state: PreventAny<S, T>): S;
232+
// (undocumented)
233+
removeMany<S extends EntityState<T>>(state: PreventAny<S, T>, keys: EntityId[]): S;
234+
// (undocumented)
235+
removeMany<S extends EntityState<T>>(state: PreventAny<S, T>, keys: PayloadAction<EntityId[]>): S;
236+
// (undocumented)
237+
removeOne<S extends EntityState<T>>(state: PreventAny<S, T>, key: EntityId): S;
238+
// (undocumented)
239+
removeOne<S extends EntityState<T>>(state: PreventAny<S, T>, key: PayloadAction<EntityId>): S;
240+
// (undocumented)
241+
setAll<S extends EntityState<T>>(state: PreventAny<S, T>, entities: T[] | Record<EntityId, T>): S;
242+
// (undocumented)
243+
setAll<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<T[] | Record<EntityId, T>>): S;
244+
// (undocumented)
245+
updateMany<S extends EntityState<T>>(state: PreventAny<S, T>, updates: Update<T>[]): S;
246+
// (undocumented)
247+
updateMany<S extends EntityState<T>>(state: PreventAny<S, T>, updates: PayloadAction<Update<T>[]>): S;
248+
// (undocumented)
249+
updateOne<S extends EntityState<T>>(state: PreventAny<S, T>, update: Update<T>): S;
250+
// (undocumented)
251+
updateOne<S extends EntityState<T>>(state: PreventAny<S, T>, update: PayloadAction<Update<T>>): S;
252+
// (undocumented)
253+
upsertMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: T[] | Record<EntityId, T>): S;
254+
// (undocumented)
255+
upsertMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<T[] | Record<EntityId, T>>): S;
256+
// (undocumented)
257+
upsertOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
258+
// (undocumented)
259+
upsertOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: PayloadAction<T>): S;
260+
}
261+
203262
// @public (undocumented)
204263
export function findNonSerializableValue(value: unknown, path?: ReadonlyArray<string>, isSerializable?: (value: unknown) => boolean, getEntries?: (value: unknown) => [string, any][], ignoredPaths?: string[]): NonSerializableValue | false;
205264

@@ -213,7 +272,7 @@ export function getDefaultMiddleware<S = any, O extends Partial<GetDefaultMiddle
213272
// @public
214273
export function getType<T extends string>(actionCreator: PayloadActionCreator<any, T>): T;
215274

216-
// @alpha (undocumented)
275+
// @public (undocumented)
217276
export type IdSelector<T> = (model: T) => EntityId;
218277

219278
// @public
@@ -278,7 +337,7 @@ export interface SerializableStateInvariantMiddlewareOptions {
278337
warnAfter?: number;
279338
}
280339

281-
// @alpha (undocumented)
340+
// @public (undocumented)
282341
export interface SerializedError {
283342
// (undocumented)
284343
code?: string;
@@ -310,10 +369,10 @@ export { ThunkAction }
310369

311370
export { ThunkDispatch }
312371

313-
// @alpha (undocumented)
372+
// @public (undocumented)
314373
export function unwrapResult<R extends ActionTypesWithOptionalErrorAction>(returned: R): PayloadForActionTypesExcludingErrorActions<R>;
315374

316-
// @alpha (undocumented)
375+
// @public (undocumented)
317376
export type Update<T> = {
318377
id: EntityId;
319378
changes: Partial<T>;

src/createAsyncThunk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type BaseThunkAPI<
2626
}
2727

2828
/**
29-
* @alpha
29+
* @public
3030
*/
3131
export interface SerializedError {
3232
name?: string
@@ -108,7 +108,7 @@ type GetRejectValue<ThunkApiConfig> = ThunkApiConfig extends {
108108
* @param type
109109
* @param payloadCreator
110110
*
111-
* @alpha
111+
* @public
112112
*/
113113
export function createAsyncThunk<
114114
Returned,
@@ -273,7 +273,7 @@ type PayloadForActionTypesExcludingErrorActions<T> = T extends { error: any }
273273
: never
274274

275275
/**
276-
* @alpha
276+
* @public
277277
*/
278278
export function unwrapResult<R extends ActionTypesWithOptionalErrorAction>(
279279
returned: R

src/entities/create_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createUnsortedStateAdapter } from './unsorted_state_adapter'
88
*
99
* @param options
1010
*
11-
* @alpha
11+
* @public
1212
*/
1313
export function createEntityAdapter<T>(
1414
options: {

src/entities/models.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,60 @@ import { PayloadAction } from '../createAction'
22
import { IsAny } from '../tsHelpers'
33

44
/**
5-
* @alpha
5+
* @public
66
*/
77
export type EntityId = number | string
88

99
/**
10-
* @alpha
10+
* @public
1111
*/
1212
export type Comparer<T> = (a: T, b: T) => number
1313

1414
/**
15-
* @alpha
15+
* @public
1616
*/
1717
export type IdSelector<T> = (model: T) => EntityId
1818

1919
/**
20-
* @alpha
20+
* @public
2121
*/
2222
export interface DictionaryNum<T> {
2323
[id: number]: T | undefined
2424
}
2525

2626
/**
27-
* @alpha
27+
* @public
2828
*/
2929
export interface Dictionary<T> extends DictionaryNum<T> {
3030
[id: string]: T | undefined
3131
}
3232

3333
/**
34-
* @alpha
34+
* @public
3535
*/
3636
export type Update<T> = { id: EntityId; changes: Partial<T> }
3737

3838
/**
39-
* @alpha
39+
* @public
4040
*/
4141
export interface EntityState<T> {
4242
ids: EntityId[]
4343
entities: Dictionary<T>
4444
}
4545

46+
/**
47+
* @public
48+
*/
4649
export interface EntityDefinition<T> {
4750
selectId: IdSelector<T>
4851
sortComparer: false | Comparer<T>
4952
}
5053

5154
export type PreventAny<S, T> = IsAny<S, EntityState<T>, S>
5255

56+
/**
57+
* @public
58+
*/
5359
export interface EntityStateAdapter<T> {
5460
addOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S
5561
addOne<S extends EntityState<T>>(
@@ -126,6 +132,9 @@ export interface EntityStateAdapter<T> {
126132
): S
127133
}
128134

135+
/**
136+
* @public
137+
*/
129138
export interface EntitySelectors<T, V> {
130139
selectIds: (state: V) => EntityId[]
131140
selectEntities: (state: V) => Dictionary<T>
@@ -135,7 +144,7 @@ export interface EntitySelectors<T, V> {
135144
}
136145

137146
/**
138-
* @alpha
147+
* @public
139148
*/
140149
export interface EntityAdapter<T> extends EntityStateAdapter<T> {
141150
selectId: IdSelector<T>

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export {
8787
Dictionary,
8888
EntityState,
8989
EntityAdapter,
90+
EntitySelectors,
91+
EntityStateAdapter,
92+
EntityId,
9093
Update,
9194
IdSelector,
9295
Comparer

website/src/css/custom.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
--ifm-color-primary-light: #825ac2;
1414
--ifm-color-primary-lighter: #9b7bce;
1515
--ifm-color-primary-lightest: #b49cda;
16+
1617
--ifm-code-font-size: 95%;
1718
--ifm-code-border-radius: 3px;
1819
--ifm-code-background: rgba(27, 31, 35, 0.05);
1920

2021
--ifm-blockquote-color: #ecf4f9;
2122
--ifm-blockquote-color-dark: #cbddea;
23+
24+
--ifm-code-padding-vertical: 0.1rem;
25+
--ifm-code-padding-horizontal: 0.2rem;
2226

2327
--ifm-pre-background: rgb(39, 40, 34);
2428

0 commit comments

Comments
 (0)