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

javascript - Compile typescript without transpiling async functions - Stack Overflow

programmeradmin4浏览0评论

Is there a way to use the TypeScript compiler only to remove type annotations, but not transpiling async functions? Something like a { target: 'esInfinite' } option? The reason is: There are browsers that already support async functions, so I wish to have a build target where those functions are not affected.

example input:

async function foo(a : number) : Promise<void> {}

example output:

async function foo(a) {}

Is there a way to use the TypeScript compiler only to remove type annotations, but not transpiling async functions? Something like a { target: 'esInfinite' } option? The reason is: There are browsers that already support async functions, so I wish to have a build target where those functions are not affected.

example input:

async function foo(a : number) : Promise<void> {}

example output:

async function foo(a) {}
Share Improve this question edited Oct 22, 2016 at 16:27 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked Oct 7, 2016 at 14:04 Tamas HegedusTamas Hegedus 29.9k12 gold badges66 silver badges101 bronze badges 1
  • 1 There's a pull request to add support for ES2017, but until that lands, not as far as I am aware. – Sean Vieira Commented Oct 7, 2016 at 16:49
Add a comment  | 

3 Answers 3

Reset to default 20

In your tsconfig.json, change your target to ES2017, then it will preserve the async/await.

{
  "compilerOptions": {
    .....
    "target": "ES2017",
    .....
  }
}

DO make sure your run-time supports it natively!!!

PS: as of Apr 2018, AWS Lambda now supports Nodejs 8. You should be able to use the above config for it.

This feature was already requested here. Targeting es2016 and es2017 should be available in the Community milestone and in TypeScript 2.1.

I made a quick hack to override the metro-react-native-babel-preset npm package using a public git repo. I replaced the following in the package.json file under devDependencies:

    "metro-react-native-babel-preset": "^0.73.9",

with...

    "metro-react-native-babel-preset": "github:svpease/metro-react-native-babel-preset-minus-async#v1.0.1",

..which strips out the async-to-generator plugin so that async functions could finally be detected using code like the following:

(() => {}).constructor.name === 'Function'; // true
Object.getPrototypeOf(async () => {}).constructor.name === 'AsyncFunction'; // true
发布评论

评论列表(0)

  1. 暂无评论