WebStorm doesn't recognize or autocomplete types declared globally in d.ts file

355 Views Asked by At

I'm trying to extend Jest's expect method per their documentation here. I'm using TypeScript in a create-react-app project, so I need to extend the CustomMatchers interface to include my new expectation. That documentation has an example of how to do that.

I've put the type declaration in a d.ts file, and called expect.extend in my setupTests.ts script that runs before all the tests. When the test using the new expectation runs, it passes and fails correctly.

The problem is that WebStorm doesn't recognize the new expectation method. It underlines it in red saying TS2339: Property 'toBeFocused' does not exist on type 'Matchers ', and it won't autocomplete the name for me. I've tried putting the declaration in a file called global.d.ts or jest.d.ts in several different places in my project structure including <root>, <root>/src, <root>/test, and <root>/typings.

I've also tried including the d.ts file in the .tsconfig's include array, and in package.json's types array.

The only thing that works is including the type declaration and the call to expect.extend directly in a test file that uses the new expectation method.

I tried setting something up based on this other answer. The method wanted to add was called toBeFocused. A file called matchers.ts existed in <root>/test trying to declare the function, and setupTests.ts called expect.extend.

0

There are 0 best solutions below