Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

strict-type-predicates reports 'Expression is always true.' which is not true #2783

Closed
Martin-Luft opened this issue May 18, 2017 · 9 comments

Comments

@Martin-Luft
Copy link

Martin-Luft commented May 18, 2017

Bug Report

  • TSLint version: 5.2.0
  • TypeScript version: 2.3.2
  • Running TSLint via: ng lint --type-check from Angular CLI

TypeScript code being linted

['a', 'b', 'c'].map((s: string) => {
  if (s === 'b') {
    return undefined;
  }
  return 1;
}).filter((n: number) => n !== undefined);
['a', 'b', 'c'].map((s: string) => {
  if (s === 'b') {
    return undefined;
  }
  return 1;
}).filter((n: number | undefined) => n !== undefined);
['a', 'b', 'c'].map((s: string) => {
  if (s === 'b') {
    return undefined;
  }
  return 1;
}).filter(n => n !== undefined);

with tslint.json configuration:

{
  "rules": {
    "strict-type-predicates": true
  }
}

Actual behavior

ERROR: ...: Expression is always true.

Expected behavior

No error.

@ajafff
Copy link
Contributor

ajafff commented May 18, 2017 via email

@Martin-Luft
Copy link
Author

Martin-Luft commented May 18, 2017

Why is number wrong? The mapping function maps string to number... When I use any the same error occurs.

Without type annotation the same error occurs.

@ajafff
Copy link
Contributor

ajafff commented May 18, 2017

Your function maps to number | undefined . Either use that as type annotation of the parameter or just omit it and let typescript infer the type.

The bug with any is fixed by a recent PR

@Martin-Luft
Copy link
Author

I omitted it and the same error occurs...

@Martin-Luft
Copy link
Author

Using number | undefined also not work :(

@ajafff
Copy link
Contributor

ajafff commented May 18, 2017

That indeed looks like a bug. Do you have strictNullChecks enabled in your tsconfig.json?

@Martin-Luft
Copy link
Author

Martin-Luft commented May 18, 2017

No strictNullChecks is not enabled.

Btw: any now works for me, strange...

@ajafff
Copy link
Contributor

ajafff commented May 18, 2017

This rule does not work as expected without strictNullChecks, because the current implementation assumes that it's enabled.

I doubt it would be useful to add special handling for strictNullChecks = false, because the purpose of this rule is to check if you are comparing to null or undefined on non-nullable values. Everything else is already checked by the compiler.
The only check that could be performed without strictNullChecks would be somethink like this:

declare var num: number;
typeof num === "function"; // this will never be true
typeof num === "symbol"; // this will never be true
typeof num === "string"; // this will never be true
...
typeof num === "object"; // could be true when num === null
typeof num === "undefined"; // could be true when num === undefined

@ajafff
Copy link
Contributor

ajafff commented May 18, 2017

Of course we could do a better job documenting that behavior. Maybe even warn at runtime if this rule is used without strictNullChecks (and not apply this rule?).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants