Use either external module declaration or regular module declaration

Topics: General
Jun 20, 2013 at 6:26 AM
For a JavaScript library that will use a module loader like AMD or RequireJS when available or will fall back to assigning the module to the global namespace, is there a similar solution for typescript declarations?

For example, the Q promise library does this. A declaration could look like:
declare function Q(value: any): Qpromise;
declare module Q {
    export function when(value: any, onFulfilled: Function, onRejected?: Function): Qpromise;
    export function fbind(method: Function, ...args: any[]): Qpromise;
    export function fcall(method: Function, ...args: any[]): Qpromise;

// etc. etc.

declare module "q" {
    export = Q;
Obviously in this circumstance, a node app, for example, could attempt to use Q directly. This will cause a run time error, but it would be great if this could be caught at compile time.