Master TypeScript From Scratch

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

TypeScript Advanced Types

Learn advanced TypeScript concepts such as Generics, Generic Constraints, Utility Types, Type Narrowing, Conditional Types and Mapped Types.

Simple Explanation: Advanced Types help developers create flexible, reusable and type-safe applications. They allow us to work with different data types, narrow possible values and build new types from existing definitions.

1. What are Advanced Types?

Advanced Types are TypeScript features that help describe more complex data structures and relationships between types.

For example, Generics allow the same function to work with different data types while preserving type information. Utility Types can transform existing types, and Conditional Types select a type based on a condition.

Example: Generic Function

function identity<T>(value: T): T {
    return value;
}

const numberValue = identity<number>(100);
const stringValue = identity<string>("TypeScript");

console.log(numberValue);
console.log(stringValue);

Expected Output

100
TypeScript

The same function works with numbers and strings while preserving the type of the value passed to it.

2. Advanced Types Workflow Diagram

Follow this workflow to understand how advanced type features help developers define, transform and safely use data.

πŸ“
Define Types

Specify the expected types and constraints.

β†’
βš™οΈ
Transform or Reuse

Use Generics, Utility Types or type expressions.

β†’
πŸ”
Type Checking

TypeScript checks values and narrows types where possible.

β†’
βœ…
Use Safely

Write reusable code with better type information.

Remember: TypeScript advanced types primarily provide compile-time checking. They do not automatically validate external API data at runtime.

3. Topics Covered in This Module

Learn each topic step by step with explanations, TypeScript code examples, expected outputs, practical applications and visual diagrams.

🧩

1. Generics

Create reusable functions, classes and interfaces that work with different types while preserving type information.

Key concept: Type parameters such as T.

πŸ”’

2. Generic Constraints

Restrict generic type parameters so they must satisfy specific requirements.

Key concept: extends in generic declarations.

πŸ› οΈ

3. Utility Types

Transform existing types into new forms without repeatedly writing the same type definitions.

Examples: Partial, Required, Pick and Omit.

πŸ”

4. Type Narrowing

Narrow a value from multiple possible types to a more specific type using checks and control flow.

Examples: typeof, in and instanceof.

πŸ”€

5. Conditional Types

Select a resulting type based on whether one type extends another type.

Key concept: Conditional type expressions.

πŸ—ΊοΈ

6. Mapped Types

Create new types by transforming the properties of an existing type.

Key concept: Mapping over property keys.

4. Why Do We Need Generics?

Without Generics, developers may write separate functions for different data types. A generic function can handle different types while maintaining type safety.

Without Generics

function getNumber(value: number): number {
    return value;
}

function getString(value: string): string {
    return value;
}

With Generics

function getValue<T>(value: T): T {
    return value;
}

console.log(getValue(100));
console.log(getValue("Hello"));

Expected Output

100
Hello
Key Point: The generic function avoids duplicate implementations and preserves the relationship between its input and return types.

5. Where Are Advanced Types Used?

API Response Handling

Use Generics to represent reusable API response structures and their data types.

Reusable Components

Build functions, classes and interfaces that work with multiple data types.

Form and User Data

Use utility types to describe optional, required or selected properties in data models.

Complex Business Logic

Use type narrowing and conditional types to represent different states and type relationships.

Summary

TypeScript Advanced Types help developers create reusable and flexible type definitions. Generics preserve type information, Generic Constraints restrict allowed type parameters, Utility Types transform existing types, Type Narrowing refines possible types, Conditional Types select types based on conditions, and Mapped Types transform property definitions.

Next, learn Generics and how to create reusable functions that work with different data types.