Brad Martin Brad Martin - 8 months ago 60
TypeScript Question

How to let TypeScript know about a function attached the the global namespace

I've got a good grasp of TypeScript and use it often so I know how to do most things.

Assume I have a function attached the

, for example
is the event. This function is attached to global but it's also part of a module that's in the project's node_modules. It has perfect typings to work with but since the module doesn't export functions directly (remember they're attached to global).

So now I can't do
import { getThisOrThat } from 'the-module';
because the transpiled would be:

module.getThisOrThat() /// crash and burn cause it's on global :)

I can't
the module either because, it's the same of course when transpiled.

To pass the compiler I currently know of two options.

  • declare var getThisOrThat: Function;

  • global.getThisOrThat()

Both of those work to pass the compiler check but I really want to give the entire project the benefit of the typings for this module with global functions. I've also tried adding it with a
<ref />
but no luck there.


So you have typings that declare some function in a module, but at runtime you want to use it as if it was global? Then you can add your own d.ts file that augments global namespace:

declare module 'the-module-global' {

import * as TheModule from 'the-module';// this import is used for typechecking only

global {
    var getThisOrThat : typeof TheModule.getThisOrThat;