- Abstract class
- Annotations
- Array
- Asserts
- Class
- Const
- Decorators
- Default parameter
- Dictionary
- Enum
- For loop
- forEach()
- Function
- Generics
- Index signature
- Infer
- Inheritance
- Interface
- Let
- Map type
- Mixin
- Module
- Namespace
- Never
- Object type
- Operator
- Optional parameter
- Promise
- Property
- Tuples
- Type alias
- Type guard
- Type narrowing
- Union
- Utility types
- Var
- Void
TYPESCRIPT
TypeScript Type Alias: Syntax, Usage, and Examples
A TypeScript type alias gives you a way to name a type, making complex type definitions easier to understand and reuse. Instead of repeating a structure multiple times, you can define it once and use it wherever you need it.
How to Use Type Alias in TypeScript
To create a TypeScript type alias, use the type
keyword followed by the name of the alias and the type definition:
type UserID = string;
type Coordinates = { x: number; y: number };
You can then use UserID
or Coordinates
like any other type:
const userId: UserID = "abc123";
const point: Coordinates = { x: 5, y: 8 };
Type aliases in TypeScript are flexible—you can create aliases for primitives, objects, arrays, unions, intersections, functions, and more.
When to Use TypeScript Type Aliases
Simplifying Complex Types
Long or deeply nested types can clutter your code. TypeScript type alias syntax lets you shorten and reuse them.
type Product = {
id: number;
name: string;
price: number;
tags?: string[];
};
Now use Product
in function signatures or variables without rewriting the shape.
Reusing Types Across Your Project
If you use the same shape in different parts of your code, define it once with a type alias. This improves consistency and makes updates easier.
type Address = {
street: string;
city: string;
zip: string;
};
Now both your billing and shipping forms can use Address
.
Representing Unions or Intersections
TypeScript type alias syntax works great for combining multiple types:
type Status = "pending" | "approved" | "rejected";
type Action = {
type: "CREATE" | "UPDATE" | "DELETE";
payload: any;
};
These make your intentions clear and keep logic type-safe.
Examples of Using Type Alias TypeScript Syntax
Union Type Alias
type Role = "admin" | "user" | "guest";
function redirectUser(role: Role) {
if (role === "admin") {
// send to admin dashboard
}
}
This limits the function to specific role values.
Function Type Alias
type Logger = (msg: string) => void;
const logInfo: Logger = (message) => {
console.log("INFO:", message);
};
You avoid repeating the function signature each time.
Object Alias with Optional Properties
type Settings = {
theme: "light" | "dark";
notifications?: boolean;
};
This is especially useful when modeling UI preferences or configurations.
Type Alias for Arrays
type Usernames = string[];
const names: Usernames = ["Alice", "Bob"];
Much cleaner than repeatedly writing string[]
.
Learn More About TypeScript Type Aliases
Combining Types with Intersections
You can use the &
operator to create more specific types:
type Name = { first: string; last: string };
type Contact = { email: string; phone: string };
type Person = Name & Contact;
const p: Person = {
first: "Jamie",
last: "Doe",
email: "jamie@example.com",
phone: "555-1234",
};
This works well when extending types in a modular way.
Difference Between Type Alias and Interface
One of the most common questions is: type alias vs interface TypeScript—what’s the difference?
Similarities:
- Both can describe object shapes.
- Both support extending and merging to some degree.
Key Differences:
- You can use a type alias for any type, including unions, intersections, and primitives. Interfaces only work for object types.
- Interfaces support declaration merging. If you declare an interface twice with the same name, TypeScript merges the definitions. Type aliases don’t merge—they throw an error.
Example:
interface Box {
width: number;
}
interface Box {
height: number;
}
const b: Box = { width: 10, height: 20 }; // Valid due to merging
You can’t do this with a type alias.
In general, use interface
for objects you expect to extend or merge. Use type
when you need unions, tuples, or function types.
Naming Conventions
By convention, type aliases in TypeScript use PascalCase:
type OrderStatus = "pending" | "shipped" | "delivered";
type Point2D = { x: number; y: number };
This helps distinguish them from variables and values.
Using Utility Types with Type Aliases
You can combine type aliases with built-in utility types like Partial
, Pick
, or Omit
:
type Product = {
id: number;
name: string;
price: number;
};
type ProductPreview = Pick<Product, "id" | "name">;
This approach keeps your code DRY and consistent.
Nested Type Aliases
You can use one alias inside another to build complex shapes:
type Author = {
name: string;
email: string;
};
type BlogPost = {
title: string;
content: string;
author: Author;
};
This is great for structuring APIs and UI models.
Advanced Use of Type Alias in TypeScript
Conditional Types
You can use type aliases to build conditional logic:
type IsString<T> = T extends string ? "Yes" : "No";
type Test = IsString<number>; // "No"
type Test2 = IsString<"hello">; // "Yes"
Useful in utility types or generic helpers.
Template Literal Types
Combine strings at the type level:
type HTTPMethod = "GET" | "POST";
type Route = `/api/${string}`;
const route: Route = "/api/user";
Recursive Types
Type aliases can even be recursive:
type Comment = {
message: string;
replies?: Comment[];
};
This is perfect for nested data like comments, folders, or menus.
Best Practices for Type Alias TypeScript Usage
- Use aliases for clarity, not just brevity.
- Prefer aliases when working with primitives, unions, or function types.
- Use interfaces for extending or defining API contracts.
- Stick to naming conventions to keep types readable.
- Document complex aliases so other devs (or future you) don’t get lost.
A TypeScript type alias helps you write cleaner, more understandable code. Whether you’re simplifying function signatures, building nested data models, or representing union types, type aliases let you abstract complexity and stay consistent. You don’t need to memorize all the built-in options—just start small and build as your project grows.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.