Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

TypeScript Type Aliases

Learn how to create reusable names for object structures, primitive types, union types, function types and other TypeScript type expressions.

Simple Explanation: A type alias gives a name to an existing type or type expression. It helps make complex types easier to read, reuse and maintain.

1. What is a Type Alias?

A type alias is created using the type keyword. It gives a meaningful name to a type that can be reused throughout a TypeScript application.

Type aliases can describe primitive types, object structures, union types, intersection types, tuples and function signatures.

Basic Syntax

type StudentName = string;

const studentName: StudentName = "Rahul";

console.log(studentName);

Expected Output

Rahul

Here, StudentName is an alias for the string type. The variable studentName therefore accepts a string value.

2. Type Alias Workflow Diagram

Understand how a type alias is declared, reused and checked by TypeScript.

📝
Define Type

Create a type alias using the type keyword.

→
🏷️
Give a Name

Assign a meaningful name to the type expression.

→
📦
Reuse Type

Use the alias for variables, parameters or return types.

→
✅
Type Checking

TypeScript checks values against the aliased type.

Remember: A type alias provides a reusable name for a type. It does not create a new runtime class or value by itself.

3. Type Alias for Primitive Types

You can create an alias for primitive types such as string, number and boolean. This can make domain-specific values easier to understand.

Example

type EmployeeName = string;
type EmployeeSalary = number;
type IsActive = boolean;

const name: EmployeeName = "Priya";
const salary: EmployeeSalary = 40000;
const active: IsActive = true;

console.log(name);
console.log(salary);
console.log(active);

Expected Output

Priya
40000
true

These aliases still represent the original primitive types. For example, EmployeeSalary is an alias for number, not a separate runtime type.

4. Type Alias for Object Structures

A type alias can describe the properties an object should contain. This is useful when the same object structure is needed in several places in an application.

Example: Employee

type Employee = {
    id: number;
    name: string;
    department: string;
    salary: number;
};

const employee1: Employee = {
    id: 101,
    name: "Amit",
    department: "IT",
    salary: 35000
};

console.log(employee1.name);
console.log(employee1.department);
console.log(employee1.salary);

Expected Output

Amit
IT
35000

Object Type Workflow

📘
Employee Type

Defines id, name, department and salary.

→
👨‍💻
Employee Object

Supplies values for the required properties.

→
🔍
Validation by TypeScript

Checks that property names and types are compatible.

5. Type Alias for Union Types

A union type allows a value to belong to one of several specified types. A type alias gives that union a reusable name.

Example

type Identifier = string | number;

const studentId: Identifier = 101;
const employeeId: Identifier = "EMP102";

console.log(studentId);
console.log(employeeId);

Expected Output

101
EMP102

The Identifier alias allows either a string or a number. A boolean value would not be accepted by this type.

Note: The full topic of union types, including type narrowing, is covered in the next pages of this module.

6. Type Alias for Function Types

A type alias can describe a function's parameters and return type. Functions assigned to that type must have a compatible signature.

Example

type Operation = (a: number, b: number) => number;

const add: Operation = (a, b) => {
    return a + b;
};

const multiply: Operation = (a, b) => {
    return a * b;
};

console.log(add(10, 5));
console.log(multiply(10, 5));

Expected Output

15
50

How It Works

📝
Operation Type

Requires two number parameters and a number result.

➕
Add Function

Implements addition using the required signature.

✖️
Multiply Function

Implements multiplication using the same signature.

7. Type Alias for Tuple Types

A tuple represents an array-like structure with a specified number of elements and a type for each position. A type alias can give that tuple structure a meaningful name.

Example

type StudentRecord = [number, string, boolean];

const student1: StudentRecord = [101, "Rahul", true];

console.log(student1[0]);
console.log(student1[1]);
console.log(student1[2]);

Expected Output

101
Rahul
true

The tuple contains three positions: a number, a string and a boolean. The order and types of the elements matter.

8. Type Alias for Intersection Types

An intersection type combines requirements from multiple types. A type alias can name the resulting combined type.

Example

type Person = {
    name: string;
};

type EmployeeInfo = {
    employeeId: number;
};

type Employee = Person & EmployeeInfo;

const employee1: Employee = {
    name: "Priya",
    employeeId: 501
};

console.log(employee1.name);
console.log(employee1.employeeId);

Expected Output

Priya
501

The Employee alias combines both requirements. An Employee value needs the properties from Person and EmployeeInfo.

Note: Intersection types are explained in detail in the Intersection Types page.

9. Type Alias vs Interface

Both type aliases and interfaces can describe object structures. However, type aliases can also represent unions, intersections, primitives, tuples and function signatures.

Feature Type Alias Interface
Keyword type interface
Object structures Supported Supported
Primitive aliases Supported Not used to alias primitive types
Union and intersection types Can name these type expressions Cannot directly declare a union or intersection as an interface
Extending object structures Can use intersections Can extend other interfaces
Declaration merging Duplicate type alias declarations generally cause an error Compatible declarations can merge

10. Advantages of Type Aliases

Reusability

Define a type once and reuse it across variables, functions and objects.

Readability

Give meaningful names to complex type expressions.

Type Safety

Help TypeScript identify incompatible values during development.

Flexible Type Definitions

Name object types, unions, intersections, tuples and function signatures.

Summary

A type alias is declared using the type keyword. It can name primitive types, object structures, union and intersection types, tuples and function signatures. Aliases make type definitions more reusable and easier to understand.

Next, learn Union Types and how a variable can accept one of several specified types safely.