Skip to content

Instantly share code, notes, and snippets.

@ahmedelgabri ahmedelgabri/ts.md
Created Jul 2, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.