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

false positive error after null checking with typed boolean variable instead non typed variable #56369

Closed
LKodex opened this issue Nov 12, 2023 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@LKodex
Copy link

LKodex commented Nov 12, 2023

🔎 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

TypeScript: TS Playground

💻 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

@MartinJohns
Copy link
Contributor

You forgot to fill out the issue template (missing playground link).

This is working as intended. Control flow analysis of aliases expressions does not work when your variable has a type annotation, as per #44730:

Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a const variable declaration with no type annotation, [...]

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 13, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants