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
669
639
670
```ts
640
671
interfaceBook {
@@ -643,8 +674,8 @@ interface Book {
643
674
// ...
644
675
}
645
676
646
-
// highlight-next-line
647
677
const booksAdapter =createEntityAdapter({
678
+
// highlight-next-line
648
679
selectId: (book:Book) =>book.bookId,
649
680
sortComparer: (a, b) =>a.title.localeCompare(b.title)
0 commit comments