使用TypeScript将箭头函数编译为常规函数 [英] Compile arrow functions into regular functions with TypeScript

查看:126
本文介绍了使用TypeScript将箭头函数编译为常规函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题,但我还没有在任何地方找到答案:是否有一些开关可以将TypeScript编译箭头函数转换为纯JavaScript函数?

Quite a simple question, but I haven't found an answer yet anywhere: Is there some switch to make TypeScript compile arrow functions into plain JavaScript functions?

I在我的代码中使用它们很多,我不想重写所有内容。但我最近意识到,IE不支持它们。

I use them a lot in my code, and I don't want to rewrite everything. But I lately realized, that IE doesn't support them.

我已经尝试将脚本版本切换到ES5,但之后我的代码将不再编译,因为我也使用过滤器,这似乎不是它的一部分。但是,我不知道,如果那样做的话首先要做。

I already tried to switch the script version to ES5, but then my code won't compile anymore, because I'm using "filter" as well, which doesn't seem to be a part of it. However, I don't know, if that would do the job in the first place.

推荐答案

如果你想转换你的箭头用于常规函数并保持其余的代码编译,你可以将目标配置设置为 es5 lib 属性为 es6

If you want to just convert your arrow functions to regular functions and keep the rest of the code compiling, you can set the target config to es5 and lib property to es6:

"compilerOptions": {
    "target": "es5",
    "lib": ["es6"],
    // ....
}

这将允许使用ES6功能的代码在定位ES5时进行编译。但是你必须确保这些功能(如 filter )在运行时可用。如果它们不可用,它将抛出运行时异常。

This will allow your code that uses ES6 features to compile while targeting ES5. But you have to make sure those features (like filter) are available at runtime. If they are not available it will throw runtime exceptions.

这篇关于使用TypeScript将箭头函数编译为常规函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆