You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Typing `createEntityAdapter` only requires you to specify the entity type as the single generic argument. This typically looks like:
637
+
Usage of `createEntityAdapter` with Typescript varies based on whether your entities are normalized by an `id` property, or whether a custom `selectId` is needed.
638
+
639
+
If your entities are normalized by an `id` property, `createEntityAdapter` only requires you to specify the entity type as the single generic argument. For example:
640
+
641
+
```ts
642
+
interfaceBook {
643
+
id:number
644
+
title:string
645
+
}
646
+
647
+
// no selectId needed here, as the entity has an `id` property we can default to
648
+
// highlight-next-line
649
+
const booksAdapter =createEntityAdapter<Book>({
650
+
sortComparer: (a, b) =>a.title.localeCompare(b.title)
On the other hand, if the entity needs to be normalized by a different property, we instead recommend passing a custom `selectId` function and annotating there. This allows proper inference of the ID's type, instead of having to provide it manually.
638
666
639
667
```ts
640
668
interfaceBook {
@@ -643,18 +671,15 @@ interface Book {
643
671
// ...
644
672
}
645
673
646
-
// highlight-next-line
647
674
const booksAdapter =createEntityAdapter({
675
+
// highlight-next-line
648
676
selectId: (book:Book) =>book.bookId,
649
677
sortComparer: (a, b) =>a.title.localeCompare(b.title)
0 commit comments