Tslint: New rule to warn against triple equals null

Created on 19 Jan 2016  ยท  9Comments  ยท  Source: palantir/tslint

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?

Easy P3 Accepting PRs Rule Suggestion ๐ŸŒน R.I.P. ๐ŸŒน

All 9 comments

:+1: for this

I'll add that if possible this rule should play nicely with the no-null-keyword rule. That is:

  • if 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 issue

  • if no-null-keyword is 'off'

    • === null and === undefined should both error because of the rule proposed in this issue

Perhaps 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!

๐Ÿ’€ _It's time!_ ๐Ÿ’€

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! ๐Ÿ‘‹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajinder-yadav picture rajinder-yadav  ยท  3Comments

cretz picture cretz  ยท  3Comments

DanielKucal picture DanielKucal  ยท  3Comments

cateyes99 picture cateyes99  ยท  3Comments

allbto picture allbto  ยท  3Comments