Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials TypeScript Generic Interfaces
TypeScript Intermediate FREE

Generic Interfaces

Lesson 2 of 17 Intermediate Interactive

Generic interfaces define flexible object shapes.

Syntax

TYPESCRIPT
interface ApiResponse<T> {
    data: T;
    status: number;
    message: string;
}

const res: ApiResponse<User> = {
    data: user,
    status: 200,
    message: "OK"
};
Generic Interfaces
TYPESCRIPT
interface ApiResponse<T> {
    data: T;
    status: number;
    message: string;
}

interface User {
    id: number;
    name: string;
}

let response: ApiResponse<User> = {
    data: { id: 1, name: "Alice" },
    status: 200,
    message: "OK"
};

console.log(response.data.name);