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,
- we created a custom alias with variables name of type string and ID of type number.
- We declared a variable info with the custom alias type annotation data.
- 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
-
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.
-
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;
-
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!