Export object of private class.

Topics: Language Specification
Jun 18, 2013 at 7:25 AM
Edited Jun 28, 2013 at 2:29 AM

My work is trying to use a singleton pattern, and the way we're currently doing it in javascript is by exporting a new object instead of the constructing class.

e.g. in a module:
function Greeter() {
Greeter.prototype.greet = function() {
module.exports = new Greeter();
What this allows us to do is:
  • inherit classes, overriding methods where needed and also calling super methods.
  • return an instantiated object as the module.exports variable.
Things to note:
  • I do not want to have to put the class declaration in another file.
  • I am happy to export the class as well, however I require that the object is attached to module.exports.
  • I do not want to have to manage an interface that the class implements/the object is cast to, as trying to keep interfaces up to date is a pain.
The main reason for this request is because we require module.exports to equal the new object, the object must not be an attribute of module.exports. However Typescript currently complains about exporting private types.

I would like to know if there is any way that I can do this currently? Or if there isn't, perhaps a way to declare that you want intellisense for a particular variable, even if it's type is private?

Thanks in advance!

Jun 20, 2013 at 10:50 PM
Jun 26, 2013 at 1:00 PM
I agree with Griffork. In javascript i'm using same singleton pattern as he described and miss this feature in TS.
Jul 2, 2013 at 3:50 AM
I would really like to know if this is ever going to be supported, so I can decide now whether or not to support it.
Jul 2, 2013 at 7:50 PM
I believe this accomplishes what you want, there is some subtlety involved:
interface GreeterExport {
    greet(): void;

class Greeter implements GreeterExport {
    greet() {

var GreeterExport: GreeterExport = new Greeter();
export = GreeterExport;
which emits this JavaScript:
var Greeter = (function () {
    function Greeter() {
    Greeter.prototype.greet = function () {
    return Greeter;

var GreeterExport = new Greeter();

module.exports = GreeterExport;
What's happened here is we're using export= to export all meanings of the identifier GreeterExport (in this case a var and an interface definition) and ensuring that the var is explicitly typed to the interface name so that it is of an exported type rather than the non-exported (aka private) class type.

Does that do what you want?
Jul 2, 2013 at 9:28 PM
Does that do what you want?
Technically yes, but NO (at least not for me).
In your example you have to define all public members twice - first as an Interface and then in Class definition.
It's ugly and adding lots of typing.

What i want is this:
class Greeter {
    greet() {

var greeter: Greeter = new Greeter()
export = greeter
// or simply: export = new Greeter()
It's not a killing feature, but brings cleaner code and must be easy to implement.
Jul 2, 2013 at 10:34 PM
Yes, I agree, the duplication is not ideal, but there is at least a strongly typed solution that emits the right, minimal JS for this pattern. Feel free to log an issue about improvements in how this pattern is modeled here.
Jul 3, 2013 at 1:28 AM
Due to another issue that got raised the handling of singletons in my project has changed, making this a moot issue.

However I can see it becoming a problem again in the future. Would you like to raise it as an issue dorny? If you do, post here and I'll vote on it.
Jul 3, 2013 at 5:46 AM
As an aside that perhaps may not be relevant, couldn't you use a TypeScript module to create the singleton?

Andrew Gaspar

Jul 3, 2013 at 6:24 PM
Yes, depending on your needs a module could do the job. I offered the class solution since it does allow you to model some additional things that can't be used as a top level module element (ex index signatures).
Jul 6, 2013 at 2:22 PM
Issue created: #1312.