Skip to content

obj[prop] || (obj[prop] = {}): Object is possibly 'undefined' -- Documentation? #48335

Open
@therealgilles

Description

@therealgilles

Suggestion

It would be great to have TS documentation about "Object is possibly 'undefined'. ts(2532)" common cases that most everyone will run into and be puzzled about. The TS playground below shows such an example when using code like: obj[prop] || (obj[prop] = {}) and when most everyone would expect obj[prop] not to be flagged as 'undefined'.

TS Playground: link

🔍 Search Terms

Object is possibly 'undefined'. ts(2532)
TypeScript Object property assignment
TypeScript Object OR assignment undefined

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I would like to see common cases documentation about the "Object is possibly 'undefined'. ts(2532)" error. Maybe it exists and I could not find it.

📃 Motivating Example

I see many issues filed related to ts(2532) because of the current limitations of TypeScript.

💻 Use Cases

See TS Playground link.

Metadata

Metadata

Assignees

Labels

DocsThe issue relates to how you learn TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions