TypeScript Type Alias

TypeScript Type Alias

Type alias is used for creating new name for a custom type and these type aliases are same as original type. Type alias introduced in version 1.5 and generic type alias introduced in version 1.6. To create a type alias use type keyword

Syntax : type custom_type_name = “valid typescript type”

Alias for primitive type

  

        type type_str= string; // create the string type alias
        type type_num= number // create a number type alias
        type type_bool = boolean // create a boolean type alias
        type type_arr = Array<number>
            // create a array type alias

            let  str1 : type_str ;
            str1 ='John Doe'
            let num1 :type_num =1001;
            let bool1:type_bool =true;
            let arr1 : type_arr =[101,102,103,104]



            console.log(str1);      // John Doe
            console.log(num1);      // 1001
            console.log(bool1);     // true
            console.log(arr1);      //[101,102,103,104]
    

Type Alias for object type

  

        // create a alias type of object
        type Employee={empCode:number,name:string}

        // create a variable of Employee

        let employeeObj : Employee;

        // assign the value to employeeObj

        employeeObj ={
        empCode:10001,
        name:'John Doe'
        }

        console.log(employeeObj);
    

JavaScript complied code:

  

        // create a variable of Employee
        let employeeObj;
        // assign the value to employeeObj
        employeeObj = {
        empCode: 10001,
        name: 'John Doe'
        };
        console.log(employeeObj);
    

Type Alias for Union

  

        // create a type alias which can hold string and number values
        type CouponCode =string |number

        let couponCode_variable :CouponCode;

        couponCode_variable ='COUPON1';
        console.log(couponCode_variable);  //COUPON1


        couponCode_variable =100001;
        console.log(couponCode_variable); // 100001

    

JavaScript compiled code

  

        let couponCode_variable;
        couponCode_variable = 'COUPON1';
        console.log(couponCode_variable);
        couponCode_variable = 100001;
        console.log(couponCode_variable);
    

Type Alias for Function

  

        // create a type alias for function
        type addTwoNumberAlias=(x:number,y:number )=> number ;

        // create a  type alias variable

        let addTwoNumber : addTwoNumberAlias;

        // assign the function

        addTwoNumber = function(x:number,y:number): number {

        return x+y;
        }

        // call the function

        console.log(addTwoNumber(10,5));  // 15
        console.log(addTwoNumber(5,5));  // 10
    

JavaScript compile code

  

        // create a  type alias variable
        let addTwoNumber;
        // assign the function
        addTwoNumber = function (x, y) {
        return x + y;
        };
        // call the function
        console.log(addTwoNumber(10, 5));
        console.log(addTwoNumber(5, 5));
    

Interfaces vs. Type Aliases

 

  1. Interfaces create a new name, type aliases don’t
  2. Type aliases cannot be extended or implemented from