【问题标题】:Interface 'JestImportMeta' incorrectly extends interface 'ImportMeta'接口 \'JestImportMeta\' 错误地扩展了接口 \'ImportMeta\'
【发布时间】:2022-11-10 16:11:54
【问题描述】:

我正在从 jest v27 迁移到 v29 并遇到此错误:

node_modules/@jest/environment/build/index.d.ts:329:26 - error TS2430: Interface 'JestImportMeta' incorrectly extends interface 'ImportMeta'.
  The types returned by 'jest.createMockFromModule(...)' are incompatible between these types.
    Type 'unknown' is not assignable to type 'T'.
      'T' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.

329 export declare interface JestImportMeta extends ImportMeta {
                             ~~~~~~~~~~~~~~


Found 1 error in node_modules/@jest/environment/build/index.d.ts:329

尝试使用tsc 进行编译时会发生此错误。

我的 package.json 中的相关包:

"devDependencies": {
    "@types/jest": "^29.0.0",
    "jest": "^29.0.0",
    "jest-environment-node": "^29.0.0",
    "nodemon": "^2.0.4",
    "ts-jest": "^29.0.0"
}

版本:

  • NodeJs v16.15.1
  • npm v8.11.0

【问题讨论】:

    标签: node.js typescript jestjs


    【解决方案1】:

    这里的问题是 tsc 也在构建你的测试文件,这并不理想。

    解决方案:

    就像我在下面所做的那样,从您的 tsconfig.json 中排除测试文件。

    {
        "compilerOptions": {
            "module": "commonjs",
            "target": "es2017",
            "lib": [
                "es2015"
            ],
            "moduleResolution": "node",
            "sourceMap": true,
            "outDir": "bin",
            "baseUrl": ".",
            "paths": {
                "*": [
                    "node_modules/*",
                    "src/types/*"
                ]
            },
            "emitDecoratorMetadata": true,
            "experimentalDecorators": true
        },
        "include": [
            "src/**/*"
        ],
        "exclude": [
            "src/**/*.spec.ts",
            "src/**/*.test.ts"
        ]
    }
    

    【讨论】:

    • 如果您不想在其中输入错误,您实际上确实希望 tsc 构建您的测试文件......
    【解决方案2】:

    这是在 jest v29.1.0 和 @types/jest v29.0.3 中修复的类型中的错误 https://github.com/facebook/jest/issues/13199#issuecomment-1260897649

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 2018-11-28
      • 2017-07-10
      • 2019-03-21
      • 2020-11-29
      • 2020-06-03
      • 2022-11-10
      • 1970-01-01
      • 2021-07-26
      相关资源
      最近更新 更多