0

I am creating Angular application with code coverage report.

In one method I want to skip some line using some false condition, So I tried to write one function in my component like:

sum(num1:number,num2:number){
    if(1 == 2)
    {
        //Some Code
    }
     return num1+num2;
}

But it is giving me error message like

Operator '==' cannot be applied to type '1' and '2'

In some cases it is allowing and in some cases it is not allowing like:

if(1 != 2) //not allowed
if(1 == 2) //not allowed
if('1' == '2') //not allowed
if(1===2) //not allowed

if(2 == 2) //allowed
if(2 != 2) //allowed
if('1' == '1') //allowed
if(1===1) //allowed
if(parseInt('1') == parseInt('2')) //allowed

Can anyone explain about these scenarios?

Mehul Patel
  • 1,084
  • 2
  • 12
  • 28
  • This is because you should use === instead of == for type checking – Ihor Skliar May 14 '18 at 13:47
  • I tried 1===2 also but not working. Editing my question with these scenarios. – Mehul Patel May 14 '18 at 13:49
  • 2
    Possible duplicate of [Operator '==' cannot be applied to types x and y in Typescript 2](https://stackoverflow.com/questions/39243143/operator-cannot-be-applied-to-types-x-and-y-in-typescript-2) – jcalz May 14 '18 at 13:50
  • I think you mean TypeScript *is* working by giving you an error. :) Duplicate question should explain. – Aaron Beall May 14 '18 at 13:54

2 Answers2

3

Using comparison operators on values of different types is usually an error, so TypeScript warns about that:

if (1 === "1") {} // error

TypeScript treats numeric and string literals as their own types, so comparing two different literals will be an error for the same reason:

if (1 === 2) {} // error

Note that comparing two literals in JavaScript is usually guaranteed to have a known outcome. There's only one possible result for (1 == 2). So generally there's not much use to writing that code since you can replace it with its known outcome: (1 == 2) can be replaced with false, and (1 === 1) can be replaced with true. And so, there's not much of a reason for the language maintainers to care about adding support for comparing two different literals... it's hard to imagine a valid use case for comparing literals that isn't better served by not comparing literals.

However, if you really need to make such a comparison you can widen (one of) the literal types to their common supertype, like number or string:

if (1 as number == 2) { console.log("nope"); } // works
if ('1' as string == '2') { console.log("nope"); } // works

You are telling TypeScript to consider 1 not as the literal 1 but as a number, and that can be compared to 2. But again, you don't really need to do that. Instead, you can do:

if (false) { console.log("nope"); } // unreachable code detected!

That new warning is also a good one. Usually, unreachable code is a sign of an error. Personally I'd rather just comment that code out or remove it than leave unreachable code. Or just leave the error as a reminder that you should go back and clean it up.

However, if you really want an if (false) {} scenario to work without an error, the more idiomatic way to do it would be something like

if (false as boolean) { console.log("nope"); } // works

where you're telling the compiler to consider false to be just a boolean, and therefore it no longer realizes or cares that the code after the conditional is unreachable.

Hope that makes sense.

jcalz
  • 264,269
  • 27
  • 359
  • 360
0

Please,chekout once whether it is number or string,because in some scenario if you are geting values from other component it may/may not in number format it is in string quoted number like '12345' insted of 12345,so it may create some problem

Omkar Jadhav
  • 656
  • 1
  • 5
  • 18