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 arithmetics on class/objects with valueOf method #5337

Closed
Zorgatone opened this issue Oct 20, 2015 · 7 comments
Closed

Allow arithmetics on class/objects with valueOf method #5337

Zorgatone opened this issue Oct 20, 2015 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@Zorgatone
Copy link

Both of the two examples below won't work, and I get a compiler error/warning: "The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.".

Example 1 (defining method valueOf and implementing Number interface) working on runtime:

class LOL implements Number {
    private _value: number;

    constructor(value) {
        this._value = value;
    }

    valueOf() {
        return this._value;
    }

    toFixed() {
        return Number.prototype.toFixed.call(this._value);
    }

    toExponential() {
        return Number.prototype.toExponential.call(this._value);
    }

    toPrecision() {
        return Number.prototype.toPrecision.call(this._value);
    }
}

type Lol = number | LOL;

let X: Lol = new LOL(10);
let Y: Lol = new LOL(7);

alert(X * Y);

Example 2 (extending Number "class") Runtime Error: "Uncaught TypeError: Number.prototype.valueOf is not generic"

class LOL extends Number {
    constructor(value) {
        super(value);
    }

    valueOf() {
        return super.valueOf();
    }

    toFixed() {
        return super.toFixed();
    }

    toExponential() {
        return super.toExponential();
    }

    toPrecision() {
        return super.toPrecision();
    }
}

type Lol = number | LOL;

let X: Lol = new LOL(10);
let Y: Lol = new LOL(7);

alert(X * Y);
@Zorgatone Zorgatone changed the title Allow arithmetics on class/objects Allow arithmetics on class/objects with valueOf method Oct 20, 2015
@Zorgatone
Copy link
Author

I also tried this one (defining a variable with the Number interface), and got the same right-hand side error message:

let X: Number = new LOL(10);
let Y: Number = new LOL(7);

alert(X * Y);

@Zorgatone
Copy link
Author

I must define X,Y as any type, which is not the best thing to do 😞

@xLama
Copy link

xLama commented Oct 20, 2015

This works on runtime but you can get a compiler error:

var x = new Number(10);
var y = new Number(20)

x * y 

I guess by design.

@Zorgatone
Copy link
Author

@xLama yeah, of course. I was just experimenting with it, to see if there is a way to stop that compiler warning/error.

The other example works on runtime, but the TypeScript compiler complains about it

@xLama
Copy link

xLama commented Oct 20, 2015

@Zorgatone But I don´t know why fails because ES lets it. Maybe a Microsoft employee can explain us. I think it should work to align with ECMA.

@Zorgatone
Copy link
Author

It's just a warning/error from Typescript, but the generated code is correct and executes fine on runtime (first example only).

I know the second example will never work, it was a test

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 20, 2015
@RyanCavanaugh
Copy link
Member

See #2361

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants