Skip to content

Wrap Record types in Partial for propertyNames#1643

Merged
js2me merged 3 commits intoacacode:mainfrom
nolannbiron:main
Mar 8, 2026
Merged

Wrap Record types in Partial for propertyNames#1643
js2me merged 3 commits intoacacode:mainfrom
nolannbiron:main

Conversation

@nolannbiron
Copy link
Contributor

Problem

propertyNames in JSON Schema only constrains which keys are allowed on an object, it does not make them required. However, the generated TypeScript output uses Record<K, V>, which requires all keys to be present.

For example, given a schema with propertyNames referencing an enum [en, fr, de, es], the output was:

export type LocalizedString = Record<TranslationLanguage, string>;

This incorrectly requires all enum values as keys.

Solution

When propertyNames is present, wrap the generated Record<K, V> with Partial<> in the primitive schema parser. Without propertyNames, plain additionalProperties with string keys continues to generate Record<string, V> as before.

The fix is a single conditional in src/schema-parser/base-schema-parsers/primitive.ts: if a propertyNamesSchema exists, the output becomes Partial<Record<K, V>> via the existing TypeWithGeneric construct.

Verification

Updated the propertyNames test snapshot to expect Partial<Record<KekEnum, number>> in both top-level type aliases and nested interface fields.

Fixes #1642

Construct the Record type in a temporary variable and, when a propertyNames schema is present, wrap it with Partial (Partial<Record<...>>) before assigning to contentType. This ensures records generated for propertyNames are optional by default. Updated snapshots to reflect the Partial wrapper.
@changeset-bot
Copy link

changeset-bot bot commented Mar 6, 2026

🦋 Changeset detected

Latest commit: 9609f0d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
swagger-typescript-api Minor

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

@js2me
Copy link
Member

js2me commented Mar 8, 2026

LGTM! I think it's ok :)

@js2me js2me merged commit 7f76066 into acacode:main Mar 8, 2026
4 checks passed
@github-actions github-actions bot mentioned this pull request Mar 8, 2026
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.

propertyNames generates Record<K, V> instead of Partial<Record<K, V>>

2 participants