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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM