TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

name is special

Open richlim opened this issue 10 years ago • 15 comments

TypeScript Version:

Version 1.9.0-dev.20160417

Code

// A self-contained demonstration of the problem follows...
let name: string = "hallo";

Expected behavior: compile

Actual behavior: error TS2451: Cannot redeclare block-scoped variable 'name'.

richlim avatar Apr 17 '16 16:04 richlim

The issue is that there's always a name when you're running in a browser environment:

https://developer.mozilla.org/en-US/docs/Web/API/Window/name

In TypeScript 2.0, you'll be able to exclude the definition using the lib flag if you're running in, say, Node.js.

DanielRosenwasser avatar Apr 18 '16 01:04 DanielRosenwasser

See also #8107.

DanielRosenwasser avatar Apr 18 '16 02:04 DanielRosenwasser

Then maybe the TypeScript documentation should be changed? In Basic Types:String: let name: string = "bob";

richlim avatar Apr 19 '16 04:04 richlim

I already have fixed this at https://github.com/Microsoft/TypeScript-Handbook/issues/251, so we'll just need to republish the docs to the website.

DanielRosenwasser avatar Apr 19 '16 17:04 DanielRosenwasser

@DanielRosenwasser is there anyway to get more context on where this "name" variable is defined ? I'm only getting ERROR in [default] C:\www\front-portal-v2\node_modules\typescript\lib\lib.d.ts:18448:14 Cannot redeclare block-scoped variable 'name'. but there are no details as to where it is in my code, and I cannot find it :(

ocombe avatar Sep 12 '16 07:09 ocombe

you should see another error for the other file, something like:

./node_modules/typescript/lib/lib.d.ts(18454,15): error TS2451: Cannot redeclare block-scoped variable 'name'.
./test/a.ts(1,5): error TS2451: Cannot redeclare block-scoped variable 'name'.

mhegazy avatar Sep 12 '16 17:09 mhegazy

no I'm not, that's the problem :(

ocombe avatar Sep 12 '16 17:09 ocombe

how are your running your compiler? what version of tsc are you using? can you share a repro?

mhegazy avatar Sep 12 '16 17:09 mhegazy

I'm using webpack 1.x with awesome-typescript-loader and typescript 2.0.2 I cannot share a repro as it is happening in a private work project and if I knew what was causing the problem in my code I'd fix it :D I've downgraded to ts 2.0 for now

ocombe avatar Sep 12 '16 17:09 ocombe

so if you run tsc on the tsconfig directly not through awesome-typescript-loader, do you see the error?

mhegazy avatar Sep 12 '16 17:09 mhegazy

I retried with just tsc, no error, it builds the project just fine, I guess the problem lies within awesome-typescript-loader then :)

ocombe avatar Sep 14 '16 08:09 ocombe

I retried with just tsc, no error, it builds the project just fine,

what version of tsc were you using? this change was introduced in TS 2.0.

mhegazy avatar Sep 14 '16 17:09 mhegazy

I was using 2.0.0 before, and updated to 2.0.2, I updated awesome-typescript-loader from 2.2.1 to 2.2.4 at the same time

ocombe avatar Sep 14 '16 17:09 ocombe

that is not what i would expect. i would expect an error message for re-declaring a block-scoped variable. i would be interested to see a repro project if one is available.

mhegazy avatar Sep 14 '16 19:09 mhegazy

Ok, as it turns out the problem was with zone.js that was polluting the global namespace and replacing window.name (https://github.com/angular/zone.js/issues/456), I just updated zone.js and the error disappeared !

ocombe avatar Sep 20 '16 07:09 ocombe