ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.71k stars 872 forks source link

Default export broken for typescript moduleResolution node16 #2381

Closed apottere closed 5 months ago

apottere commented 6 months ago

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/ajv@8.12.0/node_modules/ajv/dist/ajv")' has no construct signatures.

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

I saw https://github.com/ajv-validator/ajv/issues/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 commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

I see, I will take a look, thanks.

yernandus commented 6 months ago

any updates on this?

benasher44 commented 6 months ago

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

apottere commented 6 months ago

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

jasoniangreen commented 6 months ago

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 commented 5 months ago

When can we expect the fix to be in? Thanks

StormeHawke commented 5 months ago

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 commented 4 months ago

Can you try the new version v8.13.0?