Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-useless-predicate: detect always true literal comparison #533

Closed
ajafff opened this issue Jan 18, 2019 · 0 comments
Closed

no-useless-predicate: detect always true literal comparison #533

ajafff opened this issue Jan 18, 2019 · 0 comments

Comments

@ajafff
Copy link
Member

ajafff commented Jan 18, 2019

TypeScript already detects comparisons that will always be false because the types have no overlap.
However, it doesn't detect the opposite: if a comparison of literal types will always be true.
Related upstream feature request: microsoft/TypeScript#28569

declare function get<T>(): T;

'a' === 'a'; // always true
'a' == 'a'; // always true
'a' !== 'a'; // always false

'a' === 'b'; // compiler already errors here
'a' !== 'b'; // compiler already errors here
'0' == 0; // compiler already errors here

get<'a' | 'b'>() === 'a'; // valid
get<'a' | 'b'>() === get<'a' | 'b'>(); // valid
get<string> === get<string>(); // valid

// advanced
'0' == get<'0' | 0>(); // always true
'0' == get<'0' | 1>(); // valid

// should be handled by a different rule
let a: string;
a === a;

The same applies to switch although that should be rather rare.

When microsoft/TypeScript#26592 is fixed upstream, there might be the need for an additional check for loose equal with values of a different type, e.g. '0' == 0.

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

No branches or pull requests

1 participant