2

I need to implement Singleton pattern in TypeScript. I have found a solution here but it seems like an overkill for me. After all, JavaScript is great in creating singletons.

Is it possible to write in TypeScript something like this (without getting an error)?

module Helpers {
    Helpers.Singleton = {};
}

Currently, it correctly generates the output that I am expecting:

// Module
var Helpers;
(function (Helpers) {
    Helpers.Singleton = {};
})(Helpers || (Helpers = {}));

But why is TypeScript compiler inside my VisualStudio complaining about that (it says that it "Could not find symbol 'Helpers'." at line 2!)? Is it possible to write it in another way?

Left side shows generated code, right side illustrates the error highlight by VisualStudio

1 Answer 1

10
module Helpers {
    export module Singleton {
        export var etc = 4;
        export function printSomething() {
            // etc
        }
    }
}

// later...
Helpers.Singleton.printSomething();
Sign up to request clarification or add additional context in comments.

1 Comment

Why isn't enough to export just the Singleton module? but all its properties and functions? thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.