Using Type Aliases for Custom Types

Tutorial 2 of 5

Introduction

This tutorial aims to provide a firm understanding of using type aliases to create custom types in TypeScript. Type aliases are a powerful feature in TypeScript that allow you to give a type a new name. This is particularly useful when working with complex types, or when you want to make your code more readable and maintainable.

What you will learn:

  • What are type aliases in TypeScript
  • How to create and use type aliases
  • The difference between type aliases and interfaces
  • Best practices when using type aliases

Prerequisites:

  • Basic knowledge of TypeScript
  • Familiarity with JavaScript data types

Step-by-Step Guide

In TypeScript, you can use the type keyword to create a type alias. Type aliases can represent primitive types, union types, intersection types, tuple types, and any other types that you'd want to reuse.

type StringOrNumber = string | number;

In the above example, StringOrNumber is a type alias that represents either a string or a number.

Tip: While type aliases and interfaces may seem similar, they have subtle differences. An interface can represent an object type, and can also be implemented by a class. On the other hand, a type alias can represent any type, not just object types.

Code Examples

Example 1: Using type alias with primitive types

type Name = string;

let myName: Name;
myName = 'John Doe'; // This is valid
myName = 123; // This will throw an error

In this example, Name is a type alias for the string type. So, myName can only be assigned a string value.

Example 2: Using type alias with union types

type StringOrNumber = string | number;

let data: StringOrNumber;
data = 'Hello'; // This is valid
data = 123; // This is also valid
data = true; // This will throw an error

In this example, StringOrNumber is a type alias for the union type string | number. So, data can be assigned either a string or a number.

Summary

In this tutorial, we learned about type aliases in TypeScript. We learned how to create and use type aliases, and how they can help make our code more readable and maintainable. We also looked at the difference between type aliases and interfaces.

For further learning, you might want to explore more complex uses of type aliases, such as with intersection types, tuple types, etc.

Practice Exercises

Exercise 1: Create a type alias for the union type string | number | boolean, and use it in a variable declaration.

Solution:

type Primitive = string | number | boolean;

let myData: Primitive;
myData = 'Hello'; // This is valid
myData = 123; // This is also valid
myData = true; // This is also valid

Exercise 2: Create a type alias for an object type with properties name (string), age (number), and isActive (boolean), and use it to declare an object.

Solution:

type User = {
  name: string;
  age: number;
  isActive: boolean;
};

let user: User = {
  name: 'John Doe',
  age: 30,
  isActive: true,
};

Tips for further practice: Try creating and using type aliases with more complex types, such as arrays, functions, and generics.