Skip to content

Commit 24863a1

Browse files
committed
Simplify entity ID type definitions
1 parent 11ed168 commit 24863a1

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/entities/models.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { PayloadAction } from '../createAction'
22

3-
export type ComparerStr<T> = (a: T, b: T) => string
4-
export type ComparerNum<T> = (a: T, b: T) => number
5-
63
/**
74
* @alpha
85
*/
9-
export type Comparer<T> = ComparerNum<T> | ComparerStr<T>
6+
export type EntityId = number | string
107

11-
export type IdSelectorStr<T> = (model: T) => string
12-
export type IdSelectorNum<T> = (model: T) => number
8+
/**
9+
* @alpha
10+
*/
11+
export type Comparer<T> = (a: T, b: T) => EntityId
1312

1413
/**
1514
* @alpha
1615
*/
17-
export type IdSelector<T> = IdSelectorStr<T> | IdSelectorNum<T>
16+
export type IdSelector<T> = (model: T) => EntityId
1817

18+
/**
19+
* @alpha
20+
*/
1921
export interface DictionaryNum<T> {
2022
[id: number]: T | undefined
2123
}
@@ -47,13 +49,16 @@ export type Update<T> = UpdateStr<T> | UpdateNum<T>
4749
*/
4850
export type EntityMap<T> = (entity: T) => T
4951

52+
/**
53+
* @alpha
54+
*/
5055
export type TypeOrPayloadAction<T> = T | PayloadAction<T>
5156

5257
/**
5358
* @alpha
5459
*/
5560
export interface EntityState<T> {
56-
ids: string[] | number[]
61+
ids: EntityId[]
5762
entities: Dictionary<T>
5863
}
5964

src/entities/unsorted_state_adapter.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
5757
})
5858

5959
if (didMutate) {
60-
// Work around TS not letting us call array methods if it's not a single known type
61-
const ids = state.ids as string[]
62-
state.ids = ids.filter((id: any) => id in state.entities)
60+
state.ids = state.ids.filter(id => id in state.entities)
6361
}
6462
}
6563

0 commit comments

Comments
 (0)