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
feat(eslint-plugin): add strict-type-predicates #738
Closed
bradleyayers
wants to merge
7
commits into
typescript-eslint:master
from
bradleyayers:strict-type-predicates
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0861d20
feat(eslint-plugin): add strict-type-predicates
bradleyayers 5a642e1
fix(eslint-plugin): [strict-type-predicates] add rule doc
bradleyayers 1df0433
fix(eslint-plugin): add strict-type-predicates to readme
bradleyayers 97fa253
fix(eslint-plugin): yarn generate:configs
bradleyayers a054982
fix(eslint-plugin): fix docs for strict-type-predicates
bradleyayers f5f1d27
fix(eslint-plugin): lint
bradleyayers 447bc25
Merge branch 'master' into strict-type-predicates
armano2 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/eslint-plugin/docs/rules/strict-type-predicates.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Disallow always true (or false) type predicates (`strict-type-predicates`) | ||
|
||
Warns for type predicates that are always true or always false. Works for | ||
`typeof` comparisons to constants (e.g. `typeof foo === 'string'`), and equality | ||
comparison to `null`/`undefined`. (TypeScript won’t let you compare `1 === 2`, | ||
but it has an exception for `1 === undefined`.) Does not yet work for | ||
`instanceof`. Does not warn for `if (x.y)` where `x.y` is always truthy. For | ||
that, see [`strict-boolean-expressions`](./strict-boolean-expressions.md). | ||
|
||
This rule requires `strictNullChecks` to be enabled. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
const numberOrNull: number | null = 0; | ||
// Implicitly checks for `!== undefined`, which is always false. | ||
if (numberOrNull != null) { | ||
return; | ||
} | ||
|
||
const numberOrUndefined: number | undefined = 0; | ||
// Implicitly checks for `!== null`, which is always false. | ||
if (numberOrNull != undefined) { | ||
return; | ||
} | ||
|
||
const number: number = 0; | ||
// Always false. | ||
if (typeof number === 'string') { | ||
return; | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
const numberOrNull: number | null = 0; | ||
if (numberOrNull !== null) { | ||
return; | ||
} | ||
|
||
const numberOrUndefined: number | undefined = 0; | ||
if (numberOrNull !== undefined) { | ||
return; | ||
} | ||
|
||
const number: number = 0; | ||
// Always false. | ||
if (typeof number === 'number') { | ||
return; | ||
} | ||
Comment on lines
+37
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit with these - just for correctness, typescript narrows the type of the A good way around this is to either declare const numberOrUndefined: number | undefined;
if (numberOrNull !== undefined) {
return;
} Or wrap your examples in a function: function ex1(numberOrUndefined: number | undefined) {
if (numberOrNull !== undefined) {
return;
}
} |
||
``` | ||
|
||
## Related To | ||
|
||
- TSLint: [strict-type-predicates](https://palantir.github.io/tslint/rules/strict-type-predicates) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be reported by the rule, because it's always true?
This should probably follow the above tests