Open
Description
openapi-fetch version
0.14.0
Description
If I have some generated types that look like this:
responses: {
/** @description successful operation */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Pet"];
};
};
/** @description Invalid ID supplied */
400: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description Internal server error */
500: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["OtherError"];
};
};
};
And I have some code where I want to return the responses for 200 and 400 results, but not 500s (I'll throw those), If I do something like this:
type GetPetResponse = components['schemas']['Error'] | components['schemas']['Pet']
async function getPetById(id: number) : Promise<GetPetResponse>{
const result= await client.GET("/pet/{petId}", {
"params": {
"path" :{
"petId": id
}
}
});
if (result.error) {
if(result.response.status === 400){
// TypeError here - the type is not narrowed to just the 400.
return result.error;
}
throw new Error("unhandleable error")
}
return result.data;
}
Reproduction
https://github.com/dwjohnston/openapi-typescript-example/tree/response-errors
Note on line 23 of app.ts the type error.
Expected result
There should not be a type error.
The type should have narrowed to just the types available for 400 responses.
Extra
- I’m willing to open a PR (see CONTRIBUTING.md)