Type Parameters for Types
What
Type parameters are not limited to functions; they can also be used to create generic interfaces and type aliases.
How
You add the angle bracket notation to the type or interface name:
interface ApiResponse<T> {
data: T;
status: number;
}
type UserResponse = ApiResponse<{ id: string; name: string }>;
Why
Generic types are fundamental for building consistent data structures across an application. For example, almost every API response follows a similar structure (data, error, timestamp). By using a generic ApiResponse<T>, you can define the wrapper once and reuse it for every specific data type your API returns.
Summary
Use generic types to create consistent wrappers and containers for varying data structures.