Skip to content

x != y marked as always true when it is not #36534

Closed
@DominicKramer

Description

@DominicKramer

Search Terms:
!=, implicit conversion

Code

const x = 0;
const y = '0';

if (x != y) {
  console.log('In if block');
} else {
  console.log('In else block');
}

Expected behavior:
The code should compile successfully. Since != uses implicit conversion it is possible for 'x != y' to be false even if x is a number and y is a string. For example 0 != '0' is false).

Note that when the javascript file generated by tsc in the above code is run, the else block is hit as expected.

Actual behavior:
The code has the compilation error:

error TS2367: This condition will always return 'true' since the types 'number' and 'string' have no overlap.

5 if (x != y) {
      ~~~~~~

Playground Link:

http://www.typescriptlang.org/play/?ts=3.8.0-dev.20200128#code/FAYw9gdgzgLgBADzgXjgBgNykrOBPFOAcjSK2AEsAzOACiQEJU8BKOAb2DjnGjABsApgDp+YAOa0iASQhxqcAEZiQAayIssAXziD+UQRy48cAkWMky5eg0pXrNwLcCA

Related Issues:

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions