Vadorequest
Vadorequest

Reputation: 18079

Typescript - Encapsulating module/classes so it's not accessible from the browser

I'm looking for a way to write my TypeScript classes in a way that I could provide only what I want to provide to the browser (global scope), basically something like this:

myModule.Api

Where Api would contain some public functions that could be executed from the browser, but relaying on other functions that would be written is other classes within the same module or sub-modules.

I need that these classes could use each other, but wouldn't be usable from the browser global scope, only through the Api.

What's the way to go with that? I couldn't figure out how to reuse classes from other classes without exposing them to the browser directly using export keyword.

Upvotes: 0

Views: 659

Answers (1)

basarat
basarat

Reputation: 276313

but wouldn't be usable from the browser

If you want something to transcend file boundaries you will need to export it from the file.

However if you keep it all on a single file then you don't need to use export.

module myModule.Api{
    var notExported = 0;
    export function exported(){
        return notExported + 1;
    }
}

I would like to point out that when using external modules each file is its own module and not exported to the browser i.e. window : https://www.youtube.com/watch?v=KDrWLMUY0R0

Upvotes: 1

Related Questions