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

strictNullChecks false positives for non-null object properties #60847

Open
runny-yolk opened this issue Dec 23, 2024 · 1 comment
Open

strictNullChecks false positives for non-null object properties #60847

runny-yolk opened this issue Dec 23, 2024 · 1 comment

Comments

@runny-yolk
Copy link

runny-yolk commented Dec 23, 2024

πŸ”Ž Search Terms

possibly null undefined object property

πŸ•— Version & Regression Information

Originally detected with 5.7.2, but also confirmed in v5.8.0-dev.20241223 via the playground.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241223#code/DYUwLgBAHgXBB2BXAtgIxAJwgHwY4wEAvBAKwDcAUKJAJ7HQQDUZV14EA9qgFZwDesPGkw48BAL4NBcUhKo0IALwbceAOijNWEAPS6ImDJwxwA5Gs1mIASwDOEAA6c7dm6mD0zSAmfUAKAEYADgAGABYAdgBKIA

πŸ’» Code

let x: number | null = 5;
let y = x + 5; // no problem

let obj: {x: number | null} = {x: 5};
let z = obj.x + 5; // error: 'obj.x' is possibly 'null'. (18047)

πŸ™ Actual behavior

When nullable object properties have been assigned a non-null value, TS still considers them "possibly null", even though it does not for equivalently typed variables.

πŸ™‚ Expected behavior

No error.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Dec 23, 2024

unions narrow, objects with union-typed properties do not; you'd need #42384 for this to work as desired. If you made obj of type {x: number} | {x: null} it would also work today (since that's a union of objects).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants