TypeScript 编译目标(target)设置

TypeScript的编译配置文件tsconfig.json中有许多配置项,本文简单对比编译目标环境的配置项(target)的设置。模块(module)不在本文讨论之内,是另外一个大话题。

实验对于target 分别取值es5, es2015, es2016, es2017时,输出。

文件1:index.ts

async function helloWorld(): Promise<string> {
    const res = await fetch('/static/data.json');
    const txt = await res.text();
    return txt;
}
(async ()=>{
    const txt = await helloWorld()
    console.log(`async func: `, txt)
})()

执行tsc编译

tsc --target 'es3'    index.ts --outFile 'index.es3.js'    --lib 'es6','dom'
tsc --target 'es5'    index.ts --outFile 'index.es5.js'    --lib 'es6','dom'
tsc --target 'es6'    index.ts --outFile 'index.es6.js'
tsc --target 'es2017' index.ts --outFile 'index.es2017.js'

结果:

es3, es5 内容一样,有64行;

es6有19行;

es2017和原文一样(调整了空行,补充了分号)

猜你喜欢

转载自www.cnblogs.com/wangjiping/p/TypeScript-target.html
今日推荐