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

Provide an overload for unknown responseType #1276

Merged
merged 2 commits into from May 31, 2020

Conversation

kschat
Copy link
Contributor

@kschat kschat commented May 19, 2020

Checklist

  • I have read the documentation.
  • I have included a pull request description of my changes.
  • I have included some tests (more on why this is unchecked below).

Wrapping got is difficult with the current function overloads. The changes made in #1051 made things much better, but given a responseType of 'text' | 'buffer' | 'json' in the following call:

const responseType = 'text' as 'text' | 'buffer' | 'json';
const result = await got({
  url: 'github.com',
  responseType,
});

got will return CancelableRequest<unknown> | Request (or just unknown when awaited) instead of CancelableRequest<Response<unknown>>. This change adds an overload that catches the case where responseType isn't statically known, but we do know that a Response object will be returned.

I haven't added any tests for this PR since this is a type update and I didn't see any tests for types. Given the complexity of the types used by got, though, I'm happy to add some if desired. I've had good luck using conditional-type-checks for testing types in the past.

Related issues:
#954
#1122

source/types.ts Outdated Show resolved Hide resolved
@szmarczak szmarczak changed the title Provide overload when responseType isnt statically known Provide an overload for unknown responseType May 31, 2020
@szmarczak szmarczak merged commit b9ba18a into sindresorhus:master May 31, 2020
@szmarczak
Copy link
Collaborator

@kschat Can you also submit a test for this particular use case?

@kschat
Copy link
Contributor Author

kschat commented Jun 2, 2020

@szmarczak sorry for the delayed response. I'm happy to open another PR with a test if that's still needed.

@kschat kschat deleted the got-function-types branch June 2, 2020 14:56
@szmarczak
Copy link
Collaborator

Sure, that would be great.

szmarczak pushed a commit to jaulz/got that referenced this pull request Jul 6, 2020
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.

None yet

2 participants