sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
MIT License
11.83k stars 341 forks source link

Fix request types for use with Node #578

Closed jliuhtonen closed 2 months ago

jliuhtonen commented 2 months ago
Screenshot 2024-04-20 at 21 59 11

When using Ky with Node, TypeScript will run into compilation error regarding types required by UndiciRequestInit. This is because @types/node does not yet provide these types, and they are not imported from undici-types either. The latter would not even be possible to keep Ky usable with browsers.

This happened to work in the project, because @sindresorhus/tsconfig includes DOM in the libs section. In most Node applications, the DOM lib should not be there and clashes with Node types. One way to circumvent this is to add skipLibCheck: true to tsconfig.json, but this is not a good solution.

This commit adds the needed types until @types/node imports the missing types and adds them to globals. After that, all "Undici" types from the request types should be removed.

jliuhtonen commented 2 months ago

Here is an example of a project not compiling because of the issue fixed in this PR.

sindresorhus commented 2 months ago

This commit adds the needed types until @types/node imports the missing types and adds them to globals. After that, all "Undici" types from the request types should be removed.

Can you open an issue on the TS types repo and add a code comment with the link? I like to know that it will eventually be resolved upstream.

jliuhtonen commented 2 months ago

@sindresorhus done, I opened a discussion here: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69408

Code comment also added.