Almost always I want to compare against null and undefined, not just null. So === null is probably an error.
This rule should suggest to the user, Did you mean == null instead?
:+1: for this
I'll add that if possible this rule should play nicely with the no-null-keyword rule. That is:
no-null-keyword is 'on'=== null should error because of the no-null-keyword rule === undefined should error because of the rule proposed in this issueno-null-keyword is 'off'=== null and === undefined should both error because of the rule proposed in this issuePerhaps the title of the issue could be adjusted to reflect this?
@myitcv right now we don't have a way of communicating between rules to check if others are enabled. So I think the best path forward is to split your ideas up into two rules, no-triple-equals-null and no-triple-equals-undefined. See https://github.com/palantir/tslint/pull/1060/files#r58076945
Since undefined is not a reserved keyword in JavaScript and could easily be redefined, please extend the a == undefined check to include the pattern a == void(0), a == void 0, a == void true or other void-expressions since void is actually a reserved keyword and will always return the undefined primitive value.
+1
--strictNullChecks should catch the error. i.e.:
function len(x: string | null | undefined) {
return x === null ? 0 : x.length; // Error: object is possibly 'undefined'.
}
I'd still like to have this rule for projects that haven't migrated to strictNullChecks yet
Note: per #4534, this issue will be closed in less than a month if no PR is sent to add the rule. If you _really_ need the rule, custom rules are always an option and can be maintained outside this repo!
TSLint is being deprecated and no longer accepting pull requests for new rules. See #4534. ๐ฑ
If you'd like to see this rule implemented, you have two choices:
๐ It was a pleasure open sourcing with you!
_If you believe this message was posted here in error, please comment so we can re-open the issue!_
๐ค Beep boop! ๐ TSLint is deprecated ๐ _(#4534)_ and you should switch to typescript-eslint! ๐ค
๐ This issue is being locked to prevent further unnecessary discussions. Thank you! ๐