65

My question is if there is operator overloading in typescript, if it exists I could give an example or a link where you can read about it.

2 Answers 2

42

No it does not exist. It is very unlikely that it will exist unless there is a clear Spec on how it might be implemented in Pure JavaScript.

8
  • 46
    Forgive my ignorance, but why would operator overloading be needed in pure Javascript, in order for TypeScript to support it? Couldn't the TypeScript compiler, for example, convert a + operator in the TypeScript source with a function call in the JavaScript output. Feb 14, 2018 at 14:10
  • 7
    Couldn't the TypeScript compiler, for example, convert a + operator in the TypeScript source with a function call in the JavaScript output It could but that would defeat what TypeScript is all about Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata. github.com/Microsoft/TypeScript/wiki/…
    – basarat
    Feb 14, 2018 at 20:33
  • 13
    As long as the operator type checks, it could just be translated into a specific function call. The dependency on the type system would only be during compilation.
    – Abram
    Jun 17, 2019 at 19:18
  • 34
    It is totally unnecessary to have operator overloading in native javascript for this to work. Dec 20, 2019 at 20:37
  • 4
    Typescript has many, many examples of new syntax like this one.
    – ecstrema
    Oct 2, 2020 at 23:46
9

If you'd like operator overloading to exist, you could contribute to this language proposal: https://github.com/tc39/proposal-operator-overloading

If the proposal is declined you'll have a very thorough reason why it shouldn't be added to the language.

1
  • 2
    Anybody know whats up with that proposal? It seems like it was last active around 4 years ago, and no indication there whether its under consideration still or just abandoned.
    – GGizmos
    Feb 11 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.