Skip to main content

Typescript Basics

Typescript language is a wrapper around javascript language. Any javascript code can also be a typescript code, but typescript has additional features like Strong Typing, Object Oriented features, Catching errors during compile time.

In Angular applications, all javascript code is generally written in typescript. Angular CLI under the hood compliles the typescript file into javascript before bundling in the deployable package.

We can individually learn Typescript without using Angular CLI by downloading Typescript using npm (node package manager).

Basic commands before working on typescript:

 ---------------------------------------------------------------------------------
 Command used to install typescript                         
 ---------------------------------------------------------------------------------
 Use ctrl+~ to open the terminal window in visual   
studio code                                                               
  
 Use command : npm install -g typescript               
 g --> global (installs typescript globally in your laptop   
 -------------------------------------------------------------

 ---------------------------------------------------------------------------------
 Command used to compile typescript                         
 ---------------------------------------------------------------------------------
 Use ctrl+~ to open the terminal window in visual studio code
  
 Use command : tsc main.ts                                   
 This command compiles and produces a javascript file named  main.js                                             
 -------------------------------------------------------------

 ---------------------------------------------------------------------------------
 Command used to compile and run typescript                          
 ---------------------------------------------------------------------------------
 Use ctrl+~ to open the terminal window in visual studio code

 Use command : tsc main.ts | node main.js                   
 This command will compile the main.ts file and will execute  the javascript file main.js   
---------------------------------------------------------------------------------

Variable Declaration:
In Javascript, variables are declared using the keyword - var. In ES2015 javascript standard, JS introduced a new variable declaration using the keyword - let.

The main difference in using variable declaration with 'var' and 'let' is that 'var' has scope to nearest function and 'let' has block scoping.

Type Assertion:
This is mainly used to assert the type of the variable so that intelli sense in Visual Studio Code (IDE) can provide the functions associated with that variable.

Arrow Functions : Same as lambda functions in java.

Object Oriented features of typescript:

In type script we cannot have multiple constructors. Solution is to make the constructor parameters optional. If one constructor parameter is made optional, then all the parameters following it should be made optional.

Eg :

class Point {
let x: number;
let y: number;


constructor(x?:number, y?:number){ --> If we keep a question mark, this means that x is optional)
this.x = x;
this.y = y;
}

}

Access modifiers : Public, private, protected. All the members of the class are by default public.

If we prefix the access modifier in the constructor, typescript will create the variables and also assign them. See the below code.

class Point {
//No need of declaring the variables

constructor(private x?:number, private y?:number){ --> If we keep a question mark, this means that x is optional)
//no need of assigning using this keyword
}
}

Modules in typescript:

If you want to use typescript file outside this file then we need to add export before class :

export class Point {
let x: number;
let y: number;


constructor(x?:number, y?:number){ --> If we keep a question mark, this means that x is optional)
this.x = x;
this.y = y;
}

}

For importing the above file in any other ts file, use below statement
import {Point} from "./path"

Thanks to Mosh for Angular youtube video : https://www.youtube.com/watch?v=k5E2AVpwsko



Comments

Popular posts from this blog

Outsystems Tips and Tricks : Adding iFrame in Reactive Web App

In Outsystems Version 11+, there is no iFrame widget available when you are building Reactive Web Applications. So in case you want to add an iFrame HTML tag or for that matter any other HTML element, you could do so by creating a generic web block and pass HTML in to it by doing as follows.

Consuming Workday WSDL into Outsystems

WSDL (Web Service Description Language) is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information. The operations and messages are described abstractly, and then bound to a concrete network protocol and message format to define an endpoint . Generally consuming a WSDL (Webservice Description Language) in Outsystems is an easy task but it might get trickier when the WSDL has unsupported use cases built into it. This article will help to understand the difficulties associated with such unsupported use case and attempts to provide a solution. Here is the list of unsupported SOAP use cases from Outystems: https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/SOAP/Consuming_SOAP_Web_Services/Unsupporte d_SOAP_Use_Cases#list-attribute-in-a-single-list-attribute Workday WSDL: The following link provides the WWS (Workday Web Services) API Documentation f...

How to take Full Page Screenshot using Google Developer Tools in Chrome Browser

Click on 3 vertical dots on the top right hand side of the browser. Click on 3 vertical dots again in the Developer Tools Menu and choose "Run Command" option or use keyboard combination ctrl+Shift+P. If you do not see Developer Tools you might have to go to chrome settings and enable this option in your chrome browser. Now type "Capture Full Page Screenshot" in the command bar. Thats it. You should now see the whole page you are viewing will be downloaded automatically in your PC as a PNG file.