Update type definition of Object.keys#17267
Conversation
Solves need to explicitly specify type of `key` when in **Strict Mode**.
## Issue it solves
```ts
const someObj = {
a: 42,
b: 'Hello'
}
Object.keys(someObj)
.forEach(key =>
someObj[key] // ERROR: Element implicitly has an 'any' type because type ... has no index signature.
)
```
|
This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. We will now review your pull request. |
---> /src/lib@ And also, there is already a similar PR, see #17261 (only modify the type of |
|
Ok sorry for not reading, and thanks for your comment. :/ PR #17261 does not solve this issue, as it just restricts what can be passed to My PR is to link the output type of |
|
EDIT: overload does nothing in this case.
declare function keys<A>(o: A): (keyof A)[];
declare const x: object;
keys(x); //=> never[]; <------- should be string[] |
|
See - #15295, |
| * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. | ||
| */ | ||
| keys(o: any): string[]; | ||
| keys<A>(o: A): (keyof A)[]; |
There was a problem hiding this comment.
<A extends {}>
The problem with making Object.keys return keyof A is that typescript makes no guarantees that the input object has only the keys that are specified in its type. There may be valid values in the array that are not considered by the type.
|
@sandersn, can you take a look? |
|
I think the issue raised in #12253 (comment) still holds: |
Solves need to explicitly specify type of
keywhen in Strict Mode.Issue it solves