【发布时间】:2016-09-23 09:30:19
【问题描述】:
我正在研究 AngularJS 2 和 Typescript,我决定用它来做一些事情,只是为了学习 Typescript 的基础知识。通过许多研究,我发现了关于模块、Typescript 的好话题,其中之一是谈论 'let' 和 'var' 命令来声明变量;根据this的问题,下面的Typescript代码应该只显示一个警报并在控制台中抛出错误:
test.ts:
for(let i = 0; i < 1; i++) {
alert(i);
}
alert(i);
编译好的 test.js:
for(var i = 0; i < 1; i++) {
alert(i);
}
alert(i);
//# sourceMappingURL=test.js.map
但事实并非如此。编译器“忽略”“let”命令并将其转换为“var”命令。为什么会这样? Typescript 是否仅适用于类?
我正在为“npm start”使用 AngularJS 配置,因此它会自动编译我的“test.ts”文件:
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
【问题讨论】:
-
更多关于 TypeScript 中的
let:basarat.gitbooks.io/typescript/content/docs/let.html
标签: javascript typescript var let