Repository navigation
Change in generic behavior between 3.0.1 and >=3.1.1 #27895
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Indexed Access TypesThe issue relates to accessing subtypes via index accessThe issue relates to accessing subtypes via index accessFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
Activity
mattmccutchen commented
on Oct 14, 2018 ContributorMore actionsThis was caused by #26698. The old behavior to check assignability of the indexed access type
{ [key: string]: E; [key: number]: E; }[E["id"]]toEwas to take the base constraint of both the object type and the index type, which gave{ [key: string]: E; [key: number]: E; }[string | number] = E, which worked. The new behavior takes the constraint of the object type, and if there isn't one, jumps straight to the base constraint of the whole indexed access type, so it goes toEntityand missesE.chrisbouchard commented
on Oct 14, 2018 AuthorMore actionsIs this considered a regression? Or, if this is the intended new behavior, is there a suggested workaround?
mattmccutchen commented
on Oct 14, 2018 ContributorMore actionsI'd call it a regression, but it's not my opinion that matters.
A workaround is to upcast
idtostring | number:export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] { const { ids, entities } = state; const ids2: (string | number)[] = ids; return ids2.map(id => entities[id]); } export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): E | undefined { const { ids, entities } = state; if (!ids.includes(id)) { return undefined; } const id2: string | number = id; return entities[id2]; }
Reacted by Chris Bouchard, Titian Cernicova-Dragomir and NN- addedBugA bug in TypeScriptA bug in TypeScriptDomain: Indexed Access TypesThe issue relates to accessing subtypes via index accessThe issue relates to accessing subtypes via index access
on Oct 16, 2018 - addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Apr 12, 2019 - locked as resolved and limited conversation to collaborators
on Oct 21, 2025
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Indexed Access TypesThe issue relates to accessing subtypes via index accessThe issue relates to accessing subtypes via index accessFixedA PR has been merged for this issueA PR has been merged for this issue
TypeScript Version: 3.2.0-dev.20181011
This code worked in 3.0.1, and is broken >=3.1.1.
Search Terms:
"is not assignable to type"
Code
For completeness, I'll just mention that I intend for users to create subinterfaces of
EntityandEntityStatefor their domain types (like@ngrx/entity), overridingidto be a more restrictive type. This is why theIdOftype is important. E.g.,Expected behavior:
The code compiles without error.
Actual behavior:
Playground Link:
Playground Link
Related Issues:
I did not find any that seemed similar, but I'm also unsure what the actual underlying problem is. I did ask on StackOverflow, and they suggested I ask here.