DEV Community

Cover image for TypeScript With React
Nur Nafish Ahmed Nobel
Nur Nafish Ahmed Nobel

Posted on

TypeScript With React

Why we use TypeScript!
TypeScript is a programming language developed and maintained by Microsoft. It introduces additional features like strict type binding (variables are bound to specific data types) to JavaScript and can also be compiled down to JavaScript as well.
If we want a large-scale project that time TypeScript is helping us to reduce code error.
And
Secondly, TypeScript means the defined type before declaring.
*Without defining Type *

function addTwo(num)  
{  
  return num + 2;  
}  

addTwo(2); // Returns 4;  

addTwo("Two"); // Returns "Two2"  
Enter fullscreen mode Exit fullscreen mode

** Typing**

function addTwo(num : number)    
{    
  return num + 2;    
}    

addTwo(2); // Returns 4;    

addTwo("Two"); // Compile Error
Enter fullscreen mode Exit fullscreen mode

Implementation of Interface in ts file

interface Vehicle{    
  wheels: Number;    
}    

class Car implements Vehicle {    
   public wheels: Number;  
   public hasTrunk: Boolean;  
}   

class MotorCycle implements Vehicle{  
   public wheels : Number;  
}
Enter fullscreen mode Exit fullscreen mode

TypeScript Explained Further

TypeScript has two different roles depending on its configuration and usage.

  1. TypeScript as a linting layer on top of JavaScript TypeScript supports a more robust interaction with the editor by adding additional syntaxes to JavaScript with type checking and code completion. Also, it helps catch errors easier in the early development phase.
  2. TypeScript as a Transpiler Transpiler is a tool that converts a code written in one programming language into another language with a more similar abstraction level. TypeScript has its own transpiler, which can convert .ts files into .js, making it possible for browsers to execute your code.

Image description

Top comments (0)