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和原文一樣(調整了空行,補充了分號)