import in react-scripts react-app-env.d.ts breaks tsc build

Adding an import to a file makes it a module. So in a module if you declare an interface Window it is declared local to that module.

If you want to use imports, but still keep the declarations global, you have two options:

Use declare global

import * as React from 'react';
import { History } from 'history';
import { RenderResult } from 'react-testing-library';

declare global {
  interface Window {
    env: any;
  }

  declare function renderWithRouter(ui: any, { route, history, }?: {
    route?: string;
    history?: History;
  }): RenderResult;
}

Use import types

interface Window {
  env: any;
}

declare function renderWithRouter(ui: any, { route, history, }?: {
  route?: string;
  history?: import('history').History;
}): import('react-testing-library').RenderResult;

Either version will work, the declare global is easier if you use a lot of types from other modules.

Tags:

Typescript