Design API interfaces with strong type safety while maintaining excellent developer experience. Prefer explicit types over loose ones to enable better IDE support and catch errors at compile time.
Design API interfaces with strong type safety while maintaining excellent developer experience. Prefer explicit types over loose ones to enable better IDE support and catch errors at compile time.
Key principles:
unknown
over any
for better type safetyExample:
// โ Avoid
interface ApiConfig {
headers?: Record<string, any>;
data?: any;
}
// โ
Better
interface ApiConfig<ResponseData = unknown, RequestData = unknown> {
headers?: HeadersDefaults & RequestHeaders;
data?: RequestData;
response?: AxiosResponse<ResponseData>;
}
This approach provides better IDE autocompletion, makes the code more maintainable, and catches potential type errors during development rather than at runtime.
Enter the URL of a public GitHub repository