從 Babel 遷移到 Typescript

以下這段ES7代碼,透過babela.ts-d.--optionalruntime之後,產出的a.js,可以簡單地再透過nodea.js得到正確的運行結果。
//a.ts
importfetchfrom'node-fetch'
!asyncfunction(){
letHTMLString=awaitfetch('http://www.google.com.tw').then(response=>response.text())
console.log(HTMLString)
}()
但,當我想使用typescript1.5作同樣的事情時,卻不知如何完成。
因為它會提示:cannotfindmodule'node-fetch'
//tsconfig.json
{
"compileOnSave":false,
"compilerOptions":{
"target":"es5",
"module":"cmd"
}
}
請問我漏了什麼了嗎?
MMMHUHU
浏览 328回答 2
2回答

慕盖茨4494581

Babel会把代码翻译成ES5的js,但是TypeScript不会。如果你在TypeScript中使用了6的特性,它会直接翻译成ES6的js。如果指定了--targetes5,它TypeScript就不支持6的特性(但支持TypeScript的特性,比如类等)

慕姐4208626

npminstalltypescript@next然后tsconfig中,target设置es6,module设置commonjs
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript