TypeScript / TypeScript Interfaces and Types

Using Type Aliases for Custom Types

This tutorial will introduce you to type aliases in TypeScript, which are a way to create custom types, and how they differ from interfaces.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers defining and using interfaces and type aliases in TypeScript for object shapes and contracts.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Image Converter

Convert between different image formats.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help