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

Allow arithmetic operations on objects that have valueOf() method #48503

Closed
5 tasks done
dawidgarus opened this issue Mar 31, 2022 · 1 comment
Closed
5 tasks done

Allow arithmetic operations on objects that have valueOf() method #48503

dawidgarus opened this issue Mar 31, 2022 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@dawidgarus
Copy link

Suggestion

πŸ” Search Terms

valueOf

βœ… Viability Checklist

My suggestion meets these guidelines:

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

⭐ Suggestion

Allow arithmetic operations on objects that have valueOf() method which returns type compatible with arithmetic operations

πŸ“ƒ Motivating Example

class NumberWrapper {
    constructor(private val: number) {}
    valueOf(): number {
        return this.val;
    }
}

const a = new NumberWrapper(4);
const b = new NumberWrapper(5);
console.log(a - b);

Currently this produces errors:

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

But this works perfectly fine with vanilla JavaScript

πŸ’» Use Cases

Creating wrappers for primitive types that could be used as a target for Proxy

@RyanCavanaugh
Copy link
Member

Duplicate #2361

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants