Empty type inferred by Object.entries
See original GitHub issueTypeScript Version: 2.7.1
Search Terms: Object entries empty TS2365
Code
let o: any = {x: 5};
let p = Object.entries(o).map(([k, v]) => v + 1);
Compile command: tsc --lib es2017,es2017.object a.ts
Expected behavior:
This should compile without any error, assuming v: any
.
This was the case for Typescript 2.6.2 at least.
Actual behavior: (3,43): error TS2365: Operator ‘+’ cannot be applied to types ‘{}’ and ‘1’.
Following codes compile without errors:
let o: object = {x: 5};
let p = Object.entries(o).map(([k, v]) => v + 1);
let o: any = {x: 5};
let p = Object.entries(o).map(([k, v]: [any, any]) => v + 1);
Issue Analytics
- State:
- Created 6 years ago
- Reactions:13
- Comments:16 (8 by maintainers)
Top Results From Across the Web
Preserve Type when using Object.entries - Stack Overflow
For example, if I have a value nameHaver of type {name: string} , I know it has a name property, but I don't...
Read more >More powerful type definitions for Object.entries()
Calling typedEntries() function, which wraps Object.entries() , returns an array of entry types inferred according to the argument type.
Read more >Object.entries() - JavaScript - MDN Web Docs
The Object.entries() static method returns an array of a given object's own enumerable string-keyed property key-value pairs.
Read more >Documentation - More on Functions - TypeScript
The function type (string) => void means “a function with a parameter named string of type ... The type was inferred - chosen...
Read more >Object.entries on Record type is incorrect and just a string ...
Basically, the object is not guaranteed not to have other extra properties than expected. Edit: here's a more detailed answer from TS's lead ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a work-around, and building on the workaround for
Object.keys()
in this comment, I’ve just made one forObject.entries()
too. Obviously only safe to use with objects that you have typed and know that these types can be relied upon.edit: Looking at it more, I’m not sure
Extract
is required / desirable here. Seems to work fine without.