Skip to content

null checks are ignored in "unknown" narrowing #37658

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

Closed
OmgImAlexis opened this issue Mar 28, 2020 · 5 comments
Closed

null checks are ignored in "unknown" narrowing #37658

OmgImAlexis opened this issue Mar 28, 2020 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@OmgImAlexis
Copy link

TypeScript Version: 3.8.3 Nightly playground

Search Terms:

Code

const func = (value: unknown) => {
    const results = [];

    // 1
    if (typeof value === 'object' && value !== null) {
        results.push(1); // (parameter) value: object
    }
    // 2
    if (value !== null && typeof value === 'object') {
        results.push(2); // (parameter) value: object | null
    }
    // 3
    if (typeof value === 'object') {
        if (value !== null) {
            results.push(3); // (parameter) value: object
        }
    }
    // 4
    if (value !== null) {
        if (typeof value === 'object') {
            results.push(4); // (parameter) value: object | null
        }
    }
    // 5
    results.push(5); // (parameter) value: unknown
  
    return results;
};

console.log(func({})); // [1, 2, 3, 4, 5]
console.log(func(null)); // [5]

Expected behavior:
2 and 4 shouldn't have null as expected type.

Actual behavior:
null checks are being ignored depending on the order they're in.

@nmain
Copy link

nmain commented Mar 28, 2020

The core problem here is #4196. The compiler has no way to express a type of Exclude<unknown, null> so the null check coming first does nothing.

However it seems 2) might be fixable as #36709 was fixed.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 1, 2020
@RyanCavanaugh
Copy link
Member

@nmain is correct. We don't have any mechanism to produce a useful narrowing for unknown !== null, and CFA is naturally order-dependent.

@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.

@OmgImAlexis
Copy link
Author

It hasn’t been addressed if this has been fixed by #37134 or not. Should this still be closed?

@LeonardDrs
Copy link

LeonardDrs commented Jan 5, 2021

Hi, I was about to post the same issue until I stumbled upon this one.
I can confirm that this is not fixed.

I understand that the first null check does currently nothing considering the narrowing and CFA order.
Is that something that we would be able to achieve with #29317?

Cheers

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

No branches or pull requests

5 participants