Typescript and it's Features

Typescript and it's Features

Introduction

Hello Again! Lets introduce ourselves to Typescript - strongly subjected as a superset of Javascript. To start with it, lets first understand the most premium question - Why Typescript ?

Typescript is an open source OOPs based language which contains all the features of Javascript and the code ultimately compiles to Javascript. It can be inferred to as ES6 version of Javascript with additional features. Some of the advantages of using Typescript are :

  • It enhances the code quality and understandability, which helps in large scale javascript application development.
  • It catches the errors at the compile time rather than waiting it for runtime which helps in increasing the agility while code refactoring.
  • It supports all of the object oriented programming features such as class, interface, inheritance etc.

Now lets dive directly into the implementation of types and how to use them.

Types by Inference

In Typescript, sometimes you wouldn't need to define the types, it will rather be inferred automatically. For instance consider this example below -

let name = "space";
// let name : string

name = 123;
// Compile-time Error: Type 'number' is not assignable to type 'string'

Here typescript has already inferred the variable as a string and now if we try to assign a value of other types, it will throw an error.

Explicitly Defining Types

When we define the type of a variable, value of that type can only be assigned to it. See the below example -

let name : number;
name = "space" 
// Compile-time Error: Type 'string' is not assignable to type 'number'

Since we have already told typescript to keep the variable as a number, it will not try to infer it's type from the first value assigned. This is an explicit type.

Using type and interface

To explicitly describe the type of something, interface and type keywords can be used. For example, lets consider a Person Object which we want to have two properties name : string and age : number.

Using type keyword

type Person = {
    name: string;
    age: number;
  }

Using interface keyword

interace Person = {
   name: string;
   age: number;
 }

Note You can choose freely between both the ways of defining a type, but the only difference is that types are not extendable whereas interface can be extended to add new types and properties.

Union Types

You can also describe the type of something as a union of two types by using the | operator. In that case, typescript will accept and compile either of the mentioned type of values assigned. Lets look at another example -

let age : number | string;
age = 17; // valid
age = "seventeen" // valid

Brushing up the types

Lets mix everything up and define a type for questions object and see how it goes.

Objective :

  • We want to define type for a questions array which which consists of multiple questionObject.

  • Each questionObject will have following properties

      - question
    
      - points
    
      - answer
    

Solution :

  • Lets first go with the questionObject, here is how we will do it.
      type questionObject = {
            question: string;
            points: number;
            answer: string;
        }
    
  • Now Since questions, is an array of questionObject, we will simply define it as
     let questions: questionObject[];
    

Conclusion

Typescript is awesome. It is robust, optimized and highly scalable. Many big companies have started using typescript and it is really helping developers around the world to debug the codebase easily. Hope this article helps you in understanding the basic implementation of Typescript.