@ types / jest index.d.ts文件返回错误

haw*_*fer 4 npm typescript jestjs stenciljs

我只是npm在我的stenciljs入门应用程序中安装了@ types / jest,现在,当我开始启动项目时,新安装的节点包将返回几个错误。这是我进入npm start项目时返回的错误:

[ ERROR ]  TypeScript: node_modules/@types/jest/index.d.ts:39:30
           A rest parameter must be of an array type.

     L39:  type ArgsType<T> = T extends (...args: infer A) => any ? A : never;

[ ERROR ]  TypeScript: node_modules/@types/jest/index.d.ts:218:112
           A tuple type element list cannot be empty.

    L217:   */
    L218:  ction spyOn<T extends {}, M extends keyof T>(object: T, method: M, accessType: 'get'): SpyInstance<T[M], []>;
    L219:  function spyOn<T extends {}, M extends keyof T>(object: T, method: M, accessType: 'set'): SpyInstance<void, [T[M]]>;

[ ERROR ]  TypeScript: node_modules/@types/jest/index.d.ts:220:144
           Type 'ArgsType<T[M]>' does not satisfy the constraint 'any[]'. Type
           '{}' is not assignable to type 'any[]'. Property 'length' is missing
           in type '{}'.

    L219:  function spyOn<T extends {}, M extends keyof T>(object: T, method: M, accessType: 'set'): SpyInstance<void, [T[M]]>;
    L220:   T, method: M): T[M] extends (...args: any[]) => any ? SpyInstance<ReturnType<T[M]>, ArgsType<T[M]>> : never;
    L221:  /**

[ ERROR ]  TypeScript: node_modules/@types/jest/index.d.ts:807:50
           Type 'ArgsType<T[P]>' does not satisfy the constraint 'any[]'. Type
           '{}' is not assignable to type 'any[]'.

    L806:  type Mocked<T> = {
    L807:      [P in keyof T]: T[P] & MockInstance<T[P], ArgsType<T[P]>>;
    L808:  } & T;
Run Code Online (Sandbox Code Playgroud)

这是我的package.json:

...
  "dependencies": {
    "@stencil/core": "~0.15.2",
    "@stencil/router": "~0.3.1",
    "imask": "^4.1.5",
    "jwt-decode": "^2.2.0",
    "material-design-icons": "^3.0.1"
  },
  "license": "MIT",
  "devDependencies": {
    "@types/jest": "^23.3.14",
    "@types/puppeteer": "1.12.1",
    "jest": "23.6.0",
    "jest-cli": "23.6.0",
    "puppeteer": "1.8.0",
    "workbox-build": "3.4.1"
  }
...
Run Code Online (Sandbox Code Playgroud)

这是我的tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "allowUnreachableCode": false,
    "declaration": false,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "moduleResolution": "node",
    "module": "esnext",
    "target": "es2017",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "jsx": "react",
    "jsxFactory": "h"
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用新的模板启动器应用程序复制错误,但不是该应用程序。另外,请尝试删除我package-lock.json的软件包并重新安装它们,以消除任何潜在的冲突。

ecr*_*345 7

尝试添加"skipLibCheck": truetsconfig.json以跳过所有声明(.d.ts)文件的类型检查。

编辑:这比实际解决方案更像是“大锤”解决方法。正如另一个答案中提到的那样,可能发生此问题,因为Jest类型是针对比项目中使用的TypeScript更新的TypeScript版本进行的。因此,正确的解决方案是将项目升级到更新的TS版本。


小智 7

我希望这还不算太晚,并且可能对某人有帮助。

您可以覆盖所需的库,而不是跳过所有库检查。在我们的例子中,这将是:@types/jest


"skipLibCheck": true作为设置的替代方法tsconfig.json

您可以从根目录创建一个新文件夹和文件: overrides/jest/index.d.ts 并添加

"compilerOptions": {
    "typeRoots": [
      "overrides",
      "./node_modules/@types"
    ]
}
Run Code Online (Sandbox Code Playgroud)

这实际上会覆盖文件夹下的所有类型检查overrides,并回退到./node_modules/@types除了笑话之外的所有类型,因为overrides/jest/index.d.ts文件可能为空。

我在 https://medium.com/@elenasufieva/handling-type-declarations-clash-in-typescript-b05b10723f47上找到了这个答案