question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty type inferred by Object.entries

See original GitHub issue

TypeScript 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:open
  • Created 6 years ago
  • Reactions:13
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

24reactions
liamnesscommented, Apr 4, 2019

As a work-around, and building on the workaround for Object.keys() in this comment, I’ve just made one for Object.entries() too. Obviously only safe to use with objects that you have typed and know that these types can be relied upon.

export const entries = Object.entries as <T>(
  o: T
) => [Extract<keyof T, string>, T[keyof T]][]

edit: Looking at it more, I’m not sure Extract is required / desirable here. Seems to work fine without.

2reactions
whbjzzwjxqcommented, Dec 7, 2019
e.g.
type item = 'node' | 'link' | 'media'                
let typeDict: Record<item, string[]> = {
                    node: [],
                    link: [],
                    media: []
                };
Object.entries(typeDict).map(([_type, labels]) => {
           **_type inferred as string but not item here**
})
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found