-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
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
export interface Entity {
id: number | string;
}
export type IdOf<E extends Entity> = E['id'];
export interface EntityState<E extends Entity> {
ids: IdOf<E>[];
entities: { [key: string]: E, [key: number]: E };
}
export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] {
const { ids, entities } = state;
return ids.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;
}
return entities[id];
}
For completeness, I'll just mention that I intend for users to create subinterfaces of Entity
and EntityState
for their domain types (like @ngrx/entity
), overriding id
to be a more restrictive type. This is why the IdOf
type is important. E.g.,
interface Task extends Entity {
id: string;
due: Date;
title: string;
}
interface TaskState extends EntityState<Task> {
// Inherits EntityState#ids as string[]
currentTask?: string;
}
Expected behavior:
The code compiles without error.
Actual behavior:
BUILD ERROR
projects/entity/src/lib/entity-state.utils.ts(13,5): error TS2322: Type '{ [key: string]: E; [key: number]: E; }[E["id"]][]' is not assignable to type 'E[]'.
Type '{ [key: string]: E; [key: number]: E; }[E["id"]]' is not assignable to type 'E'.
Type 'Entity' is not assignable to type 'E'.
projects/entity/src/lib/entity-state.utils.ts(23,5): error TS2322: Type '{ [key: string]: E; [key: number]: E; }[E["id"]]' is not assignable to type 'E | undefined'.
Type 'Entity' is not assignable to type 'E'.
Type '{ [key: string]: E; [key: number]: E; }[E["id"]]' is not assignable to type 'E'.
Type 'Entity' is not assignable to type 'E'.
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.
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