Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 19, 2021 09:34 am GMT

Understanding Typescript

A JavaScript superset, that builds up on JavaScript language, It adds new features to JavaScript, typescript cant run in the browser or even in NodeJS therefore it has a compiler that compiles typescript to JavaScript. Added features in typescript include the use of types, it also helps us be more explicit and clearer about our code.

Lets look at two code snippets one with Plain Javascript and another in typescript .
Plain Javascript

  let inputA = 1  let inputB = 2  function sum(inputA, inputB){     console.log(inputA + inputB)  }  sum(inputA,inputB)  //this will return 3  //Now what if we change the inputs to be  inputA = "1"  inputB = "2"  sum(inputA,inputB) //this will return 12

Despite the second answer being wrong, we dont get an error notification at run time. Here is when typescript comes in with its additional type feature that guarantees we get the correct answer or an error.In typescript this would be written as(you can test this code in the TS playground):

    let inputA = 1    let inputB = 2    funtion sum(inputA: number,inputB: number){     console.log(inputA + inputB)    }    sum(inputA,inputB)    //prints 3    //Now what if we change the inputs to be    inputA = "1"    inputB = "2"    sum(inputA,inputB)    //script.ts(6,9): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

Through the introduction of types in typescript, it easier to write more intuitive code. I will be writing a series of tutorial on typescript.

I will cover:

  1. Typescript Basics
  2. Typescript compiler
  3. Classes & Interfaces
  4. Advanced Typescript features such as Generics & Decorators

Stay tuned !!


Original Link: https://dev.to/bazeng/understanding-typescript-5gch

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To