Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let name: string = "bob"; doesn't work. #8107

Closed
harukaeru opened this issue Apr 15, 2016 · 3 comments
Closed

let name: string = "bob"; doesn't work. #8107

harukaeru opened this issue Apr 15, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@harukaeru
Copy link

Hello, I start to use TypeScript recently. It's super useful language for me :)
By the way, I'm in a tutorial about String Type in here https://www.typescriptlang.org/docs/handbook/basic-types.html

TypeScript Version:

1.8.10

Code

It doesn't work.

hello.ts

let name: string = "bob";

Whenever I compile, the compiler will emit the following.

$ 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'.

It works.

hello.ts

let aname: string = "bob";

Expecting Behavior

I read a part of the lib.d.ts and it has declare var name: string;. So I may understand the compiler emits an error. However what concept cannot we use the variable name in TypeScript?
And I think the tutorial which doesn't work is not good :(
Or is there something is wrong with me? I hope to replay an answer :)

@harukaeru harukaeru changed the title let name: string = "bob"; doesn't work. let name: string = "bob"; doesn't work. Apr 15, 2016
@basarat
Copy link
Contributor

basarat commented Apr 15, 2016

Because there is already a variable called name on the global scope. Try it out in your browser, console.log(name) and you will see an empty string (or maybe some other string) instead of undefined.

Fix

Put your name variable in a module or namespace : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

@basarat
Copy link
Contributor

basarat commented Apr 15, 2016

Documentation for this variable : https://developer.mozilla.org/en-US/docs/Web/API/Window/name 🌹

@harukaeru
Copy link
Author

@basarat
Thank you for your answer. That has such a meaning...
I didn't know there is already name variable in global scope :)

@DanielRosenwasser
Thank you for fixing the document, too :)

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants