Skip to content

Instantly share code, notes, and snippets.

@ahmedelgabri
Created July 2, 2019 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedelgabri/5c5619c65734e1e7787b3114ba0b58e7 to your computer and use it in GitHub Desktop.
Save ahmedelgabri/5c5619c65734e1e7787b3114ba0b58e7 to your computer and use it in GitHub Desktop.
global types in Typescript

I'm migrating a legacy codebase to Typescript & it has some global objects attached to window & I want these to be available always.

So I created globals.d.ts file that looks something like this

interface Foo {
  a: string;
  b: number;
}

interface Bar {
  c: string;
  d: Array<string>
}

declare global {
  interface Window {
    foo: Foo;
    bar: Bar;
  }
}

and I tried all of these variations in tsconfig.json

{
  "extends": "some-config",
  "include": ["./path/to/types/folder/**"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "src": ["./src"],
    }
  }
}
{
  "extends": "some-config",
  "files": ["./path/to/globals.d.ts"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "src": ["./src"],
    }
  }
}
{
  "extends": "some-config",
  "compilerOptions": {
    "typeRoots": ["node_modules/@types", "./path/to/types/folder"]
    "baseUrl": ".",
    "paths": {
      "src": ["./src"],
    }
  }
}

None of these worked...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment