got: TypeScript cannot figure out which Got type to use

What would you like to discuss?

With @types/got we could use type GotOptions and be able to pass this to got.get(options) however with v10 the types are string | OptionsOfDefaultResponseBody and OptionsOfDefaultResponseBody and its friends are not available to import at the root of the project. I was wondering if this was on purpose.

What i’m looking for and not sure how to do with v10

const options: GotOptions = {
	url: 'https://example.com',
};
const response = await got(options);

Also, this example from the docs isn’t working for me and I’m wondering if it should accept a partial? Opened https://github.com/sindresorhus/got/pull/953 with a demo

got.mergeOptions(got.defaults.options, {
	responseType: 'json',
});

Checklist

  • I have read the documentation.

Really excited about the release of v10 🎉

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 16
  • Comments: 29 (9 by maintainers)

Commits related to this issue

Most upvoted comments

go back to the old and good JavaScript… you are making up problems. IMO.

The entire problem is a result of the dynamic return type being inferred from values in the options object. IMO the simplest workaround is to avoid using the isStream, responseType and resolveBodyOnly options altogether.

// got.get<CustomType>(url, { responseType: 'json' })
got.get(url).json<CustomType>()

// got.get(url, { responseType: 'blob' })
got.get(url).blob()

// got.get(url, { responseType: 'text' })
got.get(url).text()

// got.get(url, { isStream: true })
got.stream(url)

This allows for a base options type that always matches the default overload (i.e. OptionsOfDefaultResponseBody).

type StrictOptions = Omit<Options, 'isStream' | 'responseType' | 'resolveBodyOnly'>

I might even suggest StrictOptions be an exported type from got.

// @pmmmwh

I am thinking, maybe we should expose some of the internal option types to the outside world? Or instead, exporting a type for promise and another one for stream?

That seems like a good idea to make sure TypeScript infers them correctly. For example, the following works:

const options: OptionsOfDefaultResponseBody = {
	url: 'https://example.com',
};
const response = await got(options);

Edit: Actually, thinking about it - part of the issue is that we strictly type options’ responseType/isStream/resolveBodyOnly properties, while in the actual world if the user does not do as const an object (even with responseType: 'text' or isStream: false) will always be considered as responseType?: string/isStream?: boolean. This make all our specific overloads fail and thus fallback to the last one, which is the one for streams. I will have to test if we can workaround that while typing with high specificity.

You were using @types/got which was made for an older version. You need to update your code to v10 or rollback. It has nothing to do with this issue.

That what the link above does, it will remove the types from the repo and pushes a new version to npm with deprecation notice

Or just mark it as deprecated.

El lun., 24 feb. 2020 a las 15:06, Michael Kriese (notifications@github.com) escribió:

Maybe someone should add a package removal pr https://github.com/DefinitelyTyped/DefinitelyTyped#removing-a-package to for @types/got

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sindresorhus/got/issues/954?email_source=notifications&email_token=ACTTGMQDGD6WOIZQ6KUSAK3REPH43A5CNFSM4JTOJM22YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMX4YOQ#issuecomment-590335034, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACTTGMTXRGZ7NE5UOACRU3DREPH43ANCNFSM4JTOJM2Q .

I’m bumping up against something with the typings, although it might not be related to this issue. After an update, it no longer seems possible to import FormOptions and friends from got; This breaks my code.

I also can’t import GotPromise and a load of other stuff. What is happening? Do we know of any solutions to this yet?

@pmmmwh is right about the bigger picture of this issue. Just trying to proxy arguments types to nested function

  const queuedGet = (...args: Parameters<GotFunctions>) =>
    queue.add(() => got.get(...args))

will fail with error

Types of property 'isStream' are incompatible.
  Type 'false | undefined' is not assignable to type 'true'.

P.S. I have submitted a PR for exporting GotFunctions interface https://github.com/sindresorhus/got/pull/1017

We have this error when we try to do that

image

The only way I had to manage this is to do an as const and don’t declare as GotOptions like this:

const options = {
    method,
    cookieJar,
    agent,
    responseType: "json",
    form,
    headers
  } as const;

await got(url, options);