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

Cannot redeclare block-scoped variable 'name' #22436

Closed
rajinder-yadav opened this issue Mar 11, 2017 · 6 comments
Closed

Cannot redeclare block-scoped variable 'name' #22436

rajinder-yadav opened this issue Mar 11, 2017 · 6 comments
Assignees
Labels
*as-designed Described behavior is as designed typescript Typescript support issues

Comments

@rajinder-yadav
Copy link

  • VSCode Version: Code 1.10.2 (8076a19, 2017-03-08T14:00:46.854Z)
  • OS Version: Linux x64 4.4.0-66-generic
  • Extensions:
Extension Author Version
vscode-markdownlint DavidAnson 0.7.0
EditorConfig EditorConfig 0.6.0
sort-lines Tyriar 1.2.0
vscode-eslint dbaeumer 1.2.7
tslint eg2 0.8.1
cpptools ms-vscode 0.10.3
Spell seanmcbreen 0.9.1

Below code is generating a false error:


Steps to Reproduce:

const sub = {
  "name": "Rajinder Yadav",
  "email": "devguy.ca@gmail.com" 
};

let t = `
{
  "name": "++lib-name++",
  "version": "0.0.0",
  "description": "",
  "keywords": [],
  "main": "dist/--libraryname--.js",
  "typings": "dist/src/--libraryname--.d.ts",
  "files": [
    "dist"
  ],
  "author": "++name++ <++email++>",
  "license": "MIT",
  "engines": {
    "node": ">=6.0.0"
  },
`;


const name = "++name++";
const email = "++email++";
let s = t
.replace(name, sub.name )
.replace(email, sub.email);

console.log(s);

Error

node_modules/typescript/lib/lib.d.ts(18935,15): error TS2451: Cannot redeclare block-scoped variable 'name'.                       
src/main.ts(25,7): error TS2451: Cannot redeclare block-scoped variable 'name'.                                                    

npm ERR! Linux 4.4.0-66-generic
npm ERR! argv "/home/yadav/.nvm/versions/node/v7.5.0/bin/node" "/home/yadav/.nvm/versions/node/v7.5.0/lib/node_modules/npm/bin/npm-cli.js" "run" "--@tscli/demo:doc_folder=docs/typedoc" "--@tscli/demo:main=build/main.js" "tsc"
npm ERR! node v7.5.0
npm ERR! npm  v4.3.0
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! @tscli/demo@0.1.0 tsc: `tsc`
npm ERR! Exit status 2
****
@mjbvz mjbvz self-assigned this Mar 11, 2017
@rajinder-yadav
Copy link
Author

In fact the same error can be produced with a file containing only the following line.

const name = "test";

@mjbvz
Copy link
Collaborator

mjbvz commented Mar 13, 2017

Thanks @rajinder-yadav

I believe this behavior is by design. By default, TypeScript uses the DOM typings for the global execution environment and there is a name property on the global window in the DOM. In this case, TypeScript is providing a potentially helpful error message, provided the script is executed in a browser at least.

There are two easy ways to avoid this problem:

Let me know if you run into any trouble with this

@mjbvz mjbvz closed this as completed Mar 13, 2017
@mjbvz mjbvz added *as-designed Described behavior is as designed typescript Typescript support issues labels Mar 13, 2017
@dziamid
Copy link

dziamid commented Aug 2, 2017

Wow, I'm so surprised to see that typescript behaves this way. Shouldn't it suppose to act similar to nodejs where each file is a module and all variables declared in a file are locally scoped unless explicitly exported?

@mjbvz
Copy link
Collaborator

mjbvz commented Aug 2, 2017

@dziamid TS supports both use cases. If you use import or export or module then vars are locally scoped to the file but we still allow people to use globals in older style code

@dziamid
Copy link

dziamid commented Aug 2, 2017

What about require? Does it also mean that file is a module?

@piq9117
Copy link

piq9117 commented Oct 14, 2017

I added "lib": ["es2015"] in my compilerOptions and the warning is still persisting. 😢
update:
this solved it! 😃
https://stackoverflow.com/questions/40900791/cannot-redeclare-block-scoped-variable-in-unrelated-files

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*as-designed Described behavior is as designed typescript Typescript support issues
Projects
None yet
Development

No branches or pull requests

4 participants