0

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 global, for example getThisOrThat() 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 require() 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.

2
  • 1
    Have you tried creating a d.ts file that contains your globals? Commented Oct 28, 2016 at 7:33
  • I've not because I'm not sure what that would look like. I've got a d.ts for the global functions but I can't import it bc transpiled it's calling the module.function. so what would the d.ts look like and how to require/import it? Commented Oct 28, 2016 at 11:08

1 Answer 1

2

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;
}

}
Sign up to request clarification or add additional context in comments.

1 Comment

Your wording is exactly what I need. Let me give that a try. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.