将操作员的超载添加到打字稿
我正在从事一个需要大量向量和矩阵数学的项目,我会 love 使操作员超载。
有babel插件(例如 https://github.com/github.com/rob-blackbourn/jetblackbourn/jetblack-jetblack--jetblack--jetblack--jetblack-jetblack-net-运算符 - 跨加载)使操作员对JavaScript的负载超载,但它们不使用Typescript(或者至少它们与TS Language sever for for vs code无效)。使用// @ts-ignore
有解决方法,但我觉得它很丑陋,并且可以删除下一行的类型检查的好处。
我想将代码转换为这样(其中a
,b
和c
是自定义对象):
a + b * c
到这样的东西:
a.__add__(b.__mul__(c))
如果,例如,c
是函数错误的类型__ mul __
,VS代码应下划线下划线,甚至更好地符号c
。
我想在将代码传递给TS编译器之前,可以将代码转换为TS编译器(这正是Babel插件所做的)。但是VS代码始终显示错误(运算符'+'不能应用于类型“自定义”和“自定义”
)。
所以我的问题是:有没有实现上述行为的项目?如果没有,可以以这种方式扩展Typescript和/或Typescript语言服务器?如果是这样,请提供参考的链接以进行进一步研究。
我还对JSX(用于使用的JSX)如何与TS完全集成在一起,并且是否有其他项目可以以这种方式修改TS。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Babel仅在JavaScript代码上工作,您需要编写自定义的打字稿变压器,并在TS-LOADER上配置此变压器。但是,当forktscheckerwebpackplugin检查您的语法正确性时,仍然可能会引起一个问题。也许您必须关闭此插件。
以下是用于创建空变压器的示例代码:
此外,您必须配置项目,如果在VUE中,vue.config.js将是:
Babel only works on javascript code and you'll need to write a customized typescript transformer and configured this transformer on ts-loader. But still one problem may raises when the ForkTsCheckerWebpackPlugin checks your syntax correctness. Maybe you'll have to close this plugin.
Below is an example code for creating an null transformer:
Besides, you'll have to configure your project, if in vue, the vue.config.js would be: