Create "Global / External-agnostic" declaration files without exposing internal definitions · Issue #2018 · microsoft/TypeScript (original) (raw)
Say I'm writing a declaration file for an external library that both exposes a global and supports CommonJS. According to the handbook I would write that like this:
module zoo {
function open(): void;
}
declare module "zoo" {
export = zoo;
}
And then the usage would be this:
// Either
import x = require('zoo');
x.open();
// or
zoo.open();
The problem is that while the above would compile when using external modules, at run-time it would fail since the "zoo" identifier doesn't actually exist. Is there any way to write declaration files such that you can split them into two separate files (one for internal, one for external) so that you only reference the one you intend on using but not actually duplicate the definitions in those files? For example
// zoo.d.ts
declare module "zoo" {
export = zoo;
}
// zoo-internal.d.ts
/// <reference path="zoo.d.ts" />
module zoo {
// this doesn't work but illustrates what I'm trying to accomplish
import zoo = require('zoo')
export = zoo;
}
Or any thoughts on another way to accomplish the same goals in a single file?