Typescript- What is target in tsconfig?

混江龙づ霸主 提交于 2019-11-26 20:22:58

问题


I am quite new to Typescript. What does Target in tsconfig.json signify?

{
  "compilerOptions": 
  {
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "jsx": "react",
    "moduleResolution": "classic",
    "lib": [ "es2015", "dom",  "es2017" ]
  }
}

回答1:


I am quite new to Typescript. What does Target in tsconfig.json signify?

target signifies which target of JavaScript should be emitted from the given TypeScript. Examples:

target:es5

()=>null will become function(){return null} as ES5 doesn't have arrow functions.

target:es6

()=>null will become ()=>null as ES6 has arrow functions.




回答2:


Target changes the JavaScript version you are compiling to.

The options are available at https://www.typescriptlang.org/docs/handbook/compiler-options.html

In the spirit of trying to better understand how the target flag changes my code I compiled some test code to each of the different versions to have a better understanding of the differences.

https://github.com/aizatto/typescript-playground/tree/master/dist/test-async-main

I'm also keeping notes of what I should be targeting depending on what environment I am looking at

https://www.aizatto.com/notes/typescript




回答3:


TypeScript docs state the target option's purpose:

Translate newer JavaScript constructs down to an older version like ECMAScript 5

[emphasis added]

If you're using IntelliSense in Visual Studio Code you may see it set a default value for target. Assume the default set errs on the side of safety and update it based on your requirements.

Not sure which version of ECMAScript to choose? Refer to the ECMAScript Compatibility Tables. Generally speaking you should target the most recent stable version of ECMAScript which doesn't cause errors in your server/runtime, minimum supported browser or graded browser support list.



来源:https://stackoverflow.com/questions/42415942/typescript-what-is-target-in-tsconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!