Different datatypes typescript supports

Let's understand different data types in typescript.

Just like any other scripting language TypeScript also support almost all data types, you find typescript data types are very similar to JavaScript and C#, here we explain few regularly used data types in typescript program development.

Assuming you are already familiar with JavaScript data type or C# data types , so we focus more on whatever new in typescript.

Basic Data Types in TypeScript

Here are some of the commonly used typescript data types

  • typescript data types String
    let companyName:string = "WebTrainingRoom"; 
    
  • typescript data type Number
    let totalCount: number = 100;
    
    let foodPrice: number = 150.40;
    
  • typescript data types Boolean
    let isComplete: boolean = true;
    
    let isDelayed: boolean = false;
    
  • typescript data type Any

    when we declare a variable as any, we can assign any type of value other than the type was initialized.

    let anyVar: any = "This is a string value"; 
        anyVar = 100;
        anyVar = true;
    
  • TypeScript Undefined
  • TypeScript Array
    let colors: string[] = ['Red', 'Blue', 'Green','White'];
    let colorList: Array<string>;
        colorList = ['Red', 'Blue', 'Green','White']; 
    
  • TypeScript Tuple data type
    var employee: [number, string, date] = [1, "Bill", ""];
    
  • TypeScript Enum data type
  • TypeScript Void
    function Welcome(): void { 
        console.log('Hi!')
    } 
    let w_message: void = Welcome(); 
    

    Actually, there is no meaning to assign void to a variable, still here is how you can do that!

    let someVar: void = undefined;
    
  • TypeScript Null data type
    let nValues: null = null;
    
  • TypeScript Never data type

Custom datatype in typescript

Like any other object oriented programming language, in typescript, we can define custom data types like examples below.

  • class

    Class is common data type often we create as business object, like "Student" is the business object in this example.

    class Student { 
        studId: number; 
        FirstName: string; 
        LastName: string; 
        FullName: string; 
      
        constructor(id: number, fullname: string) { 
            this.FullName = fullname; 
            this.studId = number; 
        } 
        getTotalMarks() : number { 
                totalMarks: number = 680;
            return totalMarks; 
         } 
    }  
    
  • interface

    In typescript, we can define interface, but here we can have optional properties in typescript interface, which was not possible in c# interface definition.

    Optional members are marked with question mark like Mobile?: number;

    interface Person {
        FullName: string;
        Email?: string;
        Mobile?: number;
        City: string;
    }
    
    
    function createEmployee(people: Person): {FullName: string; Email: string; Mobile: number; City: string} {
        let newEmp = {FullName: "Ajay Balasari", Mobile: 98000001};
        
        return newEmp;
    }
    
    

You may be interested to read following posts:

 
TypeScript Datatypes
Learn typescript programming with real-time examples.
Popular Libraries
Data types in TypeScript
TypeScript and JavaScript Examples