Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
benjie opened this issue Feb 9, 2023 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@benjie
Copy link

benjie commented Feb 9, 2023

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.

@MartinJohns
Copy link
Contributor

MartinJohns commented Feb 9, 2023

Because Object.create(null) returns any. There's no dedicated overload for the argument null that would return {}, and it's likely not a very common use case.

@benjie
Copy link
Author

benjie commented Feb 9, 2023

Indeed, but why is it not treated as object rather than any?

@benjie
Copy link
Author

benjie commented Feb 9, 2023

it's likely not a very common use case.

When trying to avoid prototype pollution and similar issues, dealing with null-prototype objects reduces risks significantly. Any time I ever have Record<string, any> I always use Object.create(null) for it, for safety. And in performance sensitive code paths, if I know I have only used null-prototype objects then I do not need to perform Object.hasOwnProperty checks.

@MartinJohns
Copy link
Contributor

Because object would not allow you to access any properties, and I guess the more common use case for this dynamic/"unsafe" object creation is to access the properties afterwards.

@benjie
Copy link
Author

benjie commented Feb 9, 2023

Maybe it should default to Record<any, any> or Record<number | string | symbol, any> then?

The current behavior seems suboptimal, and having to cast it when its type should already be implied via what it's being assigned to is undesirable. E.g. to change from POJO to null prototype object without affecting surrounding code we have to do quite a lot of typing:

// With POJO:
a.b = {};
// With null prototype (so verbose!):
a.b = Object.create(null) as object; // or Record<any, any>

Related issue from 2015: #3865

@MartinJohns
Copy link
Contributor

I guess you could create an issue for that: Lib change issue template

In the meantime you can simply use declaration merging to create an overload for your own code base. Just create a .d.ts file with this content:

interface ObjectConstructor {
    create(n: null): {}
}

@fatcerberus
Copy link

Keep in mind that typescript will still treat the object (in the type system) as if it derives from Object - there is currently no concept of a null-prototype object type.

@NN---
Copy link

NN--- commented Feb 11, 2023

#39882

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 14, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants