TypeScript Advanced Types
Powerful type constructs for complex scenarios
🚀 What are Advanced Types?
Advanced Types in TypeScript are sophisticated type constructs that combine, transform, and manipulate types to create powerful, flexible, and reusable type definitions for complex programming scenarios.
// Simple advanced type example
type User = { name: string; age: number };
type Admin = User & { role: 'admin' };
const admin: Admin = { name: 'John', age: 30, role: 'admin' };
Output:
✅ Type-safe admin object created successfully
Key Advanced Type Concepts
Union Types
Combine multiple types with OR logic
type ID = string | number;
let userId: ID = 123;
userId = "abc";
Intersection Types
Combine multiple types with AND logic
type A = { x: number };
type B = { y: string };
type C = A & B;
Literal Types
Exact values as types
type Direction =
'up' | 'down' | 'left' | 'right';
let move: Direction = 'up';
Mapped Types
Transform existing types
type Readonly<T> = {
readonly [K in keyof T]: T[K]
};
🔹 Union Types
Union types allow a value to be one of several types:
// Basic union type
type Status = 'success' | 'error' | 'loading';
function displayStatus(status: Status) {
console.log(`Current status: ${status}`);
}
displayStatus('success'); // ✅ Valid
// displayStatus('pending'); // ❌ Error
// Union with different types
type Result = string | number | boolean;
let value: Result;
value = 'hello';
value = 42;
value = true;
Output:
Current status: success
🔹 Intersection Types
Intersection types combine multiple types into one:
// Combining object types
type Person = {
name: string;
age: number;
};
type Employee = {
employeeId: number;
department: string;
};
type Staff = Person & Employee;
const staff: Staff = {
name: 'Alice',
age: 28,
employeeId: 1001,
department: 'Engineering'
};
Output:
✅ Staff object has all properties from Person and Employee
🔹 Type Aliases
Create reusable custom type names:
// Simple type alias
type Point = { x: number; y: number };
// Function type alias
type MathOperation = (a: number, b: number) => number;
const add: MathOperation = (a, b) => a + b;
const multiply: MathOperation = (a, b) => a * b;
// Complex type alias
type User = {
id: number;
name: string;
email: string;
role: 'admin' | 'user' | 'guest';
};
🔹 Conditional Types
Types that depend on conditions:
// Basic conditional type
type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // true
type B = IsString<number>; // false
// Practical example
type NonNullable<T> = T extends null | undefined ? never : T;
type C = NonNullable<string | null>; // string
type D = NonNullable<number | undefined>; // number
🔹 Utility Types
Built-in advanced types for common transformations:
type User = {
id: number;
name: string;
email: string;
};
// Partial - makes all properties optional
type PartialUser = Partial<User>;
// Required - makes all properties required
type RequiredUser = Required<PartialUser>;
// Pick - select specific properties
type UserPreview = Pick<User, 'id' | 'name'>;
// Omit - exclude specific properties
type UserWithoutEmail = Omit<User, 'email'>;
// Record - create object type with specific keys
type Roles = Record<'admin' | 'user', boolean>;