Skip to content

Commit 75ad35f

Browse files
authored
TS dep updates (#386)
* Update createEntityAdapter type reference * Update TypeScript and Prettier to latest * Prettier reformatting
1 parent a1d5e84 commit 75ad35f

File tree

7 files changed

+49
-71
lines changed

7 files changed

+49
-71
lines changed

docs/api/createEntityAdapter.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,26 @@ If not provided, the `state.ids` array will not be sorted, and no guarantees are
9999

100100
A "entity adapter" instance. An entity adapter is a plain JS object (not a class) containing the generated reducer functions, the original provided `selectId` and `sortComparer` callbacks, a method to generate an initial "entity state" value, and functions to generate a set of globalized and non-globalized memoized selector functions for this entity type.
101101

102-
The adapter instance will include the following methods (some TypeScript overloads removed for space):
102+
The adapter instance will include the following methods (additional referenced TypeScript types included):
103103

104104
```ts
105105
export type EntityId = number | string
106106

107-
export type Comparer<T> = (a: T, b: T) => EntityId
107+
export type Comparer<T> = (a: T, b: T) => number
108+
108109
export type IdSelector<T> = (model: T) => EntityId
109110

110111
export interface DictionaryNum<T> {
111112
[id: number]: T | undefined
112113
}
113-
export abstract class Dictionary<T> implements DictionaryNum<T> {
114+
115+
export interface Dictionary<T> extends DictionaryNum<T> {
114116
[id: string]: T | undefined
115117
}
116118

117119
export type Update<T> = { id: EntityId; changes: Partial<T> }
118-
export type EntityMap<T> = (entity: T) => T
119120

120-
export type TypeOrPayloadAction<T> = T | PayloadAction<T>
121+
export type EntityMap<T> = (entity: T) => T
121122

122123
export interface EntityState<T> {
123124
ids: EntityId[]
@@ -130,54 +131,49 @@ export interface EntityDefinition<T> {
130131
}
131132

132133
export interface EntityStateAdapter<T> {
133-
addOne<S extends EntityState<T>>(state: S, entity: TypeOrPayloadAction<T>): S
134+
addOne<S extends EntityState<T>>(state: S, entity: T): S
135+
addOne<S extends EntityState<T>>(state: S, action: PayloadAction<T>): S
134136

135-
addMany<S extends EntityState<T>>(
136-
state: S,
137-
entities: TypeOrPayloadAction<T[]>
138-
): S
137+
addMany<S extends EntityState<T>>(state: S, entities: T[]): S
138+
addMany<S extends EntityState<T>>(state: S, entities: PayloadAction<T[]>): S
139139

140-
setAll<S extends EntityState<T>>(
141-
state: S,
142-
entities: TypeOrPayloadAction<T[]>
143-
): S
140+
setAll<S extends EntityState<T>>(state: S, entities: T[]): S
141+
setAll<S extends EntityState<T>>(state: S, entities: PayloadAction<T[]>): S
144142

145-
removeOne<S extends EntityState<T>>(
146-
state: S,
147-
key: TypeOrPayloadAction<EntityId>
148-
): S
143+
removeOne<S extends EntityState<T>>(state: S, key: EntityId): S
144+
removeOne<S extends EntityState<T>>(state: S, key: PayloadAction<EntityId>): S
149145

146+
removeMany<S extends EntityState<T>>(state: S, keys: EntityId[]): S
150147
removeMany<S extends EntityState<T>>(
151148
state: S,
152-
keys: TypeOrPayloadAction<EntityId[]>
149+
keys: PayloadAction<EntityId[]>
153150
): S
154151

155152
removeAll<S extends EntityState<T>>(state: S): S
156153

154+
updateOne<S extends EntityState<T>>(state: S, update: Update<T>): S
157155
updateOne<S extends EntityState<T>>(
158156
state: S,
159-
update: TypeOrPayloadAction<Update<T>>
157+
update: PayloadAction<Update<T>>
160158
): S
161159

160+
updateMany<S extends EntityState<T>>(state: S, updates: Update<T>[]): S
162161
updateMany<S extends EntityState<T>>(
163162
state: S,
164-
updates: TypeOrPayloadAction<Update<T>[]>
163+
updates: PayloadAction<Update<T>[]>
165164
): S
166165

167-
upsertOne<S extends EntityState<T>>(
168-
state: S,
169-
entity: TypeOrPayloadAction<T>
170-
): S
166+
upsertOne<S extends EntityState<T>>(state: S, entity: T): S
167+
upsertOne<S extends EntityState<T>>(state: S, entity: PayloadAction<T>): S
171168

169+
upsertMany<S extends EntityState<T>>(state: S, entities: T[]): S
172170
upsertMany<S extends EntityState<T>>(
173171
state: S,
174-
entities: TypeOrPayloadAction<T[]>
172+
entities: PayloadAction<T[]>
175173
): S
176174

177-
map<S extends EntityState<T>>(
178-
state: S,
179-
map: TypeOrPayloadAction<EntityMap<T>>
180-
): S
175+
map<S extends EntityState<T>>(state: S, map: EntityMap<T>): S
176+
map<S extends EntityState<T>>(state: S, map: PayloadAction<EntityMap<T>>): S
181177
}
182178

183179
export interface EntitySelectors<T, V> {
@@ -369,7 +365,10 @@ console.log(store.getState().books)
369365
// {ids: ["a"], entities: {a: {id: "a", title: "First (altered)"}}, loading: 'pending' }
370366

371367
store.dispatch(
372-
booksReceived([{ id: 'b', title: 'Book 3' }, { id: 'c', title: 'Book 2' }])
368+
booksReceived([
369+
{ id: 'b', title: 'Book 3' },
370+
{ id: 'c', title: 'Book 2' }
371+
])
373372
)
374373

375374
console.log(booksSelectors.selectIds(store.getState()))

docs/tutorials/intermediate-tutorial.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,7 @@ const AddTodo = ({ addTodo }) => {
430430
)
431431
}
432432

433-
export default connect(
434-
null,
435-
mapDispatch
436-
)(AddTodo)
433+
export default connect(null, mapDispatch)(AddTodo)
437434
```
438435
439436
We start by importing the correct `addTodo` action creator from our todos slice.

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@
3232
"eslint-config-react-app": "^5.0.1",
3333
"invariant": "^2.2.4",
3434
"json-stringify-safe": "^5.0.1",
35-
"prettier": "^1.18.2",
35+
"prettier": "^1.19.1",
3636
"react": "^16.8.6",
3737
"rollup-plugin-strip-code": "^0.2.6",
3838
"tsdx": "^0.11.0",
3939
"tslib": "^1.10.0",
40-
"typescript": "^3.6.3",
40+
"typescript": "^3.8.2",
4141
"typings-tester": "^0.3.2"
4242
},
4343
"scripts": {
4444
"build-ci": "tsdx build --format cjs,esm,umd --name redux-toolkit && api-extractor run",
4545
"build": "tsdx build --format cjs,esm,umd --name redux-toolkit && api-extractor run --local",
4646
"dev": "tsdx watch --format cjs,esm,umd",
47-
"format": "prettier --write \"src/*.ts\" \"**/*.md\"",
48-
"format:check": "prettier --list-different \"src/*.ts\" \"docs/*/**.md\"",
47+
"format": "prettier --write \"src/**/*.ts\" \"**/*.md\"",
48+
"format:check": "prettier --list-different \"src/**/*.ts\" \"docs/*/**.md\"",
4949
"lint": "tsdx lint src",
5050
"prepare": "npm run lint && npm run format:check && npm test && npm run build-ci",
5151
"test": "tsdx test"

src/entities/state_selectors.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ describe('Entity State Selectors', () => {
9696
const singleEntity: Selector<
9797
EntityState<BookModel>,
9898
BookModel | undefined
99-
> = createSelector(
100-
selectors.selectEntities,
101-
entities => entities[0]
102-
)
99+
> = createSelector(selectors.selectEntities, entities => entities[0])
103100
})
104101

105102
it('should create a selector for selecting the list of models', () => {

src/entities/state_selectors.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ export function createSelectorsFactory<T>() {
1818
ids.map((id: any) => (entities as any)[id])
1919
)
2020

21-
const selectTotal = createSelector(
22-
selectIds,
23-
ids => ids.length
24-
)
21+
const selectTotal = createSelector(selectIds, ids => ids.length)
2522

2623
if (!selectState) {
2724
return {
@@ -33,22 +30,10 @@ export function createSelectorsFactory<T>() {
3330
}
3431

3532
return {
36-
selectIds: createSelector(
37-
selectState,
38-
selectIds
39-
),
40-
selectEntities: createSelector(
41-
selectState,
42-
selectEntities
43-
),
44-
selectAll: createSelector(
45-
selectState,
46-
selectAll
47-
),
48-
selectTotal: createSelector(
49-
selectState,
50-
selectTotal
51-
)
33+
selectIds: createSelector(selectState, selectIds),
34+
selectEntities: createSelector(selectState, selectEntities),
35+
selectAll: createSelector(selectState, selectAll),
36+
selectTotal: createSelector(selectState, selectTotal)
5237
}
5338
}
5439

src/tsHelpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export type DispatchForMiddlewares<M> = M extends ReadonlyArray<any>
8585
* Convert a Union type `(A|B)` to and intersecion type `(A&B)`
8686
*/
8787
type UnionToIntersection<U> = (U extends any
88-
? (k: U) => void
89-
: never) extends ((k: infer I) => void)
88+
? (k: U) => void
89+
: never) extends (k: infer I) => void
9090
? I
9191
: never
9292

0 commit comments

Comments
 (0)