TypeScript
/Intermediate
Typing API Responses
Definition
Creating interfaces that mirror the exact JSON structure returned by backend APIs to ensure frontend safety during data fetching.
Explain Like I'm New
When you call `fetch()`, TypeScript has literally no idea what data comes back over the internet. It assumes it is `any`. If you don't explicitly type the response, you lose all the benefits of TypeScript the second your app connects to the real world.
Real World Example
Typing a paginated API response: `{ page: number, total: number, data: User[] }` and casting the `fetch` result to this type.
Common Use Cases
- •Axios integration
- •Fetch API
- •React Query data typing
Interactive Example
Loading Code Playground...
Interview Questions
basic
- What does `fetch().json()` return by default in TypeScript?
intermediate
- How do you apply a type to an Axios response?
advanced
- What is the danger of manually typing API responses?