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

unknown and strict-type-predicate #4107

Closed
ThomasdenH opened this issue Aug 10, 2018 · 2 comments · Fixed by #4444
Closed

unknown and strict-type-predicate #4107

ThomasdenH opened this issue Aug 10, 2018 · 2 comments · Fixed by #4444

Comments

@ThomasdenH
Copy link
Contributor

ThomasdenH commented Aug 10, 2018

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.

@InExtremaRes
Copy link

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

julien-f added a commit to JsCommunity/json-rpc-protocol that referenced this issue Oct 10, 2018
Does not behave correctly with `unknown` type, see palantir/tslint#4107.
@LehaIvanov
Copy link

LehaIvanov commented Nov 14, 2018

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.
        }
    }
}

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

Successfully merging a pull request may close this issue.

4 participants