feat: add TypeScript 6 support#2774
feat: add TypeScript 6 support#2774benjamineckstein wants to merge 2 commits intoopenapi-ts:mainfrom
Conversation
- Widen peer dep: `^5.x` → `^5.x || ^6.x` - Upgrade devDependency in openapi-typescript-helpers: 5.9.3 → 6.0.2 - Build, tests, and lint all pass with TS6 - No code changes needed Closes openapi-ts#2723
👷 Deploy request for openapi-ts pending review.Visit the deploys page to approve it
|
🦋 Changeset detectedLatest commit: a749a52 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…e/Writable types for TS6 In TypeScript 6, `Date extends object` evaluates to `true` (changed from TS5). This caused `Readable<T>` and `Writable<T>` to structurally expand Date, RegExp, and function fields instead of preserving them. Add a guard clause for built-in objects (Date, RegExp, functions) before the `T extends object` branch in both type utilities. Ref: openapi-ts#2723
| : 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) |
There was a problem hiding this comment.
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.
Summary
Add full TypeScript 6 support — not just the peer dep range, but also a fix for a TS6 behavioral change that breaks
Readable<T>andWritable<T>.Changes
Peer dep & devDependency (commit 1):
packages/openapi-typescript/package.json: widen peer dep^5.x→^5.x || ^6.xpackages/openapi-typescript-helpers/package.json: upgrade devDependency5.9.3→6.0.2Fix Readable/Writable types (commit 2):
In TypeScript 6,
Date extends objectevaluates totrue(changed from TS5). This causedReadable<T>andWritable<T>to structurally expand built-in objects likeDateinto{ toString: {}; toDateString: {}; ... }instead of preserving theDatetype.Added a guard clause for
Date | RegExp | ((...args: never[]) => unknown)before theT extends objectbranch in both type utilities, preserving built-in objects. This fix is backward compatible with TS5.Changeset: patch release for both
openapi-typescriptandopenapi-typescript-helpersValidation
pnpm run build— all 4 packages build successfullypnpm test— all 7 test suites passpnpm run lint— cleanContext
Readable<T>/Writable<T>reported by @yamafaktory in Add support for TypeScript 6 #2723