- Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
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
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug