Typescript Basic Syntax

All the programming languages used their own syntax, So TypeScript also has defined its own syntax, TypeScript is composed of –

  1. Modules
  2. Functions
  3. Variables
  4. Statements and Expressions
  5. Comments

To run the typescript page and convert to javascript, first locate the folder where your script file exists, then have to use tsc followed the filename, you can also provide the extension .ts after the file name but it is optional

Example :

$ tsc myfirstscript.ts

or

$ tsc myfirstscript

Note: Have to use tsc and followed by the file name to compile a typescript program, you may not use the extension .ts for that stage. The process will generate a javascript file converted from the typescript code with the same name of the file but in this case, the existence will be changed to .js

You can compile multiple typescript files also and for that you have to provide multiple file names separated by a comma, after providing tsc command.

Example:

$ tsc myfirstscript.ts, mysecondscript.ts, mythirdscript.ts

Now you can run the generated javascript file from your command line also, and for that, you have to write node and then followed by the file name, here the uses of extension with the javascript file name are also optional, That is if you want you can use the .js extension and if you do not want you may not use the extension, but when you are going to include the generated javascript file in your webpage, you have to use the .js extension.

Example:

$ node myfirstscript.js

or

$ node myfirstscript

Now try with a sample typescript code and run that in your command line using node and typescript compiler,

Example:

var message: string = "Hello World!";
console.log(message);

Compiler Flag

There are a few compiler flags in the typescript compiler, which are going to help you a lot if you want to change the behavior of the compiler during the execution of the typescript code.
The flags are as described below:

  1. –help
    it used to display the applicable commands and help desk.
  2. –module
    The module command or flag is used to load the external modules.
  3. –target
    The target flag helps to set the typescript version, with which you want to compile your script.
  4. –declaration
    the declaration flag or command is used to add an additional .d before your .ts extension.
  5. –remove comments
    If in your script there is many comments and you want to remove all the comments you have given in your script and you do not want to remove manually or it’s difficult to remove the comment manually, then remove the comments flag will remove all.
  6. –out
    Suppose you have created multiple numbers of typescript files and you want to make a single output file then use the out flag.
  7. –source map
    The course map flag used to generate a .map file.
  8. –module noImplicitAny
    The module noImplicitAny is used to disallows compiler from inferring any type
  9. –watch
    The watch flag is used to compile and convert into javascript if any changes made and saved in a typescript file.

Whitespace, Case sensitive and uses of the semicolon in typescript,

If you want to format your program then you can do it as typescript ignores the space, tab, or enter in a single sentence it can be said that typescript ignores whitespace.
Typescript is case sensitive or it can be said that it is different between uppercase and lower case in TypeScript.
And uses of the semicolon is not strictly mandatory in typescript, but its a good practice to use it.

Comments in Typescript:

There is mainly two types of commenting in TypeScript and they are:

  1. single-line comment

In TypeScript a single line can be commented out by providing double space before the line or at the beginning of the line.

Example:

// to make a single line comment ‘use //’

Multi-line comment:

If you want to make multiple lines comment out or from a particular position to a particular position comment out then you have to please slace and astrict (/) at the beginning from where you want to comment out and provide a gastric and slace (/) at the end of the comment out section.

Example:

/*
Use multi-line comment
Use multi-line comment
Use multi-line comment
Use multi-line comment
*/

Share The Post On -