Skip to content

Operator Overloads #5407

Closed
Closed
@Antony-Jones

Description

@Antony-Jones

It would simplify a lot of use cases if we could override relational, equality, additive, and Multiplicative operators.

My initial thoughts on how this would work is that functions would replace the operators with functions when compiling to JavaScript.

class MyClass {
    constructor() {
    }

    public Operator > (value: any):boolean {
        // compare value
    }
}

var myClass = new MyClass();

if(myClass > otherValue){
    // Do stuff
}

Becomes:

var MyClass = (function () {
    function MyClass() {
    }
    MyClass.prototype.greaterThan = function (value) {
        // compare value
    };
    return MyClass;
})();
var myClass = new MyClass();
if (myClass.greaterThan(otherValue) {
    // do Stuff
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionDuplicateAn existing issue was already createdOut of ScopeThis idea sits outside of the TypeScript language design constraints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions