Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '==' cannot be applied to types x and y in Typescript 2

TypeScript Version: 2.0.2.0

Code I know the code is a bit stupid, but I actually have these kind of tests in my code (making an expression visitor) and I really think these should fly and compile right away.

var a: boolean = (true == false); var b: boolean = (5 == 2); 

Instead it complains that operand equal cannot be applied to types 'true', 'false', '5' and '2'. Mark that they are not boolean or number, they are actually a types of 'true','false','5','2'. I know that types 'string' and 'boolean' cannot be compared, but hey, 5 is actually a number, not type '5' or am I mistaken?

This compiles though.

let x = 2; var a: boolean = 5 == x; var b: boolean = <number>5 == <number>2; 

Am I missing something, why arent't 5 and 2 considered as type 'number' ?

Expected behavior: Should compile

Actual behavior: Results in a compile error saying 'Operand '==' cannot be applied to types '<first argument>' and '<second argument>'

Background I came over this issues in typescript defining that it should be like this, but how come? https://github.com/Microsoft/TypeScript/issues/6167

like image 959
Lostfields Avatar asked Aug 31 '16 07:08

Lostfields


People also ask

What is an operator in typescript?

An operator defines some function that will be performed on the data. The data on which operators work are called operands. Consider the following expression −. 7 + 5 = 12. Here, the values 7, 5, and 12 are operands, while + and = are operators. The major operators in TypeScript can be classified as −.

How to solve typescript error type 'X' is not assignable to type 'Y'?

The "Type 'X' is not assignable to type 'Y'" TypeScript error occurs when the values on the left and right-hand side of the assignment have incompatible types. To solve the error, use a type assertion or a type guard to verify the two values have compatible types before the assignment. Here is a very simple example of how the error occurs.

Why can't I type a string as a number in typescript?

Type 'string' is not assignable to type 'number'. Argument of type 'string | number' is not assignable to parameter of type 'number'. Type 'string' is not assignable to type 'number'. Uh-oh, we’re getting an error on padding . TypeScript is warning us that adding a number | string to a number might not give us what we want, and it’s right.

How do I fix operator '+' cannot be applied to types?

The error "Operator '+' cannot be applied to types 'Number' and 'number'" occurs when we use the Number type instead of number (lowercase n). To solve the error, make sure to use the number type with lowercase n in your TypeScript code.


Video Answer


1 Answers

Literal types have many advantages, as it lets the compiler make types as narrow as possible. Your use case is one that comes up very rarely, but wanting types to be as narrow as possible permeates the entire design of the language. So yes, while it makes your life harder in this one specific case, it makes sense in the language as a whole. Users would have to suffer a significantly worse language, just to support this one rare use case.

Unfortunately, you will have to use the explicit typing you suggest yourself in the second example. I don't see this ever being fixed, because the majority of users wants the language to yell if they try to do this. It's probably the sign of a bug in a large majority of cases.

like image 123
Simon Meskens Avatar answered Sep 18 '22 15:09

Simon Meskens