Skip to content

3.5 regression: invalid type for index lookup using constrained generic#32017

@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 3.5.2

Search Terms: generic constraint constrained index lookup function union

Code

typeMyRecord={foo: {foo: number};bar: {bar: number};};typeTag=keyofMyRecord;typeMyUnion=MyRecord[Tag];declareconstvalues: {foo: MyRecord['foo'];bar: MyRecord['bar'];};declareconstfns: {foo: ()=>MyRecord['foo'];bar: ()=>MyRecord['bar'];};declareconstpredicates: {foo: (value: MyUnion)=>value is MyRecord['foo'];bar: (value: MyUnion)=>value is MyRecord['bar'];};<TextendsTag>(tag: T)=>{// 1.{constvalue=values[tag];typeExpectedType=MyRecord[T];// Expected: no error// 3.4 and 3.5: works as expectedconsttest: ExpectedType=value;}// 2.{declareconstvalue: MyUnion;// Expected: error// 3.4: no error// 3.5: works as expectedconstdesired: MyRecord[T]=value;}// 3.{constfn=fns[tag];typeExpectedType=()=>MyRecord[T];// Expected: no error// 3.4: works as expected// 3.5: unexpected errorconsttest: ExpectedType=fn;}// 4. (this is our real world use case){declareconstvalue: MyUnion;constpredicate=predicates[tag];if(predicate(value)){// Expected: no error// 3.4: works as error// 3.5: unexpected errorconstdesired: MyRecord[T]=value;}}};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions