Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Responses are a union of possible operations response types #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RobinClowers
Copy link

Currently the data property of a response is based on the type of one success responses. This is incorrect, because different status codes may return different response types.

This is a breaking change, since working code may now cause compile errors, requiring you to narrow the type before you can use it.

Fixes #2

@RobinClowers
Copy link
Author

I think the best solution to this would if would could make the types smart enough so that you could narrow on the type, many APIs have a single response type per status. I spent some time trying to do this, but it made my brain hurt 😄.

: 'default' extends keyof T
? T['default']
: unknown
type _OpReturnType<T> = keyof T extends number | 'default' ? T[keyof T] : never
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this include all the error types as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so, that's the point of this PR, currently you can write code that doesn't handle error cases and typescript won't catch it.

@smaspe
Copy link

smaspe commented Mar 27, 2024

how do you narrow it down, though, is the status code in ApiResponse sufficient for that?

@RobinClowers
Copy link
Author

is the status code in ApiResponse sufficient for that?

This is what I was hoping to do, I have a branch where I was working on this, but I didn't get it working. As it stands now, you have to narrow the type on the response object itself, which is a bit annoying. However, I think it's much better than the current behavior where you can just ignore error cases, resulting in runtime errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ApiRepsonse data incorrectly typed for empty responses
2 participants