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
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.
-
46Forgive 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 aboutAdd 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/…– basaratFeb 14, 2018 at 20:33 -
13As 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.– AbramJun 17, 2019 at 19:18
-
34It is totally unnecessary to have operator overloading in native javascript for this to work. Dec 20, 2019 at 20:37
-
4
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.
-
2Anybody 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.– GGizmosFeb 11 at 5:14