-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow the user to refine the Typescript type for the EntityId in createEntityAdapter #955
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
Comments
Yeah, that would definitely add complexity. Also, we're already looking at adding a second generic param to the adapter over in #948 , for defining fields in Given that in most cases people shouldn't really be needing to grab the IDs array by itself, I'm not sure there's sufficient benefit to making that overridable. If you'd like to play around with the idea, please go ahead, but I think there'd need to be some some consideration of whether it's really useful. |
Ha, nice to see you from StackOverflow 👋 I agree that it would be nice, but the timing on this is very unfortunate as Mark is really bulldozing through these files right now. I'd suggest that we wait until that has calmed down and then revisit the whole thing? |
(also, given that you seem to be doing a lot of answering questions regarding RTK, you ought to drop by Reactiflux and say hi to us over in the |
Sounds good. I don't think it's hard to do, it's just a question of whether having an extra generic everywhere is worth it. The only issue I'm running into is on the lines that have
This was before being inferred as |
Just to add to the discussion. It feels to me that it might be useful to be able to define the In general, we probably know what's the type of the id of a specific entity. If for example it's
interface UserEntity {
id: string;
}
const selectors = usersAdapter.getSelectors();
const UsersList: React.FunctionComponent<ListProps> = () => {
const userIds = useSelector(selectors.selectIds);
return (
<ul>
{userIds.map((userId) => (
<UserListItem userId={userId} />
{/* ^^^^^^
Type 'EntityId' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.ts(2322)
*/}
))}
</ul>
);
}
interface UserListItemProps {
userId: UserEntity["id"];
}
const UserListItem: React.FunctionComponent<UserListItemProps> = (props) => {
const { userId } = props;
const userEntity = useSelector((state) => selectors.selectById(state, userId));
// ...
} |
I am running into a similar issue as @100terres pointed out. Is there a common way to avoid this? I would really like to avoid having to coerce the |
Still needs attention. Proper TypeScript support required. |
Inspired by this StackOverflow question, I think it would be a good feature if it was possible for an
EntityAdapter
to have a generic id type whichextends EntityId
but could be juststring
or justnumber
. This would allow theEntitySelectors
to return the correct id type fromselectIds
and require the right id type forselectById
.Most of the types in models.ts would need a second generic type parameter which is optional like
<I extends EntityId = EntityId>
.I'm going to play with the code and see if I can get that working. Unless there's some reason that this isn't doable or isn't desirable. It would definitely add a level of complexity to the types.
The text was updated successfully, but these errors were encountered: