Object.entries behaves differently for interfaces and types
See original GitHub issueBug 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:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >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
You mean
[string, any][]
?Ah, that’s at least a puzzle piece that explains it.