Skip to content

Why are Object.create(null) and {} treated differently regarding undefined-exclusion? #52698

@benjie

Description

@benjie

Bug Report

🔎 Search Terms

  • Object.create(null)
  • POJO
  • existence
  • assertion
  • undefined

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Object.create(null) but there doesn't seem to be any.

⏯ Playground Link

Playground link with relevant code

💻 Code

{
  const a: {b?: {c?:number}} = {};

  if (!a.b) {
    a.b = {};
  }
  a.b.c = 1; // Fine
}
{
  const a: {b?: {c?:number}} = {};

  if (!a.b) {
    a.b = Object.create(null);
  }
  a.b.c = 1; // TypeScript error: 'a.b' is possibly 'undefined'
}

🙁 Actual behavior

In the first block, everything is fine. In the second block, the a.b.c = 1 line issues the TypeScript error 'a.b' is possibly 'undefined'.

🙂 Expected behavior

Just as line 5 implies a.b exists, line 13 should do the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions