Search code examples
javascripttypescriptprogram-entry-point

Is there a Main()-like function in TypeScript?


I've started to write code in TypeScript for 3 days. I have completed all my class designs but at this point I'm writing the main function. Everything is nice but somebody have to run the Main() function. I do this with this way;

  • by calling directly Main();

I'm just curious about that is there any method which is not triggered by me ? or How do you handle your Main() methods when typing your code in TypeScript?


Solution

  • Unlike class-based languages, JavaScript does not require a main method.

    Since TypeScript is a super-set of JavaScript, all JavaScript code is also valid TypeScript, and therefore doesn't need a main.

    So, you can call your Object (JavaScript) or Class (TypeScript) anything you'd like -- Main, DoThis or even ServeMeADrink and all are equally valid and can be invoked by calling, just as you've done.

    They can also be invoked by:

    • event handlers
    • A framework's system, such as the data-main attribute used by some MVC/JavaScript systems.