Can you limit the scope of a TypeScript global type?

Viewed 110

Our team has configured TypeScript to make the jest types available globally:

// tsconfig.json
{
  ...,
  "types": ["jest", "@testing-library/jest-dom", ...]
}

This is really nice for *.test.ts files, but today I realized that it also accepts jest (and related functions) in regular old *.ts files—which makes sense after I read up on the tsconfig docs. Is it possible to limit the availability of a type (jest in our case) to a certain directory or subset of files?

// component.tsx (error - 'jest' should not be defined)
const mockFn = jest.fn();

// component.test.tsx (no error - 'jest' should be defined)
const mockFn = jest.fn();
0 Answers
Related