When working with TypeScript, I am fetching data that has different types:
type SuccessfulResponse = {
status: 1;
data: string;
}
type FailedResponse = {
status: 0;
error: string;
}
type ApiResponse = SuccessfulResponse | FailedResponse
After getting the data, I attempt to verify the response in this manner:
const response = await fetchApi() // returns type ApiResponse
if (!response.status) return
const data = response.data
// The compiler shows an error: Property 'data' does not exist on type 'SuccessfulResponse | FailedResponse.....
However, if I check for response.status === 0
it behaves as expected.