javascript - Error TS2351: This expression is not constructable. (AJV with TypeScript) - Stack Overflow

this is more of an issue I am facing using AJV with Typescript.I raised an similar issue in the ajv r

this is more of an issue I am facing using AJV with Typescript. I raised an similar issue in the ajv repo but couldn't really find an solution.

I am trying to import the Ajv module from ajv as such:

import Ajv from "ajv;

And, use it as I used to do it in plain JavaScript. const ajv = new Ajv({ allErrors: true });

However, I am getting an error from the ts piler as such:

error TS2351: This expression is not constructable.
  Type 'typeof import("/Users/username/Documents/node_modules/ajv/dist/ajv")' has no construct signatures.

 const ajv = new Ajv({ allErrors: true });

Is there anything that I am doing wrong during the import process?

My TS config file is as below:

 `{
  "pilerOptions": {
    /* Visit  to read more about this file */

    /* Projects */

    /* Language and Environment */
    "target": "ES6",                                  /* Set the JavaScript language version for emitted JavaScript and include patible library declarations. */

    /* Modules */
    "module": "Node16",                                /* Specify what module code is generated. */
    "moduleResolution": "Node16",                          /* Specify how TypeScript looks up a file from a given module specifier. */

    /* JavaScript Support */

    /* Emit */
    "outDir": "./dist",                                  /* Specify an output folder for all emitted files. */

    /* Interop Constraints */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type patibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */

    /* Completeness */
    // "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
    "skipLibCheck": true,                                 /* Skip type checking all .d.ts files. */

    "sourceMap": true,
    "rootDir": "src",
    "declaration": true,
    "types": ["node", "mocha"]
  },
  "include": ["src/**/*", ".env"],
  "exclude": ["dist/**/*", "node_modules", "test/**/*"]
}
` 
 

Help would be appreciated. Many Thanks....

I was expecting no error from the TS piler as I am building the JS files during deployment process.

this is more of an issue I am facing using AJV with Typescript. I raised an similar issue in the ajv repo but couldn't really find an solution.

I am trying to import the Ajv module from ajv as such:

import Ajv from "ajv;

And, use it as I used to do it in plain JavaScript. const ajv = new Ajv({ allErrors: true });

However, I am getting an error from the ts piler as such:

error TS2351: This expression is not constructable.
  Type 'typeof import("/Users/username/Documents/node_modules/ajv/dist/ajv")' has no construct signatures.

 const ajv = new Ajv({ allErrors: true });

Is there anything that I am doing wrong during the import process?

My TS config file is as below:

 `{
  "pilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    /* Projects */

    /* Language and Environment */
    "target": "ES6",                                  /* Set the JavaScript language version for emitted JavaScript and include patible library declarations. */

    /* Modules */
    "module": "Node16",                                /* Specify what module code is generated. */
    "moduleResolution": "Node16",                          /* Specify how TypeScript looks up a file from a given module specifier. */

    /* JavaScript Support */

    /* Emit */
    "outDir": "./dist",                                  /* Specify an output folder for all emitted files. */

    /* Interop Constraints */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type patibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */

    /* Completeness */
    // "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
    "skipLibCheck": true,                                 /* Skip type checking all .d.ts files. */

    "sourceMap": true,
    "rootDir": "src",
    "declaration": true,
    "types": ["node", "mocha"]
  },
  "include": ["src/**/*", ".env"],
  "exclude": ["dist/**/*", "node_modules", "test/**/*"]
}
` 
 

Help would be appreciated. Many Thanks....

I was expecting no error from the TS piler as I am building the JS files during deployment process.

Share Improve this question asked Jan 25, 2023 at 0:11 Kripu KhadkaKripu Khadka 613 bronze badges 1
  • It looks like your import statement is missing a " in the end after ajv, perhaps that is one of the problems. Other than that then it looks like it should work. import Ajv from "ajv; -> import Ajv from "ajv"; – Nils Kähler Commented Jan 25, 2023 at 9:05
Add a ment  | 

2 Answers 2

Reset to default 8

This is a CommonJS vs ES Modules error.

Change your import like this:

import _Ajv from "ajv";

const Ajv = _Ajv as unknown as typeof _Ajv.default;

And now it should be constructable:

const ajv = new Ajv();

I found the solution here: https://github./ajv-validator/ajv/issues/2132#issuement-1290409907

You can now use

import { Ajv } from 'ajv'

See this PR: https://github./ajv-validator/ajv/pull/2389

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744293434a4567142.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信