Skip to content

Unexpected narrowing of optional fields via Object.entriesΒ #61650

@defunctzombie

Description

@defunctzombie

πŸ”Ž 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

function fn(foo: { bar?: number | undefined }) {
  // typescript thinks _arr_ is `[string, number][]` when actually it is `[string, number | undefined][]`
  const arr = 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions