Closed
Description
TypeScript Version: 2.9, 3.0.0-dev.20180602
Search Terms:
Generic key mapped object implicit any destructure
Code
function foo<T extends string>(key: T, obj: { [_ in T]: number }) {
const { [key]: bar } = obj; // Element implicitly has an 'any' type because type '{ [_ in T]: number; }' has no index signature.
bar; // bar : any
// Note: this does work:
const lorem = obj[key];
}
With compiler option noImplicitAny
.
Expected behavior:
No error, the definitions for bar
and lorem
are treated as equal. This used to work in TS 2.8. The type of bar
is number
.
Actual behavior:
Error on the definition of bar
:
Element implicitly has an 'any' type because type '{ [_ in T]: number; }' has no index signature.
The type of bar
is any
.
Notice that the type of bar
is any. Set noImplicitAny to see the error.
Related Issues: