I'm running into some problem exporting, like it's said on title, a namespace from an external module to the global namespace in a declaration file. Here I'll post the piece of code to better explain my problem.
//SomeClass.d.ts - I have to declare classes like this due to a not pertinent reason
interface SomeClassConstructor {
new(): SomeClass;
prototype: SomeClass;
/* static method and variables of SomeClass*/
}
interface SomeClass {
/* method and variables of SomeClass */
}
declare const SomeClass: SomeClassConstructor;
export = SomeClass;
//externalModule.d.ts
import SomeClass_ = require("./SomeClass");
interface Module {
/* declarations */
SomeClass: typeof SomeClass_;
}
declare namespace Module {
/* other declarations */
export type SomeClass = SomeClass_;
}
export = Module;
//module.d.ts
import module = require("./externalModule");
declare global {
interface Window {
Module: module;
}
// What insert here to access Module as a namespace
}
// var someObject: /* like this here: --> */ Module.SomeClass = new window.Module.SomeClass();
//this below of course works, but I need to acces Module globally
var someOtherObject: module.SomeClass = new module.SomeClass();
Edit: Maybe this could help someone to answer this question.
I discovered that doing this:
__//script.ts__
/// <reference path="module.d.ts"/>
const Module = window.Module;
var SomeClass = new window.Module.SomeClass();
by type inference I got SomeClass as type: Module.SomeClass, and I can access the type doing:
var someObject: typeof SomeClass;
However I have to replace var SomeClass = new window.Module.SomeClass()
with var SomeClass;
(that gives a type of any) in order to avoid unwanted Module.SomeClass initializations.
It is a bad workaround that I would preferably avoid.
Doing the following doesn't work, because const
s and namespaces are distinct:
// module.d.ts
declare global {
interface Window {
Module: module;
}
const Module: typeof module;
type Module = typeof module;
}
The following should work however:
//module.d.ts
import module = require("./externalModule");
export = module
export as namespace Module
declare global {
interface Window {
Module: typeof module;
}
}
Note that if you can modify externalModule.d.ts, you can just add the export as namespace Module
declaration there.