Skip to content

Conversation

@darkbasic
Copy link
Contributor

Changes

When using enumValues: true with OpenAPI schemas containing oneOf/anyOf unions, the generated TypeScript type paths would fail because properties might only exist on some union variants.

Before (invalid TypeScript):

export const resourceItemsNestedCodeValues: ReadonlyArray<
  FlattenedDeepRequired<components>["schemas"]["Resource"]["items"]["nested"]["code"]
> = ["a", "b"];
// Error: Property 'nested' does not exist on type '{ type: "simple"; } | { type: "complex"; nested: { code: "a" | "b"; }; }'

After (valid TypeScript):

export const resourceItemsOneOf1NestedCodeValues: ReadonlyArray<
  Extract<Extract<FlattenedDeepRequired<components>["schemas"]["Resource"]["items"], {
    nested: unknown;
  }>["nested"], {
    code: unknown;
  }>["code"]
> = ["a", "b"];

The fix wraps property accesses after oneOf/anyOf indices with Extract<> to narrow the union type before accessing variant-specific properties.

This PR has been tested in a production environment with a custom build of openapi-typescript.

How to Review

I've added three tests:

  • Basic union variant narrowing
  • Same property name with different inner schemas
  • Deeply nested unions (multiple Extract levels)

Checklist

  • Unit tests updated
  • [] docs/ updated (if necessary)
  • [] pnpm run update:examples run (only applicable for openapi-typescript)

@darkbasic darkbasic requested a review from a team as a code owner November 28, 2025 16:35
@netlify
Copy link

netlify bot commented Nov 28, 2025

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit e904348

@changeset-bot
Copy link

changeset-bot bot commented Nov 28, 2025

⚠️ No Changeset found

Latest commit: e904348

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@darkbasic
Copy link
Contributor Author

Note: two tests will fail due to network errors (probably the original gist has been deleted). Nothing I can do on that front (they were already failing on main).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant