I am working on a project which requires a lot of vector and matrix math and I would love to have operator overloading.
There are Babel plugins (for example https://github.com/rob-blackbourn/jetblack-operator-overloading) that enable operator overloading for JavaScript but they don't work with TypeScript (or at least they don't work with the TS language sever for VS Code). There are workarounds using // @ts-ignore
but I find it ugly and it removes the benefits of type checking for the next line.
I would like to convert the code like this (where a
, b
, and c
are custom objects):
a + b * c
To something like this:
a.__add__(b.__mul__(c))
And if, for example, c
is of the wrong type for function __mul__
, VS Code should underline the original line or even better the symbol c
.
I imagine it would be possible to transform the code before passing it to the TS compiler (that is exactly what the Babel plugin does). But VS Code would always show the error (Operator '+' cannot be applied to types 'Custom' and 'Custom'
).
So my question is: is there a project that achieves the above described behavior? If not is it possible to extend TypeScript and/or TypeScript language server in this way? If so please provide links to references for further research.
I am also interested in how JSX (used in React) with completely new syntax achieves full integration with TS and is there any other project that modifies TS in this way.