Skip to content

Allow comparing anything with null or undefined even when strict null checks are enabled #8439

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
malibuzios opened this issue May 3, 2016 · 1 comment
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@malibuzios
Copy link

malibuzios commented May 3, 2016

TypeScript Version:

1.9.0-dev.20160503

Code:

export function safeLibraryFunction1(arg: string) {
    if (arg == null)
        throw new TypeError(`Hi JS Consumer, I checked, it turns out the argument you 
                             gave was a null or undefined`);

    if (typeof arg !== "string")
        throw new TypeError(`Hi JS Consumer, I checked, the argument you gave me wasn't a string.. 
                             seems like something went wrong in your program?..`);
    // ..
}

interface SomeType {
    a: number;
}

export function safeLibraryFunction2(arg: SomeType) {
    if (arg === undefined)
        throw new TypeError(`Hi TS Consumer, it turns out the argument was undefined.
                             I don't assume you're using strict nullability checks,
                             I know that could be a bit difficult thing to introduce
                             or you haven't even heard about it 
                             so I also check at runtime :)`);
    if (arg === null)
        throw new TypeError(`Hi TS Consumer, it turns out the argument was null, 
                             maybe you had an unsafe cast somewhere in your program
                             or you're not using strict nullability checks?
                             I already thought of that so I checked anyway ;)`);
    // ..
}

export function safeLibraryFunction3(arg: SomeType) {
    if (arg != null) {
        //.. do something
    }
    else {
        throw new TypeError(`Hi JS Consumer, I think about you all the time
                             that's why I put many safeguards in my program
                             to make sure that everything goes smoothly..
                             c'ya later..`);
    }

}

Expected behavior:
The comparisons work.

Actual behavior:

Operator `==` cannot be applied to type X and null
Operator `===` cannot be applied to type X and null
Operator `===` cannot be applied to type X and undefined
Operator `!=` cannot be applied to type X and null

(My projects are full of these errors when strictNullChecks is enabled)

@ahejlsberg ahejlsberg added Suggestion An idea for TypeScript Committed The team has roadmapped this issue labels May 3, 2016
@ahejlsberg ahejlsberg self-assigned this May 3, 2016
@ahejlsberg ahejlsberg added this to the TypeScript 2.0 milestone May 3, 2016
@ahejlsberg
Copy link
Member

Yup, this one is already on our list.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label May 4, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants