Simplify TypeScript code Maintenance with Type Aliases

Tomasz Ducin
InstructorTomasz Ducin
Share this video with your friends

Social Share Links

Send Tweet
Published 6 months ago
Updated 2 months ago

Type Aliases are an easy way to declare that you are treating a primitive type in your application differently in certain instances than others.

For instance, a Money type that is type string tells you where ever you are using Money to treat the value a certain way even though at the end of the day it is just a string.

Let's explore Type Aliases in this lesson.

[00:00] Create an alias by simply creating a new type that relates to one of the existing types in TypeScript, such as string or number. Now, analyze the data types and the models that you're working with within your application and try to identify the types of the properties, their representation [00:20] that might change in the future. In our example, we've got an employee entity and a human resource management application, and each entity has a salary. Right here, right now, the system doesn't differentiate various currencies. But if the business requirement comes to figure out what are the different currencies, what are the different amounts in their [00:40] exchange rates, this number would have to be changed. So an example of a useful type alias would be simply money which is represented by a number and what is important, we are going to use the type money throughout our entire codebase. And of course we're going to replace the salary number with salary money. [01:00] It's important to know that applying the type alias doesn't change anything when it comes to type compatibility. So salary used to be a number and after applying the alias, it still is a number. So nothing changes when it comes to type compatibility and any type of completion errors. However, the whole [01:20] point of creating the type alias is to have one definition that we have to change if the type of the expression would be altered by business requirements. So that if we want to see where is a certain number type being used, then we would see multitude, 100, or even thousands of places where [01:39] different numbers with different meanings would be found. And that would make changing the type in certain interfaces, types and functions way more difficult. And what we need to do after applying the alias is simply whenever something applies to money, we just consistently [01:59] use the money type, which we're going to, of course, import. And from now on, we simply stick to all the places which use a certain type alias. We know exactly how many places use the specific alias. We know where they are. We can analyze them [02:19] and roughly estimate how difficult would it be to alter the type according to business requirements. Another example for a type alias would be the skill of a certain employee. If we take a look at some example data that we've got in our repo, an employee would have skills which are basically some [02:39] programming skills like MVC data structures, JavaScript, Visual Studio, TypeScript, jQuery, whatever. So right now, they're simply defined as a string. So either an employee has a skill or not. But we can easily imagine that in an HR application, that could evolve into [02:59] a skill plus the level or number of years of experience in a certain skill. And again, we don't want to have multiple places to update the skill, and we just don't want to use the primitive representation, which is a string. And so we simply create yet another skill type alias, [03:19] which initially is going to be a string. So initially, this is all our investment, but in a case where we need to modify what is the representation of a skill, we have simply one declaration that we could alter.