0
votes

I've been happily developing for months in TypeScript using classes with "attached" modules in CommonJS external modules as follows :

exports = JQ;

class JQ {
    a = 0;
}

module JQ {
    export class HelpClass {}
    export interface Something {}
}

I like this pattern because it makes modules contained in files, reduce the number of imports, and still exposes a nice API.

Now, instead of exporting a class I want to export a Hybrid Type, so that it can be used as a function having other functions and classes "inside", like for example jQuery, Underscore, and many other libraries does.

I know how to declare and how to create a hybrid type in TypeScript, however don't manage to export it with an attached module. I do obtain the right emitted JS code, but the compiler complains.

For example, this :

export = JQ;

var JQ = <JQ.JQStatic>function (a:string) { return a; };
JQ.method = function() { return 1; };

module JQ {
    export interface JQStatic {
        (a:string):string;
        method() :number;
    }

    // Additional class here
    export class SomethingElse {}
}

Produces what looks like the right JS code (looks exactly like the one generated if JQ was a class and not a hybrid type) but the compiler gives duplicate identifier error, and intellisense stop working.

If I remove the additional class SomethingElse, it works correctly, so I suppose that's a special case to export an hybrid type.

I tested it also on TypeScript Playground and got same results: despite class JQ and var JQ both emit a var JQ in JS, when it's a class TypeScript allows and accepts it, otherwise not.

Is what I'm trying to do unsupported? However, since the emitted JS is valid, so it's a feature already in the compiler, should this be reported as a bug to TypeScript devs?

1

1 Answers

0
votes
function JQ(a: string) { return a; }

module JQ {
    export class SomethingElse { }

    export function method() { return 1; }
}

export = JQ;

See https://github.com/Microsoft/TypeScript/wiki/Declaration-Merging