10

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?

2 Answers 2

12

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.
1
  • You are right! This is useful. Event handlers look like a good choice. With these line of code is actually does what I want - window.addEventListener("load",Main,false); -
    – Erdi İzgi
    Aug 12, 2014 at 20:28
6

TypeScript, as JavaScript, doesn't have a main function. The code is executed from top to bottom, so you can just create the function and call it at the end of the main file, after the imports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.