0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

It is possible in JS to have a lot of code encapsulated in a function which isn't usable on the browser global scope, and only export what I want. Isn't possible in TS?
Yes. TS is just JS with added compile time type checks
So, the only solution would be to wrap the whole code into a scope while I conct it into a single file? I don't see any other solution for the moment. If I want to encapsulate the whole logic but using my classes from each other I will need to put all of them into a private scope so it's not globally accessible from the browser, then I'll need to manage what I expose globally.

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.