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

No narrowing on condition captured by const variable #39996

Closed
amannn opened this issue Aug 11, 2020 · 4 comments · Fixed by #44730
Closed

No narrowing on condition captured by const variable #39996

amannn opened this issue Aug 11, 2020 · 4 comments · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@amannn
Copy link

amannn commented Aug 11, 2020

TypeScript Version: 4.0.0-beta

Search Terms:

  • "unnecessary null check"
  • "derived boolean null check"

Expected behavior:

When a created variable contains a null check, using that boolean should be sufficient for safe access to the variable.

Actual behavior:

TypeScript needs an additional null check.

Related Issues:

Code

export default function foo(a: {b: boolean} | undefined) {
    const isNull = a == null;

    if (isNull) {
        return;
    }

    // Object is possibly 'undefined'.
    return a.b
}
Output
export default function foo(a) {
    const isNull = a == null;
    if (isNull) {
        return;
    }
    return a.b;
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@amannn
Copy link
Author

amannn commented Aug 11, 2020

My apologies if this issue already exists, I couldn't find it with my search terms! Thanks for your help!

@MartinJohns
Copy link
Contributor

Duplicate of #12184.

@tom-sherman
Copy link

You can use a type guard instead:

function isNullish<T>(x: T | null | undefined): x is null | undefined {
    return x == null;
}

declare const x: number | undefined;

if (!isNullish(x)) {
    x // number
}

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Aug 11, 2020
@DanielRosenwasser DanielRosenwasser changed the title Derived boolean null check No narrowing on condition captured by const variable Aug 13, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants