最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compile arrow functions into regular functions with TypeScript - Stack Overflow

programmeradmin4浏览0评论

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

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.

I already tried to switch the script version to ES5, but then my code won't pile 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.

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

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.

I already tried to switch the script version to ES5, but then my code won't pile 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.

Share Improve this question asked Sep 29, 2017 at 8:32 André R.André R. 4377 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

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

This will allow your code that uses ES6 features to pile 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.

Arrow functions are a part of ES6, but you should be able to transpile your code to ES5 with Babel. There is a babel-preset-typescript which you should be able to use for this.

发布评论

评论列表(0)

  1. 暂无评论