javascript - 错误 TS2351 : This expression is not constructable.(带有 TypeScript 的 AJV)

标签 javascript typescript ajv

这更多是我在使用 AJV 和 Typescript 时面临的一个问题。我在 ajv 存储库中提出了类似的问题,但找不到真正的解决方案。

我正在尝试从 ajv 导入 Ajv 模块,如下所示:

从“ajv;导入Ajv;

并且,像我以前在纯 JavaScript 中那样使用它。 const ajv = new Ajv({ allErrors: true });

但是,我从 ts 编译器收到一个错误,如下所示:

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 });

我在导入过程中是否做错了什么?

我的TS配置文件如下:

 `{
  "compilerOptions": {
    /* 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 compatible 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 compatibility. */
    "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/**/*"]
}
` 
 

如果有帮助,我们将不胜感激。非常感谢....

我期望 TS 编译器不会出现错误,因为我在部署过程中构建 JS 文件。

最佳答案

这是 CommonJS 与 ES 模块的错误。

像这样更改导入:

import _Ajv from "ajv";

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

现在它应该是可构造的:

const ajv = new Ajv();

我在这里找到了解决方案:https://github.com/ajv-validator/ajv/issues/2132#issuecomment-1290409907

关于javascript - 错误 TS2351 : This expression is not constructable.(带有 TypeScript 的 AJV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75228565/

相关文章:

javascript - typescript :是否有可数的类型?

angular - 如何使用 typescript 修复 Angular 5 中所有页面的 'search input field in the header'

java - 使用 spray 服务分层静态内容

javascript - 数据表分页中选中复选框的 jQuery 计数

javascript - Angular 7 Url 发生变化但没有其他 react

node.js - 向 Fastify 发出 POST 请求时,JSON 未被解析以进行验证

javascript - Chrome 打包应用程序中的 JSON 模式验证

reactjs - 错误 "custom keyword definition is invalid: data.errors should be boolean"

javascript - 如何从 javascript 调用 C++ 方法

javascript - 动态转换 SVG 的强大解决方案