Stefan
Stefan

Reputation: 217

TypeScript: check of `unknown` argument - are `undefined` and `null` different cases?

Code inspection (IntelliJ and GitHub) says that the following code can be simplified:

type Something = string | number | boolean | object
function isSomething(a: unknown): a is Something {
    return a != undefined && a != null;
}

because a != null is always true. Why is that? AFAIK undefined and null are different values.

Upvotes: 0

Views: 139

Answers (1)

tenshi
tenshi

Reputation: 26324

a != undefined is false if a is null. That means if a is null, the second condition will never run, since the && operator will short-circuit.

console.log(null == undefined); // true
console.log(null != undefined); // false

Upvotes: 3

Related Questions