Back to all reviewers

Type-safe API interfaces design

axios/axios
Based on 5 comments
TypeScript

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.

API TypeScript

Reviewer Prompt

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:

  1. Use explicit generic parameters with meaningful names
  2. Prefer unknown over any for better type safety
  3. Use intersection types when combining interface properties
  4. Avoid string literals when specific types are available

Example:

// โŒ 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.

5
Comments Analyzed
TypeScript
Primary Language
API
Category

Source Discussions