question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syntax for Ajv import?

See original GitHub issue

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 https://github.com/dsherret/code-block-writer/issues/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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
LinusUcommented, 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
3reactions
kalvenschrautcommented, 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()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting started - Ajv JSON schema validator
Ajv takes a schema for your JSON data and converts it into a very efficient JavaScript code that validates your data according to...
Read more >
User defined keywords - Ajv JSON schema validator
import {_, KeywordCxt} from Ajv ajv.addKeyword({ keyword: "even", type: "number", schemaType: "boolean", // $data: true // to support [$data reference](.
Read more >
API Reference | Ajv JSON schema validator
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Read more >
ajv-cli
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Read more >
Ajv JSON schema validator
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found