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
Copy file name to clipboardExpand all lines: docs/usage/usage-with-typescript.md
+31-5
Original file line number
Diff line number
Diff line change
@@ -724,20 +724,46 @@ Import and use that pre-typed `createAppAsyncThunk` instead of the original, and
724
724
725
725
## `createEntityAdapter`
726
726
727
-
Typing `createEntityAdapter`only requires you to specify the entity type as the single generic argument.
727
+
Usage of `createEntityAdapter`with Typescript varies based on whether your entities are normalized by an `id` property, or whether a custom `selectId` is needed.
728
728
729
-
The example from the `createEntityAdapter`documentation would look like this in TypeScript:
729
+
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:
730
730
731
731
```ts
732
732
interfaceBook {
733
-
bookId:number
733
+
id:number
734
734
title:string
735
-
// ...
736
735
}
737
736
737
+
// no selectId needed here, as the entity has an `id` property we can default to
738
738
// highlight-next-line
739
739
const booksAdapter =createEntityAdapter<Book>({
740
-
selectId: (book) =>book.bookId,
740
+
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.
756
+
757
+
```ts
758
+
interfaceBook {
759
+
bookId:number
760
+
title:string
761
+
// ...
762
+
}
763
+
764
+
const booksAdapter =createEntityAdapter({
765
+
// highlight-next-line
766
+
selectId: (book:Book) =>book.bookId,
741
767
sortComparer: (a, b) =>a.title.localeCompare(b.title),
0 commit comments