Inference for T[K] to U[Y] was supposedly introduced in #18391, however looking over the code, this case in inferTypes in checker.ts:
elseif(source.flags&TypeFlags.IndexedAccess&&target.flags&TypeFlags.IndexedAccess){inferFromTypes((<IndexedAccessType>source).objectType,(<IndexedAccessType>target).objectType);inferFromTypes((<IndexedAccessType>source).indexType,(<IndexedAccessType>target).indexType);}is unreachable code.
This is because the first if statement in the if-else if chain is
if(target.flags&TypeFlags.TypeVariable){which is true when the target is TypeFlags.IndexedAccess, as TypeFlags.TypeVariable = TypeFlags.TypeParameter | TypeFlags.IndexedAccess.
TypeScript Version: 2.6.1-insiders.20171019 (current playground)
Code
declarefunctionget<U,YextendskeyofU>(x: U,y: Y): U[Y];declarefunctionfind<T,KextendskeyofT>(o: T[K]): [T,K];functionimpl<A,BextendskeyofA>(a: A,b: B){constitem=get(a,b);returnfind(item);}Expected behavior:
impl has return type [A, B]
Actual behavior:
impl has return type [{}, never]
Inference for
T[K]toU[Y]was supposedly introduced in #18391, however looking over the code, this case ininferTypesinchecker.ts:is unreachable code.
This is because the first
ifstatement in theif-else ifchain iswhich is true when the
targetisTypeFlags.IndexedAccess, asTypeFlags.TypeVariable = TypeFlags.TypeParameter | TypeFlags.IndexedAccess.TypeScript Version: 2.6.1-insiders.20171019 (current playground)
Code
Expected behavior:
implhas return type[A, B]Actual behavior:
implhas return type[{}, never]