name is special
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'.
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.
See also #8107.
Then maybe the TypeScript documentation should be changed? In Basic Types:String: let name: string = "bob";
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 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 :(
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'.
no I'm not, that's the problem :(
how are your running your compiler? what version of tsc are you using? can you share a repro?
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
so if you run tsc on the tsconfig directly not through awesome-typescript-loader, do you see the error?
I retried with just tsc, no error, it builds the project just fine, I guess the problem lies within awesome-typescript-loader then :)
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.
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
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.
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 !