Skip to content

import name used as a type then locally shadowed as a value causes a compile error #7899

@bradzacher

Description

@bradzacher

Describe the bug

Look - I know this code is completely and utterly cooked - it's horrible.
I found it in the codebase at work (and I've fixed it).

However this code does work in TS and produces no errors if the imported Name is a type - EVEN WITH isolatedModules: true TS playground.

In either case when TS transpiles the code it elides the import as it assumes it was a type import.

I figured I'd file this to let you know as there does seem to be a gap in the scope analysis tooling because it doesn't recognise that the local let Name is separate to the imported one.

Input code

import { Name } from 'some_module';
let Name: Name | null = null;
Name = null;

Config

{
  "inlineSourcesContent": true,
  "jsc": {
    "externalHelpers": true,
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true,
      "tsx": true
    },
    "target": "es2019",
    "transform": {
      "react": {
        "runtime": "automatic",
        "refresh": false,
        "useBuiltins": true
      },
      "optimizer": {
        "simplify": true
      }
    }
  },
  "sourceMaps": true
}

Playground link

https://play.swc.rs/?version=1.3.74&code=H4sIAAAAAAAAA8vMLcgvKlGoVvBLzE1VqFVIK8rPVVAvzs9Njc%2FNTynNSVW35spJLQFLW0EU1SjklebkKNiCKWsusBiMAwDv20JmTwAAAA%3D%3D&config=H4sIAAAAAAAAA12RMW7DMAxF95zC0Jyh7daOzdIOnXoCQqFbFhIlkBRgN8jdIxmxoXgRxPfJrw%2FqchgGRxyI8TsV8ainxIZs7m0wKXhs%2Bp%2F6Wl7qtRY4GQpD%2BMCQUbTrq2IGUZStuRKd2WCqxNmcUb1QNndc1TP6JGBp59OUmSGS%2F4w5ie1F0%2BmOFnK9v24gP9iaHerL0%2FOrW7kA65gk9sEEwVsHGipsFLEZQLEUwchvWZeRUVB%2Fqz5CUOyUovheKBix9sG2aLUl5epN%2Fw%2FbafuhmAON827ssJ6LgdPlb74gb%2FbXGxHSD7u6AQAA

SWC Info output

No response

Expected behavior

Compiles fine and elides the import - i.e. output should be

let FC = null;
FC = null;

Actual behavior

SWC errors:

  x cannot reassign to an imported binding
   ,-[input.tsx:1:1]
 1 | import { Name } from 'some_module';
   :          ^^|^
   :            `-- imported binding
 2 | let Name: Name | null = null;
 3 | Name = null;
   : ^^^^
   `----

Version

1.3.74

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions