-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
imported symbol that is shadowed with a local symbol does not error #55584
Comments
I guess if the transpiler assumes there were no TS errors, thereβs no real conflict here. If a local declaration has the same name as an import, the transpiler can assume the import had to be a type, and any value references to that name wonβt count as a value usage of the import. I agree this is sketchy though, and Iβm open to adding a rule in |
If swc canβt easily work around this, that raises the importance. |
FWIW eslint rules also struggle with this. I didn't know this case was possible when I wrote the scope analyser - so this leads to some bad cases that slip through. For example import {Foo} from 'foo';
// import is never used - should be reported by no-unused-vars, but isn't
function Foo() {}
Foo(); These are both cases I found in the codebase at work because swc errored on them (I've just started playing around with swc instead of TS in our builds). There's some complicated things with imports because they're always technically valid in either type or value locations - so for external tools it's hard to reason about this case of local declaration shadowing. It does feel a bit weird that TS just makes the assumption that the code is correct and silently elides the import. |
Not totally sure what you mean by this. A lot of people seem to be under the impression that |
It's kind of strange that this error is being flagged against files that aren't even being compiled. I'm seeing this error when a With TS 5.5.2, I'm seeing this when compiling
(the type declaration happens to be from |
It's expected for TypeScript to show errors in all files it processes, unless |
Marking an import as "type-only" (to make it clear it isn't a value) only seems relevant for files that are being compiled to runtime JavaScript. The modifier helps answer the question "should this import be kept in the runtime code?". But in this case this lib file:
|
π Search Terms
imported symbol shadowed
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?isolatedModules=true#code/JYWwDg9gTgLgBAbzgYQuCA7Aph+BfOAMyjTgHIosBDAYxjIG44B6ZuAFQGU4soSoAznExwYAC2BCqQ4PSlwAblQA2AVywAoZVnip02XAC4UaSAfgAfOBlXLlcALzXbyhho2hIsRHABiyOAJiUgpqOkYWNi44FWUIAHchcUkYmTkY0QBPME1teH9jfzgrGztHZzs3IA
π» Code
π Actual behavior
The first case correctly errors:
The second case is allowed, even under
isolatedModules
.π Expected behavior
Both cases should error
Additional information about the issue
Looking at this from two perspectives -
isolatedModules: true
andfalse
For
isolatedModules: false
- this looks like it's working as expected. In a fully type-aware build TS can tell that the local value symbol shadows an imported type symbol - which is valid - and it can handle it just fine and knows it 100% can elide the import.However for
isolatedModules: true
- I'd expect that TS acts stricter here. Without type information it's not really safe to assume that the imported name is a type.From TS's POV I believe that when it transpiles the code it sees the imported symbol is never used as a value (due to the local symbol), so it elides the import.
This accidentally makes the code runtime-valid so it won't crash.
I'm of the opinion here that TS should always error on this case or else it's possible to write some really cooked looking code like
The text was updated successfully, but these errors were encountered: