Skip to content

Type inference problem in generic subtypes of index signature types #12744

Closed
@kazimuth

Description

@kazimuth

TypeScript Version: 2.1.4

Code

// Type with index signature
type StringToNumber = {[id: string]: number};

// This works!
type ShouldBeNumber = StringToNumber[keyof StringToNumber];
let x: ShouldBeNumber = <number>0;
let y: number = x;

// This doesn't work.
class Pred<X extends StringToNumber> {
  test(value: X[keyof X]) {
    // error TS2322: type 'X[keyof X]' is not assignable to type 'number'
    let slot: number = value;

    // error TS2352: Type 'X[keyof X]' cannot be converted to type 'number'.
    let slot2 = <number> value;
  }
}

Expected behavior:
If I understand the spec correctly, all of X's properties must be subtypes of their corresponding properties in StringToNumber. All of StringToNumber's properties are of type number, so X[keyof X] should be a subtype of / assignable to number.

Actual behavior:
As far as I can tell, no relationship is inferred between the types X[keyof X] and number.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions