swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.72k stars 71 forks source link

Sometimes namespace import has unexpected behavior #675

Closed keidarcy closed 1 year ago

keidarcy commented 1 year ago

Descirbe

When using namespace import with config like

import * as config from "config";

for (const p in config) {
  console.log("p:", p);
}
$ node -r @swc/register src/index.ts
p: a
p: default

output has no function get or has


import config from "config";

for (const p in config) {
  console.log("p:", p);
}
$ node -r @swc/register src/index.ts
p: a
p: util
p: get
p: has

output has functions like get or has

Playground link

Question

I'm not sure should @swc/register treat namespace import and default import the same. Is there a flag like in esModuleInterop in tsconfig.json to change the behavior?

keidarcy commented 1 year ago
  "module": {
    "type": "commonjs",
    "strict": true,
    "noInterop": true <---
  },

I found this flag to make this work.