question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
therealgillescommented, Mar 19, 2022

@MartinJohns: I will give it a go next week. I admit I am still fairly new to TS but I’ll try to gather examples from the filed issues.

1reaction
fatcerberuscommented, Mar 19, 2022

This isn’t really about TS2532 specifically but about type narrowing in general–TS doesn’t treat any check involving obj[key] as a type guard unless key is a literal. This does come up often though, see e.g. #48227 or #48203, and is a known limitation of control-flow-based type narrowing so it should probably be mentioned in the FAQ at this point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I solve the error 'TS2532: Object is possibly ...
Typescript is saying that change or data is possibly undefined (depending on ... where you know a particular object property cannot be null, ......
Read more >
Object.defineProperty() - JavaScript - MDN Web Docs
If an attribute is absent from the new descriptor, the old descriptor attribute's value is kept (it won't be implicitly re-defined to undefined...
Read more >
Documentation - Object Types - TypeScript
How TypeScript describes the shapes of JavaScript objects. ... TypeScript will tell us they're potentially undefined . ... We can read from 'obj.prop'....
Read more >
Optional chaining '?.' - The Modern JavaScript Tutorial
The optional chaining ?. is a safe way to access nested object ... obj?.prop – returns obj.prop if obj exists, otherwise undefined ....
Read more >
Object Types - JavaScript. Flow
In general, we recommend using exact object types whenever possible. ... @flow var obj = {}; if (Math.random()) obj.prop = true; else obj.prop...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found