Skip to content

Object#entries and Object#values doesn't consider optional properties as potentially undefined Β #52045

Closed as not planned
@eliasm307

Description

@eliasm307

Bug Report

πŸ”Ž Search Terms

  • Object entries optional properties
  • Object values optional properties

πŸ•— Version & Regression Information

  • This changed between versions 4.2.3 and 4.3.5

⏯ Playground Link

πŸ’» Code

// without tsconfig.json#exactOptionalPropertyTypes enabled, optional properties can exist with `undefined`
const obj: {foo?: string, bar?: string} = {foo: undefined, bar: undefined} 

Object.entries(obj).forEach(([key, value]) => {
    const str: string = value; // value should be recognised as potentially undefined
})

Object.values(obj).forEach(value => {
    const str: string = value; // value should be recognised as potentially undefined
})

πŸ™ Actual behavior

From v4.3 it doesnt consider optional object properties as potentially undefined (with exactOptionalPropertyTypes disabled) when using Object#entries or Object#values

πŸ™‚ Expected behavior

Optional properties should mean the value when using Object#entries or Object#values is potentially undefined when exactOptionalPropertyTypes is disabled, as undefined could be assigned to the optional properties.

This allows TS to compile code with potential runtime issues from using optional properties as if they were always defined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions