71

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
  • 51
    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. Commented 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
    Commented 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
    Commented Jun 17, 2019 at 19:18
  • 36
    It is totally unnecessary to have operator overloading in native javascript for this to work. Commented Dec 20, 2019 at 20:37
  • 4
    Typescript has many, many examples of new syntax like this one.
    – ecstrema
    Commented Oct 2, 2020 at 23:46
15

is there is operator overloading in typescript

No, and the TypeScript project doesn't seem interested in implementing this new feature unless it's first added to JavaScript. (I can respect wanting to avoid scope creep in their project.)

I was hoping that a new version of ECMAScript would implement operator overloading. See this language proposal: https://github.com/tc39/proposal-operator-overloading


Updated in 2024:

It's been decided to Withdraw the proposal.

I think I found the meeting notes with the details for why it won't be added to ECMAScript: https://github.com/tc39/notes/blob/main/meetings/2023-11/november-28.md#withdrawing-operator-overloading

Speaker's Summary of Key Points

  • Operator overloading was proposed to solve many problems with JS, including enabling different numeric types, unit-bearing calculations including CSS units, more ergonomic vector/matrix processing, and generalizing BigInt/Decimal.
  • Several JavaScript engine maintainers held that operator overloading does not have the right cost/benefit tradeoff to be worth it to implement. It would be very hard to implement efficiently, especially with the scoping/safety features.
  • Given the positions in committee on operator overloading, DE proposed withdrawing operator overloading, which may clear the way for proposals such as Decimal, where “whether to overload operators” is a pertinent design discussion.

Conclusion

  • TC39 reached consensus to withdraw the operator overloading proposal, pending SYG’s confirmation, which was given after the meeting.
  • The withdrawal is due to the high cost to implementations and high complexity of the operator overloading proposal, or any other possible proposal in this space.
  • Operator overloading may be re-introduced to TC39 in the future (especially if implementers' perception of the cost/benefit tradeoff changes), but the committee currently does not expect to spend time investigating operator overloading.
1
  • 4
    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
    Commented Feb 11, 2023 at 5:14

Your Answer

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

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