-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dictionary support to createEntityAdapter many methods #444
Add dictionary support to createEntityAdapter many methods #444
Conversation
src/entities/models.ts
Outdated
addMany<S extends EntityState<T>>( | ||
state: PreventAny<S, T>, | ||
entities: PayloadAction<T[]> | ||
entities: T[] | Record<string, T> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use EntityId
instead of string
, since IDs could be numbers.
Hmm. It looks like normalizr
's types are:
{ [key:string]: { [key:string]: T } | undefined}
Do these mesh okay? Should we be using the Dictionary<T>
type instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question. { [key: string]: T}
types should play nicely with Record<EntityId, T>
for the most part being that it's the same thing without the index key signature features. I'll try experimenting some, but maybe @phryneas has an idea here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted to leave a record of where I got to. I struggled a lot getting normalizr to give me the actual types of the processed data. If we define what the ReturnType
should be, there are no issues and the types pass through as expected without errors in the reducer.
export const fetchArticle = createAsyncThunk<
{
articles: { [key: string]: Article };
users: { [key: string]: Author };
comments: { [key: string]: Comment };
},
number,
any
>("articles/fetchArticle", async id => {
const data = await fakeAPI.articles.show(id);
// normalize the data so reducers can responded to a predictable payload, in this case: `action.payload = { users: { 1: { ... } }, articles: {}, comments: {} }`
const normalized = normalize(data, articleEntity);
return normalized.entities as any; // cast as any to avoid the type issues
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick update: the normalizr types don't infer anything at all, so this about the 'cleanest' way I can get this work:
export const fetchArticle = createAsyncThunk(
"articles/fetchArticle",
async (id: number) => {
const data = await fakeAPI.articles.show(id);
// normalize the data so reducers can responded to a predictable payload, in this case: `action.payload = { users: {}, articles: {}, comments: {} }`
const normalized = normalize<
any,
{
articles: { [key: string]: Article };
users: { [key: string]: Author };
comments: { [key: string]: Comment };
}
>(data, articleEntity);
return normalized.entities;
}
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the "Usage Guide" page, we can keep things as plain JS, so we don't have to worry about that here.
Perhaps we could make a brief note of this in the "Usage with TS" page, and possibly point to a TS sandbox.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I've already converted the sandbox mentioned in #441 to TS specifically for this code sample above. If this PR is approved, I'll update the usage docs and sandbox references there in a cleanup pass.
Looks good to me. |
Overview
This PR allows a user to pass in an object with the shape of
{ 1: { id: 1, other: 'value' }
directly to theupsertMany
,addMany
, andsetAll
methods of an entity adapter. This ultimately makes integration with a normalization library like normalizr much simpler.TodoupdateMany