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

Syntax for Ajv import? #2047

Open
ajvincent opened this issue Jul 27, 2022 · 8 comments
Open

Syntax for Ajv import? #2047

ajvincent opened this issue Jul 27, 2022 · 8 comments

Comments

@ajvincent
Copy link

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

Version 8.11.0, installed today

Ajv options object
None.

Your code

// ProjectJSON.mts

// this works, https://github.com/microsoft/TypeScript/issues/24847}
import { default as Ajv } from "ajv";

// this doesn't work, but is the currently recommended import line
//import Ajv from "ajv";
/*
_02_passthrough_types/source/ProjectJSON.mts(98,17): error TS2351: This expression is not constructable.
  Type 'typeof import("/home/ajvincent/code-generation/cross-stitch/node_modules/ajv/dist/ajv")' has no construct signatures.
*/
const ajv = new Ajv();
void(ajv);

I was bitten on this exact error with dsherret/code-block-writer#42 a few weeks ago.

Are you going to resolve the issue?

I can, by adding a little bit of documentation. I just need to know where people want it, if they don't want to quickly fix this themselves.

@epoberezkin
Copy link
Member

difficult to say what is incorrect without seeing your TS configuration.

import Ajv from "ajv" works with the TS config that ajv uses - please check it.

@epoberezkin epoberezkin changed the title TypeScript error TS2351: This expression is not constructable. (from .mts file, has workaround) Syntax for Ajv import? Aug 3, 2022
@ajvincent
Copy link
Author

difficult to say what is incorrect without seeing your TS configuration.

import Ajv from "ajv" works with the TS config that ajv uses - please check it.

Here's what I have:

{
  "compilerOptions": {
    "lib": ["es2022"],
    "module": "es2022",
    "target": "es2022",
    "moduleResolution": "node",
    "sourceMap": true,
    "declaration": true,

    "experimentalDecorators": true
  },

  "exclude": [
    "spec/**"
  ],

  "extends": "@tsconfig/node16/tsconfig.json"
}

The file is https://github.com/ajvincent/cross-stitch/blob/main/_02_passthrough_types/source/ProjectJSON.mts

@XC-Zhang
Copy link

This configuration works for me:

{
    "compilerOptions": {
        "esModuleInterop": true,
        "module": "node16",
        "moduleResolution": "node",
        "sourceMap": true,
        "strict": true
    }
}

Maybe both esModuleInterop and moduleResolution are required?

@kalvenschraut
Copy link

kalvenschraut commented Sep 9, 2022

 import { default as Ajv } from 'ajv'

The above was needed whenever your project was targeting ESM output from typescript. i.e. moduleResolution = NodeNext/Node16+ and module = NodeNext/Node16+.

With typescript 4.8.3 the above actually stopped working and started producing a different type error for me. I stumbled across this comment on a typescript issue. It seems typescript is pointing blame on the libraries being incorrectly typed for whatever reason.

To fix my error I had to switch back to

import Ajv from 'ajv'

and then when I go and try to use the Ajv class it is actually something like

 const ajv = new Ajv.default()

@LinusU
Copy link
Contributor

LinusU commented Nov 8, 2022

I can confirm that the normal import doesn't work when using moduleResolution node16 or nodenext. We are currently using the following workaround:

import AjvModule from 'ajv'

// FIXME: https://github.com/ajv-validator/ajv/issues/2047
const Ajv = AjvModule.default

jhford-scout24 pushed a commit to jhford-scout24/ajv that referenced this issue Nov 14, 2022
This is solving an issue where it's impossible for me to import Ajv in a
typescript project.  This might solve ajv-validator#2047 and allows the use of

import { Ajv } from 'ajv';
jhford-scout24 pushed a commit to jhford-scout24/ajv that referenced this issue Nov 14, 2022
This is solving an issue where it's impossible for me to import Ajv in a
typescript project.  This solves ajv-validator#2047 for me

import { Ajv } from 'ajv';
jhford-scout24 pushed a commit to jhford-scout24/ajv that referenced this issue Nov 14, 2022
This is solving an issue where it's impossible for me to import Ajv in a
typescript project.  This solves ajv-validator#2047 for me

import { Ajv } from 'ajv';
@pboymt
Copy link

pboymt commented Apr 24, 2023

In TypeScript 5.0, you can set moduleResolution to bundler to solve the problem of editor error. However, this mode is provided to allow tools such as vite to decide the module packaging method independently. The result of my compilation under esm can run normally. It is not clear whether other people's code can use this repair method.

My tsconfig.json content:

{
  "compilerOptions": {
    /* Modules */
    "module": "ES2022",                                  /* Specify what module code is generated. */
    "moduleResolution": "bundler",                       /* Specify how TypeScript looks up a file from a given module specifier. */
    ...
  }
}

wolfy1339 added a commit to octokit/webhooks that referenced this issue Sep 16, 2023
This is not a bug with the TypeScript compiler but a "feature"...
See microsoft/TypeScript#49160
See ajv-validator/ajv#2047
@jasoniangreen
Copy link
Collaborator

Is this still an issue?

@ramijarrar
Copy link

@jasoniangreen No - it was fixed in #2389

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

No branches or pull requests

8 participants