const body: unknown = 'test';
if (typeof body === 'object') // Expression is always true.
console.log('a');
with tslint.yaml configuration:
---
rules:
strict-type-predicates: true
tslint falsely warns that the expression is always true.
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.
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.
}
}
}
Most helpful comment
Same problem when comparing
unknowntype withundefined. As says TypeScript documentation, anything (includingundefined) is assignable tounknowntype, so comparing it withundefinedis always correct.