microsoft/TypeScript

Object.keys incorrect return type

Connormiha opened this issue · 2 comments

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:

Code

const a = { a: 'foo' };

Object.keys(a).forEach((key) => {
    console.log(a[key]);
});

Expected behavior:
No error

Actual behavior:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ a: string; }'.
  No index signature with a parameter of type 'string' was found on type '{ a: string; }'.

Playground Link:
https://www.typescriptlang.org/play/?ssl=9&ssc=4&pln=6&pc=1#code/MYewdgzgLgBAhjAvDA3vAXDA5AMxCLGAXwG4BYAKEoHkAjAKwFNgoA6Aa0YE8IAKOAJSs8AJwCicYAAtevTlwFIAfKkox1MUJBAAbRqx0gA5vwDa8gLoDyFItcpA

Related Issues:

There are good reasons for the current types.

Read here for the explanation : So answer

Search Terms: