Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@types/jest index.d.ts file returning error

I just npm installed @types/jest to my stenciljs starter app and now, when I go to start my project the newly installed node package is returning several errors. Here are the errors it's returning when I go to npm start my project:

[ 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;

This is my 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"
  }
...

Here is my 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"
  ]
}

I have tried replicating the error with a fresh stencil starter app and it's not the app. Also, tried deleting my package-lock.json and reinstalling the packages to get rid of any potential conflicts.

like image 760
cashews Avatar asked Feb 11 '19 21:02

cashews


2 Answers

Try adding "skipLibCheck": true to tsconfig.json to skip type checking of all declaration (.d.ts) files.

EDIT: This is more of a "sledgehammer" workaround than an actual fix. As referenced in the other answer, this issue probably happens because the Jest typings were made for a newer TypeScript version than the one being used in the project. So the proper fix would be upgrading the project to a newer TS version.

like image 149
ecraig12345 Avatar answered Nov 11 '22 11:11

ecraig12345


I hope this is not too late and might be helpful to someone.

Instead of skipping all library checks, you can override the libraries you want. In our case that would be:@types/jest

As an alternative to setting
"skipLibCheck": true to tsconfig.json

you can create a new folder and file from your root: overrides/jest/index.d.ts and add

"compilerOptions": {
    "typeRoots": [
      "overrides",
      "./node_modules/@types"
    ]
}

This would essentially override all the types check under the overrides folder and fallback to ./node_modules/@types except jest thanks to overrides/jest/index.d.ts file which can be empty.

I found this answer on https://medium.com/@elenasufieva/handling-type-declarations-clash-in-typescript-b05b10723f47

like image 6
Rahul Verma Avatar answered Nov 11 '22 11:11

Rahul Verma