- Saved searches
- Use saved searches to filter your results more quickly
- Error «Operator ‘===’ cannot be applied to types» with enum type #11533
- Error «Operator ‘===’ cannot be applied to types» with enum type #11533
- Comments
- Operator cannot be applied to types typescript
- # Operator ‘+’ cannot be applied to types ‘Number’ and number
- # Always use the number type when typing numbers in TypeScript
- # Convert the object type to a primitive
- # Additional Resources
- Saved searches
- Use saved searches to filter your results more quickly
- Operator ‘+’ cannot be applied to types ‘T’ and ‘T’ #41449
- Operator ‘+’ cannot be applied to types ‘T’ and ‘T’ #41449
- Comments
- Saved searches
- Use saved searches to filter your results more quickly
- Operator ‘+’ cannot be applied to types ‘T’ and ‘T’. #12410
- Operator ‘+’ cannot be applied to types ‘T’ and ‘T’. #12410
- Comments
- Saved searches
- Use saved searches to filter your results more quickly
- error TS2365: Operator ‘+’ cannot be applied to types ‘Number’ and ‘Number’. #2031
- error TS2365: Operator ‘+’ cannot be applied to types ‘Number’ and ‘Number’. #2031
- Comments
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
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
Error «Operator ‘===’ cannot be applied to types» with enum type #11533
Error «Operator ‘===’ cannot be applied to types» with enum type #11533
Comments
TypeScript Version: 2.0.3
enum SomeEnum V1, V2 > class SomeClass protected state:SomeEnum method() // "Operator '===' cannot be applied to types 'SomeEnum.V1' and 'SomeEnum.V2'." error here this.state = SomeEnum.V1 if (this.state === SomeEnum.V2) > // and the same error here if (SomeEnum.V1 === SomeEnum.V2) > // but no error here let state2 = SomeEnum.V1 if (state2 === SomeEnum.V2) > > >
Expected behavior:
I didn’t expect that error in these cases
The text was updated successfully, but these errors were encountered:
ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 11, 2016
@ahejlsberg so how to properly compare enum values? Such change broke our existing code using TS < 2.x.
@psulek You shouldn’t have to change anything. If something broke in your code, the compiler is likely pointing out something suspicious. Can you send a repro of what you’re seeing?
Maybe it has something to do with generators/yield.
export enum State < Unknown, Initializing, InitFailed, InitSuccess >class Processor < engineState: State; public * initializeAsync(): Iterable< this.engineState = State.Initializing; try < // yield some action // yield this.verifyAsync(); this.engineState = State.InitSuccess; > catch (err) < this.engineState = State.InitFailed; >finally < return this.engineState === State.InitSuccess; >> >
Save file as «ts-enums.ts» and compile with:
error TS2365: Operator ‘===’ cannot be applied to types ‘State.Initializing’ and ‘State.InitSuccess’
Operator cannot be applied to types typescript
Last updated: Jan 23, 2023
Reading time · 2 min
# Operator ‘+’ cannot be applied to types ‘Number’ and number
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.
Here is an example of how the error occurs.
Copied!const num: Number = 100; // ⛔️ Error: Operator '+' cannot be applied // to types 'Number' and 'number'.ts(2365) console.log(num + 100);
We used the Number , non-primitive object type to type the num variable, which caused the error.
We can’t use the addition operator (+) to add the non-primitive object type to a value of type number .
# Always use the number type when typing numbers in TypeScript
To solve this, always use the number type when typing numbers in TypeScript.
Copied!const num: number = 100; console.log(num + 100); // 👉️ 200
The type in the example can be inferred based on the provided value, so we don’t even have to set it.
Copied!// 👇️ const num: 100 const num = 100; console.log(num + 100); // 👉️ 200
We used the primitive number type which resolved the error.
# Convert the object type to a primitive
If you don’t have access to the code that uses the Number object type, convert the object type to a primitive number by passing it to the Number() function.
Copied!const num = 100 as Number; console.log(Number(num) + 100); // 👉️ 200
You might also see examples online that use the unary plus (+) operator to convert a value to a number .
Copied!const num = 100 as Number; console.log(+num + 100);
The error was caused because there is a difference between the primitive number , string and boolean types and the non-primitive Number , String , Boolean , Object , etc.
The non-primitive types are objects and should never be used when typing values in TypeScript.
The TypeScript best practices documentation warns to never use the Number , String , Boolean , Symbol or Object non-primitive objects when typing values in your TypeScript code.
Instead, you should be using number , string , boolean , symbol and Record types.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
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
Operator ‘+’ cannot be applied to types ‘T’ and ‘T’ #41449
Operator ‘+’ cannot be applied to types ‘T’ and ‘T’ #41449
Comments
I think @Jameskmonger is right that this is a compiler bug. The type T is definitely known and constrained by the compiler to be a specific type, that’s important to how generics fundamentally work and why the compiler refuses to compile even if you force it to accept the addition, e.g.:
type Numeric = bigint | number; function sum(a: T, b: T) < return a + b; > console.log(sum(1, 2)) console.log(sum(1n, 2n)) console.log(sum(1n, 2))
Note that the last sum is refused by the compiler (not at runtime).
@Jameskmonger if T extends string | number then T can be string | number which means one value of type T can be string a different value of type T can be a number .
It really sounds like you should use overloads here rather than a type parameter.
The text was updated successfully, but these errors were encountered:
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
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
Operator ‘+’ cannot be applied to types ‘T’ and ‘T’. #12410
Operator ‘+’ cannot be applied to types ‘T’ and ‘T’. #12410
Comments
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
function addT extends string | number>(x: T, y: T): T return x + y; > addstring>("a", "b"); addnumber>(5, 3); add("a", "b"); add(5, 3);
Expected behavior:
Code to compile fine, because T is of type string | number and therefore can have the + operator applied.
Actual behavior:
Operator ‘+’ cannot be applied to types ‘T’ and ‘T’.
The text was updated successfully, but these errors were encountered:
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
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
error TS2365: Operator ‘+’ cannot be applied to types ‘Number’ and ‘Number’. #2031
error TS2365: Operator ‘+’ cannot be applied to types ‘Number’ and ‘Number’. #2031
Comments
var a: Number = new Number(1); var b: Number = new Number(2); var c: number = 3; console.log(a+b); console.log(b+c);
When compiled, the compiler produces these errors:
index.ts(4,13): error TS2365: Operator '+' cannot be applied to types 'Number' and 'Number'. index.ts(5,13): error TS2365: Operator '+' cannot be applied to types 'Number' and 'number'.
But if I don’t use —noEmitOnError, the compiler produces this index.js:
var a = new Number(1); var b = new Number(2); var c = 3; console.log(a + b); console.log(b + c);
Which I can run with node and get expected results:
Why does the compiler refuse to allow addition of two Numbers, or a Number and a number?
The text was updated successfully, but these errors were encountered:
danquirk added the By Design Deprecated — use «Working as Intended» or «Design Limitation» instead label Feb 13, 2015
@danquirk, does this seem correct to you?
«The binary + operator requires both operands to be of the Number primitive type or. «
index.ts(4,13): error TS2365: Operator ‘+’ cannot be applied to types ‘Number’ and ‘Number’.
The language of section 4.15.2 is confusing because it only says Number (with a capital N) but then says primitive type , which implies number (with a lowercase n). If the Typescript team really is going to insist that only number types can be added, and that Number types cannot, then I think that section of the spec should be clarified.
But I want to protest this stance.
First, you can’t say ‘in practice people aren’t creating number via new Number’, because you obviously don’t have full knowledge of all javascript practice. I am a user (and minor contributor) to node-java, a bridge API that allows node.js applications to launch an embedded JVM and execute Java code. The node-java API returns javascript Number objects everywhere a Java method returns a numeric type.
Second, yes, you provided an artificial example how someone can create a var with a Number interface, but do you think that people do that in practice? Do you think that a programmer who did that would complain to you that applying operator+ to his var yielded incorrect results, and deny that his code was nonsensical?
Finally, I want typescript to catch at compile-time errors in use of types that are likely to result in errors at runtime, and not yield compile-time errors when the emitted code is unlikely to lead to a compile time error. I know there is a probably a lot of gray area here, but when in doubt, typescript should do no harm, i.e not prevent a programmer from using correct Javascript. If the Javascript spec disallowed adding two Numbers, or adding a Number to a number, then Typescript clearly should too. But it is wrong for Typescript to disallow something so simple as adding two numbers.