privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
9.84k stars 154 forks source link

Legacy importing type from namespace is not supported #83

Open JounQin opened 2 years ago

JounQin commented 2 years ago

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?

privatenumber commented 2 years ago

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
JounQin commented 2 years ago

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. 😅)

privatenumber commented 2 years ago

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

privatenumber commented 2 years ago

I tested it out just now. Some observations:

eg.

SyntaxError: The requested module './some-file' does not provide an export named 'someNamespace'
JounQin commented 2 years ago

Tree-shaking must be enabled to get this benefit

Or can we add a flag to enable it manually?