Table of contents
1.
Introduction
2.
Type alias
2.1.
Syntax
3.
Why do we use Type Alias?
3.1.
Without type Alias
3.2.
With Type Alias
4.
Custom Alias
4.1.
Output
5.
FAQs
6.
Key Takeaways:
Last Updated: Mar 27, 2024

TypeScript Type Alias

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We might want to use a type of variable more than once in a code, but we cannot mention the type always. Then how do we handle this situation? This question brings us to the topic of the article Type Alias. Let’s explore what Type Alias is how to use it.

Type alias

Type Alias allows you to create a new type from the existing type. We use type aliases in a similar way; we use objects and unions by directly writing them in type annotations.

In other words, the type Alias is a new name for any existing type. To create the type Alias, use the type keyword before the name we give to the type.

Syntax

type aliasName = type;

type value = string;
let name: value; // string type


In the above code, the variable value is of a type string and is a Type Alias. The variable name is also a type string as the value is also of a type string. 

Why do we use Type Alias?

The Type Alias is used throughout the code to mention a type annotation to the variable instead of repeating the original type frequently. Whenever we want to use a variable that can accept values of multiple types, we declare the type always after a variable. This makes our code reusable and flexible. Let’s look at the detailed code for how this is done.

Without type Alias

var value: string | number; 
var name: string | number;


In the above code, we declared variables without Type Aliases. But In this case, we have to declare a type every time to make our variable a union type, which increases redundancy and complexity.

With Type Alias

type value = string | number; 

var name: value = ”coding ninjas”; // type string
var ID: value = 29; // type number
var valid: value = false; // type boolean


In the above code, instead of declaring type annotations every time we declare a variable, we created a Type Alias value and declared other variables using it. 

The variable name and ID are valid and accepted as we assigned the valid types to them. But the variable valid is not accepted as we assigned a boolean value to it.

Custom Alias

Type Aliases are used to declare not only variables, but also objects and unions. We can also create a custom type of alias using the type keyword. The syntax is similar to the object.  Let’s create a custom alias to see how it works.

type data = { name: string, ID: number };
let info: data;
info = {
  name: 'coding ninjas',
  ID : 10
};
console.log(info);


In the above code, 

  1. we created a custom alias with variables name of type string and ID of type number.
  2. We declared a variable info with the custom alias type annotation data.
  3. We assigned values to the variables name and ID as “coding ninjas” and 10 inside the variable info.

Output

[LOG]: {

  "name": "coding ninjas",

  "ID": 10

}

FAQs

  1. What is a type alias in TypeScript?
    In typescript, we can create a type variable that can be reused throughout the code instead of using the existing types always. This technique is called Type Alias. This creates a new name for existing types.
     
  2. How do I create an alias in TypeScript?
    We can create a Type Alias in TypeScript by declaring a variable with type before and a type annotation after it. The syntax is as follows:
    type name = string;
     
  3. What happens when we assign a value not supported by Type Alias?
    If we assign a value of a type that is not supported by the Type Alias, the compiler shows us an error.
    Type 'typeName' is not assignable to type 'typeAliasName'

Key Takeaways:

Let’s discuss the Type Alias again in brief to get a better understanding,

  • Type Alias allows you to create a new type from the existing type. To create the type Alias, use the type keyword before the name we give to the type.
  • The Type Alias is used throughout the code to mention a type annotation to the variable instead of repeating the original type frequently.
  • Using Type Aliases to declare variables with multiple types or union types gives better efficiency and reduces redundancy of the code.
  • We can also create a custom type of alias using the type keyword. The syntax is similar to the object.
     

Hey Ninjas! Isn't TypeScript an interesting Scripting language? So to learn more about typescript, enroll in the best web development course we have found just for you. 
 

Happy Learning!

Live masterclass