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

How to correctly type an instance returned by got.extend #1117

Closed
1 task done
jogold opened this issue Mar 11, 2020 · 6 comments · Fixed by #2353
Closed
1 task done

How to correctly type an instance returned by got.extend #1117

jogold opened this issue Mar 11, 2020 · 6 comments · Fixed by #2353
Labels
enhancement This change will extend Got features ✭ help wanted ✭

Comments

@jogold
Copy link
Contributor

jogold commented Mar 11, 2020

What would you like to discuss?

Given the following client:

const jsonClient = got.extend({
  responseType: 'json',
  resolveBodyOnly: true
});

Doing:

const response = await jsonClient('https://httpbin.org/json');

hints TypeScript to believe that response is of type Response<string> while it should be unknown.

How can I correctly type jsonClient to take into account my options?

Checklist

  • I have read the documentation.
@szmarczak szmarczak added enhancement This change will extend Got features ✭ help wanted ✭ labels Mar 11, 2020
@szmarczak
Copy link
Collaborator

For now you need to cast it manually.

@chinesedfan
Copy link
Contributor

chinesedfan commented May 11, 2020

In theory, it can be achieved by conditional types. See examples like sindresorhus/pify#76.

@szmarczak If you agree with this kind of mechanism, I can send a PR to handle options.resolveBodyOnly. But types related codes will require lots of refactors and become a little complex.

@maxpain
Copy link

maxpain commented Jun 16, 2020

Any news about this?

@ejmartin504
Copy link

ejmartin504 commented Aug 16, 2020

I agree that the typing should be correct here, likely by using conditional types as @chinesedfan mentioned. I'd like to point out one workaround for this specific instance of got.extend, which my team used quite effectively in our project to get better typing: use .json

From the docs:

// This
const body = await got(url).json();

// is semantically the same as this
const body = await got(url, {responseType: 'json', resolveBodyOnly: true}); 

@TkaczykAdam
Copy link

TkaczykAdam commented Nov 23, 2021

I am a newbie in the TS world, I didn't want to type it as any. For a quick workaround, I just add the redundant .json()
@szmarczak please let us know when it will be fixed or provide an example of how to do it correctly 😃

@metelliandrea
Copy link

metelliandrea commented Mar 18, 2022

As workaround I put my response in a const and cast it to any (I guess a custom interface works too), in you case:

const jsonClient = got.extend({
  responseType: 'json',
  resolveBodyOnly: true
});

const response = await jsonClient('https://httpbin.org/json');
const data = response as any;

// other stuffs with data

Hope it help! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This change will extend Got features ✭ help wanted ✭
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants