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
- test #954 — committed to szmarczak/got by szmarczak 4 years ago
- test #954 — committed to szmarczak/got by szmarczak 4 years ago
- test #954 — committed to szmarczak/got by szmarczak 4 years ago
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
andresolveBodyOnly
options altogether.This allows for a base options type that always matches the default overload (i.e.
OptionsOfDefaultResponseBody
).I might even suggest
StrictOptions
be an exported type fromgot
.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:
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 doas const
an object (even withresponseType: 'text'
orisStream: false
) will always be considered asresponseType?: 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.https://github.com/microsoft/TypeScript/issues/24929#issuecomment-560506225
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ó:
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 fromgot
; 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
will fail with error
P.S. I have submitted a PR for exporting
GotFunctions
interface https://github.com/sindresorhus/got/pull/1017We have this error when we try to do that
The only way I had to manage this is to do an
as const
and don’t declare asGotOptions
like this: