Does Typescript have Operator Overloading?
Asked Answered
B

2

69

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.

Bushtit answered 20/3, 2016 at 3:55 Comment(0)
N
41

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.

Naucratis answered 20/3, 2016 at 5:7 Comment(8)
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.Arun
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/…Naucratis
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.Intensifier
It is totally unnecessary to have operator overloading in native javascript for this to work.Sidesman
Typescript has many, many examples of new syntax like this one.Charteris
Regarding It could but that would defeat what TypeScript is all about 'Add or rely on run-time type information in programs'. I would want the TypeScript compiler to detect the script at compile time, and then replace === with a function call (for example)Barlow
@Barlow Doable, and I would say possibly even a good idea 🌹 But not going to happen for TS, too left field for their ideology 🥀Naucratis
look at how kotlin did it. The operators are named functions, but in code you can use operatorsOvenbird
G
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.
Goldoni answered 13/2, 2022 at 21:29 Comment(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.Brusa
@Brusa we got an update! ...but not a good oneGoldoni

© 2022 - 2024 — McMap. All rights reserved.