I'm working on a TypeScript project (for the browser) where we're using a third party library with type definitions from DefinitelyTyped. In the type definitions for the library, its classes are declared like this:
declare namespace foo {
export class Bar { ... }
export class Baz { ... }
}
However, the actual implementation of the library places the classes in the global scope such that they can be referred to by just Bar
and Baz
. Because of this, using foo.Bar
will compile, but not work at runtime.
To make it work, I would like to somehow bring all the exports of foo
into the global namespace. I can't find any obvious ways to do this, so my best attempt so far is making an ambient declaration file saying
declare type Bar = foo.Bar;
declare const Bar: typeof foo.Bar;
and repeating for each exported class in foo
. This allows me to instantiate the class, use it as a type, and call any static functions it may have, but declaring the same name twice for all exported types is not ideal. A shorter way of achieving the same thing seems to be
declare class Bar extends foo.Bar {}
but now I am needlessly extending the class, and the intent is not clear.
Is there a better way to alias/extract the exported types of foo
as globals?
There is a dedicated syntax for extracting things from namespaces.
import Bar = foo.Bar;
Will bring the correspondingly named type, value, and or namespaces into scope under a single term named Bar
.
Ensure that this is placed in an ambient context such as d.ts
file or nested inside of a declare
context.
If foo
does not itself exist at runtime, then the declaration really needs to be fixed.
However, this is just a shorthand for your first work around, and what you should actually do is send a pull request to definitely typed to fix the declaration because it's broken.
Also note that your second solution involving extending Bar
should not be used because it will change the behavior of your program in unexpected ways.