Wrap Record types in Partial for propertyNames#1643
Merged
js2me merged 3 commits intoacacode:mainfrom Mar 8, 2026
Merged
Conversation
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 detectedLatest commit: 9609f0d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Member
|
LGTM! I think it's ok :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
propertyNamesin JSON Schema only constrains which keys are allowed on an object, it does not make them required. However, the generated TypeScript output usesRecord<K, V>, which requires all keys to be present.For example, given a schema with
propertyNamesreferencing 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
propertyNamesis present, wrap the generatedRecord<K, V>withPartial<>in the primitive schema parser. WithoutpropertyNames, plainadditionalPropertieswith string keys continues to generateRecord<string, V>as before.The fix is a single conditional in src/schema-parser/base-schema-parsers/primitive.ts: if a
propertyNamesSchemaexists, the output becomesPartial<Record<K, V>>via the existingTypeWithGenericconstruct.Verification
Updated the
propertyNamestest snapshot to expectPartial<Record<KekEnum, number>>in both top-level type aliases and nested interface fields.Fixes #1642