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

Default export broken for typescript moduleResolution node16 #2381

Closed
apottere opened this issue Feb 17, 2024 · 11 comments
Closed

Default export broken for typescript moduleResolution node16 #2381

apottere opened this issue Feb 17, 2024 · 11 comments

Comments

@apottere
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?
8.12.0

Your code

// tsconfig.json
{
    // ...
    "compilerOptions": {
        "module": "node16",
        "moduleResolution": "node16",
        // ...
    }
}
import Ajv from 'ajv';

const ajv = new Ajv();

What results did you expect?
No TS error is thrown.

Actual result:

error TS2351: This expression is not constructable.
  Type 'typeof import("[project]/node_modules/.pnpm/[email protected]/node_modules/ajv/dist/ajv")' has no construct signatures.

6 const ajv = new Ajv();
                  ~~~

I saw #2204, but it doesn't look like it was resolved in that issue. This is an issue with how ajv exports its types: https://arethetypeswrong.github.io/?p=ajv%408.12.0

I can provide a reproduction in a sample project but it should be fairly easy to reproduce.

@jasoniangreen
Copy link
Collaborator

Hi there, thanks for raising this.

Do you think this can be replicated in https://runkit.com/ ? Or would it need to be in a project.

I am wondering though, given Node 16 had it's EOL brought forward by 7 months due to potential vulnerabilities, should we actually by dropping support for node 16?

@apottere
Copy link
Author

I haven't used runkit before but I'll take a look.

The node16 module resolution name is confusing. It isn't the module resolution for node 16, it's the module resolution compatible with the changes introduced in node 16. Per the docs:

'node16' or 'nodenext' for modern versions of Node.js. Node.js v12 and later supports both ECMAScript imports and CommonJS require, which resolve using different algorithms. These moduleResolution values, when combined with the corresponding module values, picks the right algorithm for each resolution based on whether Node.js will see an import or require in the output JavaScript code.

As of now, node16 is the most up-to-date value for moduleResolution (not including bundler of course).

@apottere
Copy link
Author

It looks like there's no way to fiddle with builds in runkit, so I create a minimal reproduction here: https://github.com/apottere/ajv-typescript-node16

@jasoniangreen
Copy link
Collaborator

I see, I will take a look, thanks.

@yernandus
Copy link

any updates on this?

@benasher44
Copy link
Contributor

This should be fixed by #2365. Can you try @benasher44/ajv? I published that PR there

@apottere
Copy link
Author

Looks good in https://arethetypeswrong.github.io/?p=%40benasher44%2Fajv%408.12.1 and seems to resolve the issue in my reproduction 👍

@jasoniangreen
Copy link
Collaborator

Interesting tool @apottere, it does indeed look all green.

Screenshot 2024-02-28 at 21 01 07

This is compared with what we currently have
Screenshot 2024-02-28 at 21 03 33

I'll just check it out a bit more, but looking good 👍

@jomaa-daniel
Copy link

When can we expect the fix to be in? Thanks

@StormeHawke
Copy link

Also seeing this same issue in my code.
The error I'm seeing is in resolve.ts, the following two imports:

import * as equal from "fast-deep-equal"
import * as traverse from "json-schema-traverse"

can be minimally rewritten as

import equal from "fast-deep-equal"
import traverse from "json-schema-traverse"

to get it to compile (though I haven't tested if it actually works that way).

Right now I'm having to manually copy a significant amount of code from one of our internal libraries into another project because the node16 module resolution features are broken by this issue

@jasoniangreen
Copy link
Collaborator

Can you try the new version v8.13.0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants