Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/widen-typescript-peer-dep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"openapi-typescript": patch
"openapi-typescript-helpers": patch
---

feat: add TypeScript 6 support

- Widen peer dep to `^5.x || ^6.x`
- Fix `Readable<T>` and `Writable<T>` to preserve built-in objects (Date, RegExp, functions) that now match `extends object` in TS6
20 changes: 10 additions & 10 deletions packages/openapi-fetch/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ export function createObservedClient<T extends {}, M extends MediaType = MediaTy
}

/**
* Convert a Headers object to a plain object for easier comparison
* Convert a Headers object to a plain object for easier comparison.
* Uses Headers.forEach rather than Headers.entries because forEach is in base
* lib.dom with an identical signature in both TS 5.x and 6.x, while entries
* requires DOM.Iterable in TS 5.x.
*/
export function headersToObj(headers: Headers | Record<string, string>): Record<string, string> {
const iter =
headers instanceof Headers
? headers
// @ts-expect-error FIXME: this is a missing "lib" in tsconfig.json but dunno what
.entries()
: Object.entries(headers);
const result: Record<string, string> = {};
for (const [k, v] of iter) {
result[k] = v;
if (!(headers instanceof Headers)) {
return { ...headers };
}
const result: Record<string, string> = {};
headers.forEach((value, key) => {
result[key] = value;
});
return result;
}
2 changes: 1 addition & 1 deletion packages/openapi-typescript-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"lint:ts": "tsc --noEmit"
},
"devDependencies": {
"typescript": "5.9.3"
"typescript": "6.0.2"
}
}
20 changes: 12 additions & 8 deletions packages/openapi-typescript-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ export type Readable<T> =
? Readable<U>
: T extends (infer E)[]
? Readable<E>[]
: T extends object
? { [K in keyof T as NonNullable<T[K]> extends $Write<any> ? never : K]: Readable<T[K]> }
: T;
: T extends Date | RegExp | ((...args: never[]) => unknown)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

In TypeScript 6, Date extends object now evaluates to true (it was false in TS5). Without this guard, Readable<T> would structurally expand Date fields into { toString: {}; toDateString: {}; ... } instead of preserving the Date type — making any schema containing Date fields incompatible with itself when accessed through FetchResponse.

This guard preserves built-in objects (Date, RegExp, functions) by matching them before the generic T extends object branch. It's backward compatible with TS5 since these types were never matched by T extends object there.

? T
: T extends object
? { [K in keyof T as NonNullable<T[K]> extends $Write<any> ? never : K]: Readable<T[K]> }
: T;

/**
* Resolve type for writing (requests): strips $Read properties, unwraps $Write
Expand All @@ -240,8 +242,10 @@ export type Writable<T> =
? Writable<U>
: T extends (infer E)[]
? Writable<E>[]
: T extends object
? { [K in keyof T as NonNullable<T[K]> extends $Read<any> ? never : K]: Writable<T[K]> } & {
[K in keyof T as NonNullable<T[K]> extends $Read<any> ? K : never]?: never;
}
: T;
: T extends Date | RegExp | ((...args: never[]) => unknown)
? T
: T extends object
? { [K in keyof T as NonNullable<T[K]> extends $Read<any> ? never : K]: Writable<T[K]> } & {
[K in keyof T as NonNullable<T[K]> extends $Read<any> ? K : never]?: never;
}
: T;
2 changes: 1 addition & 1 deletion packages/openapi-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"version": "pnpm run build"
},
"peerDependencies": {
"typescript": "^5.x"
"typescript": "^5.x || ^6.x"
},
"dependencies": {
"@redocly/openapi-core": "^1.34.6",
Expand Down
Loading
Loading