Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 256
Erdi İzgi Avatar asked Aug 12 '14 20:08

Erdi İzgi


People also ask

Is there a main method in JavaScript?

Many programming languages require a special user-written function that marks the begin of the execution. For example, in C this function must always have the name main() . In JavaScript, however, such a function is not required.

What is type of function in TypeScript?

Types of Functions in TypeScript: There are two types of functions in TypeScript: Named Function. Anonymous Function.

Which items are not proper TypeScript?

Number , String , Boolean , Symbol and Object ❌ Don't ever use the types Number , String , Boolean , Symbol , or Object These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code.

What is ?: In TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .


2 Answers

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.
like image 166
Jeremy J Starcher Avatar answered Sep 18 '22 00:09

Jeremy J Starcher


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.

like image 22
Adrián Arroyo Calle Avatar answered Sep 18 '22 00:09

Adrián Arroyo Calle