74

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

43

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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/…
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.
It is totally unnecessary to have operator overloading in native javascript for this to work.
Typescript has many, many examples of new syntax like this one.
|
18

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.