['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
}
}
ERROR: ...: Expression is always true.
No error.
Remove the type annotation :number which is wrong anyway. That should
make it work
Am 18.05.2017 17:19 schrieb "Martin-Wegner" notifications@github.com:
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);with tslint.json configuration:
{
"rules": {
"strict-type-predicates": true
}
}Actual behavior
ERROR: ...: Expression is always true.
Expected behavior
No error.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/palantir/tslint/issues/2783, or mute the thread
https://github.com/notifications/unsubscribe-auth/ALaeKC67o3zB_pdCKG_RBNA0WTkRB4Tqks5r7GGSgaJpZM4NfZiq
.
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.
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
I omitted it and the same error occurs...
Using number | undefined also not work :(
That indeed looks like a bug. Do you have strictNullChecks enabled in your tsconfig.json?
No strictNullChecks is not enabled.
Btw: any now works for me, strange...
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
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?).
Most helpful comment
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?).