Lesson 30 of 60 – Type Aliases
50%

Type Aliases in TypeScript

A type alias allows us to create a custom name for a type. Type aliases are useful for giving meaningful names to objects, functions, unions, arrays, and other TypeScript types.

Note: Type aliases are created using the type keyword. They make complex types easier to reuse and understand.

1. What is a Type Alias?

A type alias gives another name to an existing type.

type StudentName = string;

let name: StudentName = "Amit";

Here, StudentName is an alias for the string type.

2. Type Alias Syntax

The basic syntax is:

type TypeName = Type;

Example:

type Age = number;

let studentAge: Age = 20;

3. Type Alias for String

A type alias can represent a string type.

type Name = string;

let studentName: Name = "Rahul";

console.log(studentName);

This makes the purpose of the value clearer.

4. Type Alias for Number

We can create an alias for the number type.

type StudentAge = number;

let age: StudentAge = 21;

console.log(age);

5. Type Alias for Boolean

A boolean type can also have a custom name.

type ActiveStatus = boolean;

let active: ActiveStatus = true;

console.log(active);

6. Type Alias for an Object

One of the most common uses of type aliases is defining object structures.

type Student = {
    name: string;
    age: number;
};

let student: Student = {
    name: "Amit",
    age: 20
};

7. Reusing an Object Type Alias

Once an object type alias is created, it can be used for multiple objects.

type Student = {
    name: string;
    age: number;
};

const student1: Student = {
    name: "Amit",
    age: 20
};

const student2: Student = {
    name: "Rahul",
    age: 21
};

8. Type Alias with Multiple Properties

A type alias can describe an object with many properties.

type Student = {
    id: number;
    name: string;
    age: number;
    course: string;
};

const student: Student = {
    id: 101,
    name: "Amit",
    age: 20,
    course: "TypeScript"
};

9. Type Alias with an Array

A type alias can represent an array type.

type Numbers = number[];

const marks: Numbers = [
    80,
    75,
    90
];

console.log(marks);

10. Type Alias for String Array

We can create an alias for an array of strings.

type Skills = string[];

const skills: Skills = [
    "HTML",
    "CSS",
    "TypeScript"
];

console.log(skills);

11. Type Alias with Union Types

A type alias can represent a union of multiple types.

type ID = number | string;

let studentId: ID = 101;

studentId = "ST101";

The ID type can contain either a number or a string.

12. Type Alias with Literal Values

A type alias can restrict a value to specific literal values.

type Direction =
    "left" |
    "right" |
    "up" |
    "down";

let direction: Direction = "left";

Only the specified values can be assigned.

13. Type Alias with Optional Property

Object type aliases can contain optional properties.

type User = {
    name: string;
    email: string;
    phone?: string;
};

const user: User = {
    name: "Amit",
    email: "amit@example.com"
};

The phone property is optional.

14. Type Alias with Readonly Property

A type alias can contain readonly properties.

type Student = {
    readonly id: number;
    name: string;
};

const student: Student = {
    id: 101,
    name: "Amit"
};

student.name = "Rahul";

// student.id = 102;

A readonly property cannot be reassigned after initialization.

15. Type Alias for a Function

Type aliases can describe function types.

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

const add: AddFunction = (
    a,
    b
) => a + b;

console.log(
    add(10, 20)
);

16. Reusing a Function Type Alias

The same function type can be used by different functions.

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

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

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

console.log(
    add(10, 20)
);

console.log(
    multiply(10, 20)
);

17. Type Alias with Nested Objects

Type aliases can describe nested object structures.

type Address = {
    city: string;
    pin: number;
};

type Student = {
    name: string;
    address: Address;
};

const student: Student = {
    name: "Amit",
    address: {
        city: "Aurangabad",
        pin: 824101
    }
};

18. Type Alias for an Object with an Array

A type alias can combine objects and arrays.

type Student = {
    name: string;
    marks: number[];
};

const student: Student = {
    name: "Rahul",
    marks: [
        80,
        75,
        90
    ]
};

19. Type Alias for an Array of Objects

A type alias can describe an array containing objects.

type Student = {
    name: string;
    age: number;
};

type Students = Student[];

const students: Students = [

    {
        name: "Amit",
        age: 20
    },

    {
        name: "Rahul",
        age: 21
    }

];

20. Type Alias as a Function Parameter

Type aliases can make function parameters easier to read.

type Student = {
    name: string;
    age: number;
};

function showStudent(
    student: Student
): void {

    console.log(
        student.name
    );

    console.log(
        student.age
    );
}

showStudent({
    name: "Amit",
    age: 20
});

21. Type Alias as a Function Return Type

A function can use a type alias as its return type.

type Student = {
    name: string;
    age: number;
};

function getStudent(): Student {

    return {
        name: "Amit",
        age: 20
    };
}

console.log(
    getStudent()
);

22. Type Alias in React

Type aliases are commonly used to define React component data and props.

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

const user: User = {
    name: "Amit",
    age: 20
};

The same type can later be used for component props.

23. Type Alias for React Props

A type alias can describe the props of a React component.

type UserProps = {
    name: string;
    age: number;
};

function UserCard(
    props: UserProps
) {

    return (
        <div>
            {props.name}
            {props.age}
        </div>
    );
}

This keeps component data structured and type-safe.

24. Type Alias in React Native

Type aliases are very useful in React Native for defining component props and application data.

type User = {
    id: number;
    name: string;
    email: string;
};

const user: User = {
    id: 101,
    name: "Rahul",
    email: "rahul@example.com"
};

25. Type Alias with Intersection Types

Type aliases can combine multiple types using the intersection operator &.

type Person = {
    name: string;
};

type Employee = {
    salary: number;
};

type Worker = Person & Employee;

const worker: Worker = {
    name: "Amit",
    salary: 30000
};

26. Type Alias with Union Types

Type aliases are especially useful for giving names to union types.

type Status =
    "loading" |
    "success" |
    "error";

let status: Status = "loading";

status = "success";

This prevents unrelated values from being assigned.

27. Common Type Alias Mistake

A common mistake is confusing a type alias with a variable declaration.

// Correct

type Student = {
    name: string;
    age: number;
};

const student: Student = {
    name: "Amit",
    age: 20
};

The type keyword creates a type, while const or let creates a variable.

28. When to Use Type Aliases

Type aliases are useful when:

  • An object structure is reused multiple times.
  • A union type needs a meaningful name.
  • A function type needs to be reused.
  • An array type needs a descriptive name.
  • React component props need a clear structure.
  • React Native application data needs consistent typing.

29. Complete Practical Example

Here is a practical student management example using multiple type aliases.

type Address = {
    city: string;
    pin: number;
};

type Student = {
    id: number;
    name: string;
    age: number;
    course: string;
    marks: number[];
    address: Address;
};

const student: Student = {

    id: 101,

    name: "Amit",

    age: 20,

    course: "TypeScript",

    marks: [
        80,
        75,
        90
    ],

    address: {
        city: "Aurangabad",
        pin: 824101
    }

};

console.log(
    student.name
);

console.log(
    student.address.city
);

console.log(
    student.marks
);

30. Type Aliases Summary

Type aliases allow us to create meaningful names for TypeScript types.

type Student = {
    name: string;
    age: number;
    course: string;
};

const student: Student = {
    name: "Amit",
    age: 20,
    course: "TypeScript"
};

They make complex types reusable, readable, and easier to maintain.

📌 Key Points

  • Type aliases are created using the type keyword.
  • They give meaningful names to types.
  • They can describe object structures.
  • They can represent arrays.
  • They can represent union types.
  • They can represent function types.
  • They can contain optional and readonly properties.
  • They can describe nested objects.
  • They are useful for React component props.
  • They are very useful in React Native applications.
  • Type aliases improve code readability and reusability.

🧠 Quick Quiz

Question: Which keyword is used to create a type alias in TypeScript?