As a beginner in typescript, one area that I'm struggling to find information on is how to handle types for API call responses.
For example, when making a GET request to an API and receiving a JSON object like:
{
name: "john",
age: 12
}
In my code, if I want to access response.name
, do I need to define an interface like the following to avoid any linter errors?
interface Response {
name: string,
age: number
}
Or is there a simpler way to tackle this issue? Some API calls can return JSON objects with more than 10 lines of data, so creating interfaces for each type of response seems cumbersome. Another idea I had was to create interfaces with only the values I plan to use, but I'm unsure if this is the best approach. Any insights or guidance would be greatly appreciated!