Description
TypeScript Version: 2.8.0-dev.20180216
Search Terms:
- indexed access type
- keyof
- never
Code
type A = {};
type B = A[keyof A]; // B = any
// ^^^^^^^ // ERROR: 'never' cannot be used as an index type
Expected behavior:
A[keyof A]
is a valid indexed access type that evaluated to never
.
Actual behavior:
A[keyof A]
is treated as an error, and evaluates to any
(presumably due to compiler bailout).
Related Issues:
#11929 describes indexed access types as follows:
An indexed access type
T[K]
requiresK
to be a type that is assignable tokeyof T
[...] and yields the type of the property or properties inT
selected byK
.
In this case K=never
and T={}
, so K
is assignable to keyof T
, so T[K]
appears to be a valid type.
The union of zero property keys is never
, as tsc shows with keyof {} = never
.
So the result should be the union of zero property types, i.e. also never
.
Consequences:
In more complex generics this leads to meaningful problems, e.g. in #21988 where RequiredProps<{}>
evalutes to {[x: string]: any}
when it should be {}
.