fix(typescript-input): strip brace-wrapped types from JSDoc @type tags#2968
Merged
Conversation
#2695) typescript-json-schema treats a property's `@type {string}` JSDoc tag as a "type" validation keyword and copies its raw text verbatim, braces included, into the generated schema's `type` field. That produces invalid JSON Schema like `"type": "{string}"`, which also makes quicktype fail hard when that schema is later consumed for any other output language. Sanitize the schema after generateSchema() runs: strip a single pair of surrounding braces from any string `type` value. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
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.
Bug
TypeScript input properties documented with a JSDoc
@type {...}tag (e.g.@type {string}) produced invalid"type": "{string}"in the generatedJSON Schema instead of
"type": "string", when converting via--src-lang typescript --lang schema.produced:
{ "type": "{string}", "title": "name" }instead of the expected:
{ "type": "string", "title": "name" }Because the resulting schema is invalid JSON Schema, feeding it back into
quicktype for any other target language then failed hard with
Invalid type {string} in JSON Schema.Root cause
packages/quicktype-typescript-inputwraps thetypescript-json-schemanpmpackage (a normal, unforked dependency). That package hardcodes
typeas analways-on "validation keyword": when it sees a JSDoc
@type {TypeExpression}tag on a symbol, it copies the tag's raw text — including the surrounding
braces from the JSDoc syntax — verbatim into the generated schema's
typefield, overwriting the correct type inferred from the TypeScript type. This
behavior isn't configurable via the settings quicktype passes in, so it can't
be disabled at the call site.
Fix
Added a small post-processing pass (
sanitizeTypeAnnotations) inpackages/quicktype-typescript-input/src/index.ts, run on the schemaright after
generateSchema()returns (in the same spirit as the existingpost-processing already done in that function). It walks the schema
recursively and strips a single pair of surrounding braces from any string
typevalue, e.g. turning"{string}"into"string".Test coverage
Added a regression test in
test/unit/typescript-input.test.tsreproducingthe issue's exact TypeScript input (a
@type {string}-documented propertyplus a
Required<Person>usage) and asserting the generated schema'sproperty type is the plain string
"string". The test was confirmed to failon unfixed code (
Received: "{string}") before the fix was applied.Verification
npm run build— passesnpm run test:unit— 25 files, 164 tests pass (including the newregression test)
node dist/index.js --src person.ts --src-lang typescript --lang schemanow emits
"type": "string"instead of"type": "{string}"output from the same TypeScript input (
--lang go) now succeeds insteadof erroring with
Invalid type {string} in JSON Schemaexercised by the JSON/JSON-Schema fixture system (that operates on
JSON/JSON-Schema inputs, not TypeScript source), so unit test coverage
in
test/unit/is the appropriate mechanism here, consistent with how therest of that file already tests
schemaForTypeScriptSources. CI will runthe full fixture and unit test suites.
Fixes #2695
🤖 Generated with Claude Code