Skip to content

Commit

Permalink
Fix exactOptionalPropertyTypes, fix TS 5.4
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alecmev committed Mar 22, 2024
1 parent 051d7ab commit 2dd3eb7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
8 changes: 6 additions & 2 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Required<Hooks>>(
Expand Down
1 change: 1 addition & 0 deletions source/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ export const requestOptionsRegistry: RequestInitRegistry = {
window: true,
dispatcher: true,
duplex: true,
priority: true,
};
3 changes: 1 addition & 2 deletions source/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ Omit<Options, 'hooks' | 'retry'>,
hooks: Required<Hooks>;
retry: Required<RetryOptions>;
prefixUrl: string;
credentials?: Options['credentials']; // Allows credentials to be undefined for workers
};

/**
Expand All @@ -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<RequestInit['method']>;
credentials: RequestInit['credentials'];
credentials?: NonNullable<RequestInit['credentials']>;

// Extended from custom `KyOptions`, but ensured to be set (not optional).
retry: RetryOptions;
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"exactOptionalPropertyTypes": true
},
"include": [
"source"
]
Expand Down

0 comments on commit 2dd3eb7

Please sign in to comment.