tsconfig / bases

Hosts TSConfigs to extend in a TypeScript app, tuned to a particular runtime environment
MIT License
6.4k stars 240 forks source link

dynamic imports create a double default-wrapped export - e.g. `{ default: { default: Foo } }` #273

Closed magicmark closed 4 months ago

magicmark commented 4 months ago

Repro

Runnable repro: https://stackblitz.com/edit/markl-ts-jest-repro-gbovnq?file=README.md

Relevant files:

foo.ts

export default function MyFoo() {
  return 'foo';
}

bar.ts

(async function main() {
  const foo = await import('./foo.js');
  console.log(foo);
})();

tsconfig.json

{
  "extends": ["@tsconfig/node20/tsconfig.json"],
  "include": ["src/**/*"]
}

Actual vs Expected behavior

const foo = await import('../src/foo');
console.log(foo);

This produces the following output:

[Module: null prototype] {
  __esModule: true,
  default: { default: [Function: MyFoo] }
}

Expected behavior is that it produces the following output:

[Module: null prototype] {
  __esModule: true,
  default: [Function: MyFoo]
}

(stackblitz runs on node 18, but this reproduces exactly on LTS (20) as well)

Thanks!

jakebailey commented 4 months ago

This has nothing to do with the tsconfig bases project, which is just shorthand for existing TS configs.

magicmark commented 4 months ago

@jakebailey understood but dynamic imports are stage 4 - I would expect this to be supported out of the box.

So my issue here specifically is - should @tsconfig/node20/tsconfig.json (and friends) be configured to support dynamic imports (without the double wrapping) without any extra userland overrides? I assume there's some combination of compilerOptions that does this, but i'm unclear what this would be

jakebailey commented 4 months ago

It is configured to do that, but this is a runtime issue with TypeScript's CJS import helpers, so the issue should be files on TypeScript. This repo is just shorthand for copying a recommended tsconfig and isn't the right place to ask about this.

magicmark commented 4 months ago

@jakebailey ack thanks https://github.com/microsoft/TypeScript/issues/59257