DEV Community

notdennis
notdennis

Posted on

Using Type Aliases Instead Of String literals In Typescript

Article Objective

In this article, we will discuss the difference between string literals and type aliases in TypeScript. We will also explain when to use each one depending on your use case.

Introduction

Type aliases are an incredibly useful feature in TypeScript that allows developers to create reusable aliases for types such as strings and numbers. This can be particularly helpful when it comes to achieving the goal of DRY coding, as it eliminates the need to repeatedly define the same types throughout a project. In addition to this, TypeScript also offers the ability to use string literals as a type, which can further enhance the flexibility and readability of your code. By leveraging these powerful features, developers can create cleaner, more maintainable code that is easier to work with and less prone to errors or bugs.

Using string literals

In TypeScript, we have the option to use string literals to define the type of a variable. This means that instead of simply assigning a basic string value to a variable, we can specify a specific string literal as its type. For example, if we have a variable called "animal", we could define its type as the string literal "dog". This can provide more clarity and specificity in our code.


let animal:"dog"

animal = "dog"   // ok//

animal = "pig"  // compiler compalins //
Enter fullscreen mode Exit fullscreen mode

When working with code snippets, it's important to keep in mind that certain variables may have limitations on the values they can hold. In the case of the "animal" variable, attempting to assign any value other than "dog" would result in a compiler error. However, the use of string literals can be particularly helpful when paired with TypeScript union types. By defining a type that can be more than one, developers can create even more dynamic and powerful code.


let animal:"dog" |"pig"

animal = "dog"   // ok//

animal = "pig"  // compiler is happy //

Enter fullscreen mode Exit fullscreen mode

It is worth noting that when using the union type, one can accept both "dog" and "pig." However, it is important to keep in mind that literal strings cannot be employed as a type for other variables. This renders them not reusable and, therefore, not practical. To address this issue, it is recommended to use a type alias or interface instead. This will allow for greater flexibility and efficiency in coding.

Using Type ALiases

During the process of coding, developers strive to eliminate the repetition of identical blocks or lines of code. In TypeScript, a Type Alias is a useful feature that enables the creation of a new name to reference any type, such as string, number, tuple, union, function, and more. This functionality enables developers to write code more efficiently and with greater clarity, as it allows for the use of concise and meaningful identifiers instead of repeatedly typing out lengthy type definitions.

type animal = "dog" |"pig"

const pet1:animal = "dog"   // ok//

const pet2:animal = "pig"  // compiler is happy //
Enter fullscreen mode Exit fullscreen mode

You can use the type "animal" throughout your code, as opposed to only using string literals.

Referencing other types with keyword Type

type num = 1 | 2; // number

type bool = true | false; // boolean

type obj = {a: 1} | {b: 2}; // object

type func = (() => string) | (() => void); // function
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
avwerosuoghene profile image
Avwerosuoghene Darhare-Igben

This indeed offers a cleaner and easy to debug code.
I believe type aliases in combination with enums can be useful to create more descriptive and structured type definitions.