tsx icon indicating copy to clipboard operation
tsx copied to clipboard

Legacy importing type from namespace is not supported

Open JounQin opened this issue 3 years ago • 5 comments

Bug description

import { Rule, Scope } from 'eslint'; // `Rule` and `Scope` are both ts namespace
import Variable = Scope.Variable;
import CodePathSegment = Rule.CodePathSegment;
import Variable = Scope.Variable;
                        ^

TypeError: Cannot read properties of undefined (reading 'Variable')

Reproduction

import { Rule, Scope } from 'eslint';
import Variable = Scope.Variable;
import CodePathSegment = Rule.CodePathSegment;

Environment

  System:
    OS: macOS 13.0
    CPU: (10) arm64 Apple M1 Max
    Memory: 31.99 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.16.0 - ~/Library/Caches/fnm_multishells/55951_1660375587006/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.16.0 - ~/Library/Caches/fnm_multishells/55951_1660375587006/bin/npm
  npmPackages:
    tsx: ^3.8.2 => 3.8.2

Can you contribute a fix?

  • [X] I’m interested in opening a pull request for this issue.

JounQin avatar Aug 14 '22 08:08 JounQin

If you're importing a type, I think you'll need to specify for esbuild to understand:

- import { Rule, Scope } from 'eslint'; // `Rule` and `Scope` are both ts namespace
+ import type { Rule, Scope } from 'eslint'; // `Rule` and `Scope` are both ts namespace

privatenumber avatar Aug 14 '22 15:08 privatenumber

I know how to workaround this, I just forgot that could be an esbuild issue and the codes come from 3rd-party repository. I'll raise a new issue there instead. (Stupid me. 😅)

JounQin avatar Aug 15 '22 00:08 JounQin

I haven't tested yet but I think this is unblocked as of:

  • https://github.com/esbuild-kit/core-utils/releases/tag/v2.2.0
  • https://github.com/evanw/esbuild/releases/tag/v0.15.4

privatenumber avatar Aug 16 '22 22:08 privatenumber

I tested it out just now. Some observations:

  • Tree-shaking must be enabled to get this benefit. Without tree-shaking. With tree-shaking. I haven't considered enabling tree-shaking but I'll probably have to think about it a little more; I wonder if it is safe.
  • In ESM mode (package.json#type:module), the import statements are preserved, so it fails on the first namespace import because it's preserved for ESM:
import { Rule, Scope } from 'eslint'

eg.

SyntaxError: The requested module './some-file' does not provide an export named 'someNamespace'

privatenumber avatar Aug 17 '22 03:08 privatenumber

Tree-shaking must be enabled to get this benefit

Or can we add a flag to enable it manually?

JounQin avatar Aug 17 '22 05:08 JounQin