-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
const/let variables with require() initializer are not correctly block-scoped #41697
Comments
That’s because in JS code, TypeScript treats const x = require("foo"); as import x = require("foo"); which gets transpiled to var x = require("foo"); There’s no easy fix other than supporting #31090 and defining declare function require<T extends string>(module: T): typeof import(T); |
Thu culprit seems to be #39770. Unfortunately there was no mention of this change in the release notes and there are many related issues. |
We didn't realize it was a break at the time -- our bad! |
I surveyed Definitely Typed, our user tests and all the code in their The total number of duplicated imports is quite small: 55 out of 71,000 non-toplevel
So:
I think the value of just silencing the error is pretty low since it only helps checkJS code bases, and we expect checkJS code bases to be on the way to using TS/ES imports anyway. Especially if the cost is making the 71,000 other non-toplevel |
TypeScript Version: 4.2.0-dev.20201126
Search Terms: TS2300
Code
Expected behavior:
No TS2300 error.
const
variables should be block-scoped.Actual behavior:
const
variables withrequire()
initializer leak into outer scope. Looks like they are incorrectly function scoped.Playground Link: Playground link
Related Issues:
The text was updated successfully, but these errors were encountered: