您好,我最近开始使用TypeScript。 对我来说这是超级有用的语言:)
顺便说一句,我在这里的有关String Type
的教程中https://www.typescriptlang.org/docs/handbook/basic-types.html
TypeScript版本:
1.8.10
码
_hello.ts_
let name: string = "bob";
每当我编译时,编译器都会发出以下内容。
$ tsc hello.ts
../../usr/local/lib/node_modules/typescript/lib/lib.d.ts(16757,13): error TS2451: Cannot redeclare block-scoped variable 'name'.
hello.ts(1,5): error TS2451: Cannot redeclare block-scoped variable 'name'.
_hello.ts_
let aname: string = "bob";
我读了lib.d.ts
,里面有declare var name: string;
。 因此,我可以理解编译器会发出错误。 但是,什么概念我们不能在TypeScript中使用变量name
?
而且我认为不起作用的教程不是很好:(
还是我有什么问题? 我希望重播答案:)
因为_global scope_上已经有一个名为name
的变量。 在浏览器console.log(name)
尝试一下,您会看到一个空字符串(或其他字符串),而不是undefined
。
将您的名称变量放在_module_或namespace
: https ://basarat.gitbooks.io/typescript/content/docs/project/modules.html
该变量的文档: https :
@basarat
谢谢您的回答。 就是这样的意思
我不知道全局范围中已经有name
变量了:)
@DanielRosenwasser
也感谢您修复文档:)
最有用的评论
因为_global scope_上已经有一个名为
name
的变量。 在浏览器console.log(name)
尝试一下,您会看到一个空字符串(或其他字符串),而不是undefined
。固定
将您的名称变量放在_module_或
namespace
: https ://basarat.gitbooks.io/typescript/content/docs/project/modules.html