Skip to main content

Posts

Showing posts with the label TypeScript

TypeScript Decorators

Decorators In order to use decorators in our code, make sure that in tsconfig file, "target": "es5",   ,"experimentalDecorators": true, function   Logger ( constructor :  Function ){      console . log ( "Logging..." );      console . log ( constructor ); } @ Logger class   Person  {      name  = "TEJA" ;      constructor (){          console . log ( "creating a person object..." );     } } const   pers  =  new   Person (); console . log ( pers ); Factory Decorator: From the above example, In Logger function, we have used one more constructor function and call it via decorator. Therefore it help to call the class by default in app.ts and also we can call other set of functions on load. function   Logger ( logString :  string...

TypeScript Generics

Generics: Generic is a type which connected to other type, where we don't know what type of data it will return //Generics const   names  : Array < string > = []; //which is similar to string array as const names: string[] Promises: Anothor example is promise method , which allows to two arguments in a method and return string type array. Promise is a built in generic type in typescript const promise = new Promise() // which is a constructor function. Syntax example const   promise :  Promise < string > =  new   Promise (( resolve ,  reject )  =>  {      setTimeout (()  =>  {          resolve ( "This is my code" );     }, 1000 )      }); promise . then ( data   =>  {      data . split ( ' ' ); }); ...

TypeScript Classes & Interfaces

Objects: real time objects we are doing online shopping product list method to add to card etc...add product, delete product, edit product -  this product works with rendering, storing date etc. each piece is object having certain functionality Objects are instances of the classes. Classes: Classes are blueprint to the objects. Classes holds the objects and methods they have. //First class class   Department  {      name :  string ;      constructor ( n :  string ){          this . name  =  n ;     } } const   accountName  =  new   Department ( "Accounts" ); // here Department is the blueprint to the object console . log ( accountName ); Constructor Functions and this key word class   Department  {      name :  string ;      constr...

TypeScript Compiler

TypeScript: https://tejasadventure.blogspot.com/2019/12/typescript.html Watch mode Watch mode will always compile the code with automatic saving the file in VS code. tsc --init // when we hit, it tells the typescript to be create a config.json file and automate to save the code and show compilation errors. Let Vs const const variable values cannot be changed. let is the key word where the variable will be available in that block. Arrow function //Arrow function const   adding  = ( a : number ,  b : number )  =>  {      return   a  +  b ; }; // the below example doesn't require return, which means direct implement notation doesn't require return const    substraction  = ( a : number ,  b : number )  =>   ( b  -  a ); console . log ( adding ( 2 , 5 )); Sp...

TypeScript

Visual studio code extensions useful for typescript coding. Path intellisense ESlint Material theme icon Prettier-code form How to run typescript application in local server? What ever the changes we made in our code will automatically reflect in the browser, For this setup, we need a local server make it ready in our local machine that can run on local browser. We have to install lite-server in machine from NPM and servers us dynamic changes in code reflect in the browser of local machine. Use the below commands for the setup. node -v npm -v npm install npm@latest -g npm init --yes npm install lite-server --save-dev install tsconfig.json  tsc --init. NOTE: const variable values cannot be changed. let is the key word where the variable will be available in that block. But let support for all the types String[] // means array of strings are storing in a variable Any[] //means flexible for any types of array publ...