Skip to content

Commit

Permalink
Fix exactOptionalPropertyTypes and compatibility with TypeScript 5.4 (
Browse files Browse the repository at this point in the history
  • Loading branch information
alecmev committed Mar 24, 2024
1 parent 051d7ab commit 08f912d
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
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
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
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
@@ -1,5 +1,8 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"exactOptionalPropertyTypes": true
},
"include": [
"source"
]
Expand Down

0 comments on commit 08f912d

Please sign in to comment.