I am currently involved in a project that heavily relies on vector and matrix mathematics, and I am keen on incorporating operator overloading into my code.
Although there are Babel plugins available (such as https://github.com/rob-blackbourn/jetblack-operator-overloading) that enable operator overloading for JavaScript, they do not seem to work seamlessly with TypeScript, especially when it comes to the TS language server for VS Code. While there are workarounds involving the use of // @ts-ignore
, I personally find this approach unappealing as it undermines the benefits of type checking for the subsequent line of code.
My goal is to transform code like this (where a
, b
, and c
represent custom objects):
a + b * c
Into something along the lines of:
a.__add__(b.__mul__(c))
In case, for instance, c
is incompatible with the function __mul__
, I would like VS Code to highlight either the original line or ideally the symbol c
itself.
I envision the ability to preprocess the code before submitting it to the TS compiler (similar to what the Babel plugin accomplishes). However, I anticipate that VS Code would consistently flag an error (
Operator '+' cannot be applied to types 'Custom' and 'Custom'
).
My inquiry is as follows: Is there an existing project that delivers the aforementioned functionality? If not, is it feasible to enhance TypeScript and/or the TypeScript language server in this manner? Kindly share any relevant resources for further exploration.
Furthermore, I am intrigued by how JSX (utilized in React) seamlessly integrates with TS using a unique syntax. Are there any other projects that alter TS in a similar fashion?