Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapters throw typescript errors on import when moduleResolution is set to "NodeNext" or "ESNext" #5110

Closed
adam-coster opened this issue May 29, 2022 · 1 comment
Labels
adapters - general Support for functionality general to all adapters types / typescript

Comments

@adam-coster
Copy link
Contributor

adam-coster commented May 29, 2022

Describe the bug

The index.d.ts file of each adapter has its export like so:

declare function plugin(): Adapter;
export = plugin;

But when Typescript (4.7+) is set to have moduleResolution of NodeNext or ESNext the import statement throws a compiler error. This can be fixed by changing the export to the following:

export default function plugin(): Adapter;

Reproduction

Importing with NodeNext module resolution (should see the error):

https://www.typescriptlang.org/play?target=99&moduleResolution=99&ts=4.7.0-beta#code/JYWwDg9gTgLgBAQwCYLDAplAyjBNgDGcAZlBCHAOQACAzgG7oA2GAVrQPTKoZQC0CAK4wIlANxA

Same deal with ESNext:

https://www.typescriptlang.org/play?target=99&ts=4.7.0-beta#code/JYWwDg9gTgLgBAQwCYLDAplAyjBNgDGcAZlBCHAOQACAzgG7oA2GAVrQPTKoZQC0CAK4wIlANxA

Logs

No response

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700KF
    Memory: 21.52 GB / 31.26 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.13.1 - ~/.volta/tools/image/node/16.13.1/bin/node
    npm: 8.9.0 - ~/.volta/tools/image/npm/8.9.0/bin/npm
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0-next.48 => 1.0.0-next.48 
    @sveltejs/kit: ^1.0.0-next.345 => 1.0.0-next.345 
    typescript: 4.8.0-dev.20220529

Severity

serious, but I can work around it

Additional Information

As far as I can guess, there aren't backwards compatibility problems if the export is changed to:

export default function plugin(): Adapter;

Since the packages are already set up as ESM.

@dummdidumm dummdidumm added adapters - general Support for functionality general to all adapters types / typescript low hanging fruit labels May 29, 2022
@adam-coster
Copy link
Contributor Author

adam-coster commented May 29, 2022

I cloned the SvelteKit repo to see if I could just make a PR real quick, but it turns out it's not that simple.

The adapters each have an entrypoint index.js file like this:

/** @type {import('.')} */
export default function (options = {}) {
  // ...

And an index.d.ts file as discussed above. So the .js files are importing the definitions files.

In this scenario, simply changing the original declaration to an export default statement causes the JSDoc-imported-type to mismatch. Somehow the typing becomes more strict when using export default versus declare. So making this change will require some sort of change to how types are imported into the JavaScript files.

EDIT: It looks like changing those JSDoc import statements to e.g. /** @type {import('.').default} */ does the trick, so I'll see if I can get a PR going.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters - general Support for functionality general to all adapters types / typescript
Projects
None yet
Development

No branches or pull requests

2 participants