- Notifications
You must be signed in to change notification settings - Fork 13k
Closed as not planned
Closed as not planned
Copy link
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
🔎 Search Terms
false positive error after null checking with typed boolean variable instead non typed variable
strictNullChecks false positive error
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about 2.0.3 to 5.4.0-dev.20231112
- I was unable to test this on prior versions because of insufficient knowledge with older typescript versions
⏯ Playground Link
💻 Code
Example code
Common base
The function simply returns a type or a null:
tsconfig.json
{ "compilerOptions": { "strictNullChecks": true } }
index.ts
type Example = { field: string } function test(): Example | null { return { field: "Hello, world!" }; } // ...
Code with the Issue
The following code doesn't compile. It can't recognize the null checking of the variable
index.ts
// ... const variable = test(); // Can be literally any type, any, boolean, Boolean const isVariableNull: any = !variable; if (!isVariableNull) { // Property 'field' does not exist on type 'Example | null'.(2339) const { field } = variable; }
Equivalent working code
The following code is equivalent to the previous one and correctly compiles.
index.ts
// ... const variable = test(); // Mustn't be typed const isVariableNull = !variable; if (!isVariableNull) { // Property 'field' can be normally used here const { field } = variable; }
🙁 Actual behavior
TypeScript can't recognize null checking of variables when the value is assigned to a typed variable and later used to do the null checking
🙂 Expected behavior
TypeScript should recognize the null checking and compile the code normally (Like it's equivalent snippet)
Additional information about the issue
No response
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug