Tslint: unknown and strict-type-predicate

Created on 10 Aug 2018  路  2Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.11.0
  • __TypeScript version__: 3.0.1
  • __Running TSLint via__: CLI

TypeScript code being linted

const body: unknown = 'test';
if (typeof body === 'object') // Expression is always true.
  console.log('a');

with tslint.yaml configuration:

---
rules:
  strict-type-predicates: true

Actual behavior

tslint falsely warns that the expression is always true.

Expected behavior

Since the object is of type unknown (and actually a string), the expression could be both true and false and thus tslint should not warn.

Easy Accepting PRs Bug

Most helpful comment

Same problem when comparing unknown type with undefined. As says TypeScript documentation, anything (including undefined) is assignable to unknown type, so comparing it with undefined is always correct.

class TestClass {
    private readonly myVar: unknown = "Test";
    public constructor() {
        this.myVar = undefined;
        if (this.myVar !== undefined) { // Expression is always true.
        }
    }
}

All 2 comments

Same here. In fact unknown should be treated the same as any with regards to this rule.

Same problem when comparing unknown type with undefined. As says TypeScript documentation, anything (including undefined) is assignable to unknown type, so comparing it with undefined is always correct.

class TestClass {
    private readonly myVar: unknown = "Test";
    public constructor() {
        this.myVar = undefined;
        if (this.myVar !== undefined) { // Expression is always true.
        }
    }
}
Was this page helpful?
0 / 5 - 0 ratings