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

The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2363) #45460

Open
ubershmekel opened this issue Aug 15, 2021 · 1 comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript

Comments

@ubershmekel
Copy link
Contributor

ubershmekel commented Aug 15, 2021

Suggestion

The following code errors:

      const x: Number = 5;
      const y = 5 - x;

With this:

The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2363)

Which is super confusing because it is a number. But I didn't realize the problem was the first letter capitalization. Number vs number.

🔍 Search Terms

right-hand side of an arithmetic

✅ Viability Checklist

My suggestion meets these guidelines:

  • [v] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [v] This wouldn't change the runtime behavior of existing JavaScript code
  • [v] This could be implemented without emitting different JS based on the types of the expressions
  • [v] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [v] This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Would be nice if there was an error during the assignment, is Number also a number? Another option is error 2363 could mention this potential issue. E.g.

The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. Not 'Number' .ts(2363)

And replace 'Number' with whatever type was actually found.

📃 Motivating Example

      const x: Number = 5;
      const y = 5 - x;

Minor typos should have a clear error message.

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Aug 16, 2021
@bxt
Copy link

bxt commented Dec 16, 2021

I don't think the the first letter capitalization is the only problem. Consider Playground:

const x: Number = 1;
const y: Number = 2;
const z = x + y; // Operator '+' cannot be applied to types 'Number' and 'Number'.

const a: BigInt = 1n;
const b: BigInt = 2n;
const c = a + b; // Operator '+' cannot be applied to types 'BigInt' and 'BigInt'

So here the types are exactly the same, both starting with upper case, and still TS is complaining. This is very confusing.

I think this issue would be solved when #2361 is solved, it's not an exact duplicate though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants