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.
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.
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 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 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.
© 2022 - 2024 — McMap. All rights reserved.