From 2dd3eb7339cb6396f983dfc03861e701bc552bf6 Mon Sep 17 00:00:00 2001 From: Alec Mev Date: Fri, 22 Mar 2024 12:38:50 +0000 Subject: [PATCH] Fix exactOptionalPropertyTypes, fix TS 5.4 https://github.com/sindresorhus/ky/pull/559 undid the fix in https://github.com/sindresorhus/ky/pull/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. --- source/core/Ky.ts | 8 ++++++-- source/core/constants.ts | 1 + source/types/options.ts | 3 +-- tsconfig.json | 3 +++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/source/core/Ky.ts b/source/core/Ky.ts index 9147191a..638cb324 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -118,9 +118,13 @@ export class Ky { // eslint-disable-next-line complexity constructor(input: Input, options: Options = {}) { this._input = input; - const isCredentialsSupported = 'credentials' in Request.prototype; + const credentials + = this._input instanceof Request && 'credentials' in Request.prototype + ? this._input.credentials + : undefined; + this._options = { - credentials: isCredentialsSupported ? (this._input as Request).credentials : undefined, + ...(credentials && {credentials}), // For exactOptionalPropertyTypes ...options, headers: mergeHeaders((this._input as Request).headers, options.headers), hooks: deepMerge>( diff --git a/source/core/constants.ts b/source/core/constants.ts index 0f1fcdbd..27309629 100644 --- a/source/core/constants.ts +++ b/source/core/constants.ts @@ -76,4 +76,5 @@ export const requestOptionsRegistry: RequestInitRegistry = { window: true, dispatcher: true, duplex: true, + priority: true, }; diff --git a/source/types/options.ts b/source/types/options.ts index d69555f1..c391dbaa 100644 --- a/source/types/options.ts +++ b/source/types/options.ts @@ -245,7 +245,6 @@ Omit, hooks: Required; retry: Required; prefixUrl: string; - credentials?: Options['credentials']; // Allows credentials to be undefined for workers }; /** @@ -254,7 +253,7 @@ Normalized options passed to the `fetch` call and the `beforeRequest` hooks. export interface NormalizedOptions extends RequestInit { // eslint-disable-line @typescript-eslint/consistent-type-definitions -- This must stay an interface so that it can be extended outside of Ky for use in `ky.create`. // Extended from `RequestInit`, but ensured to be set (not optional). method: NonNullable; - credentials: RequestInit['credentials']; + credentials?: NonNullable; // Extended from custom `KyOptions`, but ensured to be set (not optional). retry: RetryOptions; diff --git a/tsconfig.json b/tsconfig.json index d9a03892..bbbb2278 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,8 @@ { "extends": "@sindresorhus/tsconfig", + "compilerOptions": { + "exactOptionalPropertyTypes": true + }, "include": [ "source" ]