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

Make types compatible with both DOM and Node.js #543

Merged
merged 1 commit into from Nov 16, 2023

Conversation

alecmev
Copy link
Contributor

@alecmev alecmev commented Nov 12, 2023

Otherwise TypeScript fails with skipLibCheck: false, exactOptionalPropertyTypes: true and no DOM:

Error: node_modules/ky/distribution/types/options.d.ts(226,18): error TS2430: Interface 'NormalizedOptions' incorrectly extends interface 'RequestInit'.
  Types of property 'credentials' are incompatible.
    Type 'RequestCredentials | undefined' is not assignable to type 'RequestCredentials'.
      Type 'undefined' is not assignable to type 'RequestCredentials'.
Error: node_modules/ky/distribution/types/options.d.ts(16,29): error TS2552: Cannot find name 'HeadersInit'. Did you mean 'KyHeadersInit'?
Error: node_modules/ky/distribution/types/options.d.ts(160,21): error TS2552: Cannot find name 'RequestInfo'. Did you mean 'RequestInit'?
Error: node_modules/ky/distribution/types/options.d.ts(226,18): error TS2430: Interface 'NormalizedOptions' incorrectly extends interface 'RequestInit'.
  Types of property 'method' are incompatible.
    Type 'string | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.

Would add a test, but it doesn't seem like there are any type tests. How do you want to go about this?

@alecmev alecmev changed the title Fix NormalizedOptions Make types compatible with both DOM and Node.js Nov 12, 2023
@@ -175,7 +175,7 @@ export class Ky {
this._options.duplex = 'half';
}

this.request = new globalThis.Request(this._input as RequestInfo, this._options as RequestInit);
Copy link
Contributor Author

@alecmev alecmev Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't appear to be needed?

@@ -177,7 +178,7 @@ export type KyOptions = {
const json = await ky('https://example.com', {fetch}).json();
```
*/
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking, but actually both @types/node and lib.dom.d.ts define it this way.

Comment on lines +255 to +256
method: NonNullable<RequestInit['method']>;
credentials: NonNullable<RequestInit['credentials']>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactOptionalPropertyTypes: true fails on this, as optional properties get resolved as Foo | undefined, and that isn't compatible with RequestInit.

@sindresorhus sindresorhus merged commit 3cf5064 into sindresorhus:main Nov 16, 2023
1 of 2 checks passed
@sindresorhus
Copy link
Owner

I'm unable to publish this until #544 is figured out.

@alecmev alecmev deleted the patch-1 branch November 16, 2023 13:10
@alecmev
Copy link
Contributor Author

alecmev commented Nov 16, 2023

That's alright, thanks for taking a look!

alecmev added a commit to alecmev/ky that referenced this pull request Mar 21, 2024
alecmev added a commit to alecmev/ky that referenced this pull request Mar 21, 2024
alecmev added a commit to alecmev/ky that referenced this pull request Mar 22, 2024
sindresorhus#559 undid the fix in
sindresorhus#543. Added
`exactOptionalPropertyTypes` to `compilerOptions` to prevent this from
happening again, seems pretty low-impact. It should be okay to pass
`undefined` to `credentials` and other options, but this is an upstream
problem (see `RequestInit` in `undici-types/fetch.d.ts`).

The build was failing on TS 5.4 because there's a `priority` in
`RequestInit` now.
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