You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionfn(foo: {bar?: number|undefined}){// typescript thinks _arr_ is `[string, number][]` when actually it is `[string, number | undefined][]`constarr=Object.entries(foo);console.log(arr);}fn({bar: undefined});
π Actual behavior
The type from Object.entries is narrowed
π Expected behavior
Object.entries(foo) produces a type output that is [string, number | undefined][] when foo has type { bar?: number | undefined }
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
defunctzombie
changed the title
Unexpected narrowing of optional fields when using Object.entries
Unexpected narrowing of optional fields via Object.entries
May 3, 2025
@MartinJohns could you elaborate on why that option would change the behavior here? From my perspective Object.entries has dropped the notion that one of the fields could be set to undefined and that would be a valid value for it. I understand the idea that it could be omitted entirely - but not being present is different than being set to undefined.
Without exactOptionalPropertyTypes there is no distinction between {prop?: someType} and {prop?: someType | undefined}; the latter behaves as if the former.
not being present is different than being set to undefined
That is the distinction that exactOptionalPropertyTypes allows for.
π Search Terms
object.entires incorrect type narrowed
π Version & Regression Information
I can experience this on v5.8.3. I also checked back to 5.0.4 on the playground and see the same behavior
β― Playground Link
https://www.typescriptlang.org/play/?target=10&ts=5.8.3#code/GYVwdgxgLglg9mABMMAKYc4C5EG9EBGAhgE4D8OYIAtgQKYmIA+i4AJncDGHW4gL4BKPAChEiAPQTEUAJ4AHOgGcIJGPKgyAFtwDWS7TANHEAAwDaSqGrABzADSIqtBgF1zr04gDuWukiJoECIAGxDZRBhNCCIkejNLa24HJxp6RhZ2Tm5ed08xRAgEK0RSRgBeRAB5AgArOmgAOn8k5XRMQQBuAqKwJTgQukaQuFtUMq6RfhERFFRcAuISHCyuHjYprqA
π» Code
π Actual behavior
The type from
Object.entries
is narrowedπ Expected behavior
Object.entries(foo)
produces a type output that is[string, number | undefined][]
when foo has type{ bar?: number | undefined }
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: