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.

Object.entries behaves differently for interfaces and types

See original GitHub issue

Bug Report

When using types, Object.entries returns a union of possible values as value, while a similar interface, it returns any

🔎 Search Terms

Object.entries union any

🕗 Version & Regression Information

This seems to go back at least until TS 4, I didn’t test further.

⏯ Playground Link

Playground link with relevant code

💻 Code

// explicit type has union as value type
{
    type Foo = {
        key: "value",
        anotherKey: number
    }

    let x: Foo = { key: "value", anotherKey: 3 }

    //const entries: [string, number | "value"][]
    const entries = Object.entries(x)
}

// explicit interface has `any` as value type
{
    interface Foo {
        key: "value",
        anotherKey: number
    }

    let x: Foo = { key: "value", anotherKey: 3 }

    //const entries: [string, any][]
    const entries = Object.entries(x)
}

🙁 Actual behavior

Object.entries behaves differently for types and interfaces

🙂 Expected behavior

Object.entries behaves the same for types and interfaces.

I am aware that the general argument is “interfaces could be extended” - but pretty much the same goes for a type here as well, right? Is this an oversight or intended behaviour?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Josh-Cenacommented, Jul 12, 2022

Object.entries() should really always return any or unknown.

You mean [string, any][]?

0reactions
phryneascommented, Jul 12, 2022

Ah, that’s at least a puzzle piece that explains it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Interfaces - TypeScript
Interfaces are capable of describing the wide range of shapes that JavaScript objects can take. In addition to describing an object with properties,...
Read more >
Interfaces vs Types in TypeScript - Stack Overflow
An object in JavaScript is a key/value map, and an "object type" is typescript's way of typing those key/value maps. Both interface and...
Read more >
Types vs. interfaces in TypeScript - LogRocket Blog
To me type aliases are more strict, so it makes more sense for me to use that by default – even for objects....
Read more >
Interfaces in Typescript: What are they and how to use them?
Interface As Class Types ... A class creates a blueprint for how an object should appear and behave and then implements that blueprint...
Read more >
TypeScript Interface Tutorial With Examples
We will also compare TypeScript Type vs Interface. ... Given below is an example code showing another object with different properties
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