Title: const/let variables with require() initializer are not correctly block-scoped · Issue #41697 · microsoft/TypeScript · GitHub
Open Graph Title: const/let variables with require() initializer are not correctly block-scoped · Issue #41697 · microsoft/TypeScript
X Title: const/let variables with require() initializer are not correctly block-scoped · Issue #41697 · microsoft/TypeScript
Description: TypeScript Version: 4.2.0-dev.20201126 Search Terms: TS2300 Code // compiled in JS mode. Works fine when compiled as TS switch(something) { case "a": { const x = require("fs"); // TS2300: Duplicate identifier 'x' console.log(x); break; }...
Open Graph Description: TypeScript Version: 4.2.0-dev.20201126 Search Terms: TS2300 Code // compiled in JS mode. Works fine when compiled as TS switch(something) { case "a": { const x = require("fs"); // TS2300: Duplicate...
X Description: TypeScript Version: 4.2.0-dev.20201126 Search Terms: TS2300 Code // compiled in JS mode. Works fine when compiled as TS switch(something) { case "a": { const x = require("fs"); ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/41697
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"const/let variables with require() initializer are not correctly block-scoped","articleBody":"\u003c!-- 🚨 STOP 🚨 STOP 🚨 STOP 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 4.2.0-dev.20201126\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** TS2300\r\n\r\n**Code**\r\n\r\n```js\r\n// compiled in JS mode. Works fine when compiled as TS\r\nswitch(something) {\r\n case \"a\": {\r\n const x = require(\"fs\"); // TS2300: Duplicate identifier 'x'\r\n console.log(x);\r\n break;\r\n }\r\n case \"b\": {\r\n const x = require(\"fs\"); // TS2300: Duplicate identifier 'x'\r\n console.log(x);\r\n break;\r\n }\r\n case \"c\": {\r\n const x = 123; // No error (only happens with require() as initializer)\r\n console.log(x);\r\n break;\r\n }\r\n}\r\nx.readFileSync(\"x\"); // should be error\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo TS2300 error. `const` variables should be block-scoped.\r\n\r\n**Actual behavior:**\r\n\r\n`const` variables with `require()` initializer leak into outer scope. Looks like they are incorrectly function scoped.\r\n\r\n**Playground Link:** [Playground link](https://www.typescriptlang.org/play?ts=4.2.0-dev.20201126&useJavaScript=true#code/JYWwDg9gTgLgBAIgHYQCYFMEG4BQODOA7sDAMYAWAFMUqhIQHQA2EpAhjMBEg+VOgDMAlHADeOOJLjt86RGwQAuMRKlrS3fPAAecALxx+ARwCuwfpQQD8CIbjXrNEJumYQA5pW13VDuACN+NgBreykAX19pNllEfyUVP0kNJC04XQNjMwsrGx8k6ScXN09vML9A9BDyuEj1GLkEUgTxApS0jLgARgAmAGYsOAB6IbgAOQg4dCgoaCjHVOdXFlL8pMrqqMjI7QYg1AAxYBcAZQBPJFJLbVtBkbh8cggTJlQAuWnZqCA)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n","author":{"url":"https://github.com/sokra","@type":"Person","name":"sokra"},"datePublished":"2020-11-26T08:52:20Z","url":"https://github.com/microsoft/TypeScript/issues/41697"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:af443b89-b96a-c06a-a021-45233090c426 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C6BA:6365:24E1C75:313FCC9:6B01A20C |
| html-safe-nonce | dd48328a4a6b7214f7a53e8a02e850aea93af86472b0c0646c4ad8c6e683036c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNkJBOjYzNjU6MjRFMUM3NTozMTNGQ0M5OjZCMDFBMjBDIiwidmlzaXRvcl9pZCI6IjIzNTQ4OTYwNjQ0MjQ4MTMwNjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b8e18410900a3efa6fe9b7bd18c8edd250964177e031c0f9aabeb2cbb231a20a |
| hovercard-subject-tag | issue:751405760 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/41697/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b91218cdf0691ecaa64e1f35f40881d50d52a3901309846a4218f604d8772049/microsoft/TypeScript/issues/41697 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b91218cdf0691ecaa64e1f35f40881d50d52a3901309846a4218f604d8772049/microsoft/TypeScript/issues/41697 |
| og:image:alt | TypeScript Version: 4.2.0-dev.20201126 Search Terms: TS2300 Code // compiled in JS mode. Works fine when compiled as TS switch(something) { case "a": { const x = require("fs"); // TS2300: Duplicate... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sokra |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width