@amritk/helpers 0.14.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI.md +13 -5
- package/README.md +4 -1
- package/dist/assert-id-scopes.d.ts +20 -0
- package/dist/assert-id-scopes.js +45 -0
- package/dist/build-anchor-map.d.ts +26 -0
- package/dist/build-anchor-map.js +35 -0
- package/dist/build-dynamic-ref-map.d.ts +8 -0
- package/dist/build-dynamic-ref-map.js +7 -5
- package/dist/build-resource-registry.d.ts +88 -0
- package/dist/build-resource-registry.js +90 -0
- package/dist/escape-regex-pattern.d.ts +27 -0
- package/dist/escape-regex-pattern.js +21 -1
- package/dist/extract-refs.js +6 -4
- package/dist/generate-index-barrel.js +13 -6
- package/dist/generate-type-definition.js +1 -4
- package/dist/graft-external-schemas.d.ts +83 -0
- package/dist/graft-external-schemas.js +70 -0
- package/dist/max-schema-depth.d.ts +22 -0
- package/dist/max-schema-depth.js +10 -0
- package/dist/mjst-extension.d.ts +4 -2
- package/dist/mjst-extension.js +11 -2
- package/dist/multiple-of-check.d.ts +31 -0
- package/dist/multiple-of-check.js +6 -10
- package/dist/normalize-ref-scopes.d.ts +29 -0
- package/dist/normalize-ref-scopes.js +53 -0
- package/dist/prune-external-schemas.d.ts +16 -0
- package/dist/prune-external-schemas.js +80 -0
- package/dist/ref-to-filename.d.ts +1 -0
- package/dist/ref-to-filename.js +19 -3
- package/dist/ref-to-name.js +7 -4
- package/dist/resolve-dynamic-refs.d.ts +10 -0
- package/dist/resolve-dynamic-refs.js +18 -16
- package/dist/resolve-ref.d.ts +4 -2
- package/dist/resolve-ref.js +32 -6
- package/dist/resolve-scoped-ref.d.ts +35 -0
- package/dist/resolve-scoped-ref.js +35 -0
- package/dist/safe-accessor.d.ts +38 -1
- package/dist/safe-accessor.js +15 -0
- package/dist/string-length-check.d.ts +8 -0
- package/dist/string-length-check.js +23 -0
- package/dist/upgrade-draft07-schema.js +9 -6
- package/dist/walk-ref-graph.d.ts +38 -3
- package/dist/walk-ref-graph.js +170 -23
- package/package.json +13 -3
- package/src/build-dynamic-ref-map.ts +0 -61
- package/src/derive-root-type-name.ts +0 -54
- package/src/escape-regex-pattern.ts +0 -87
- package/src/extract-dynamic-anchor-defs.ts +0 -25
- package/src/extract-refs.ts +0 -73
- package/src/fold-nullable.ts +0 -60
- package/src/generate-index-barrel.ts +0 -118
- package/src/generate-type-definition.ts +0 -784
- package/src/mjst-extension.ts +0 -81
- package/src/multiple-of-check.ts +0 -32
- package/src/parse-documentation.ts +0 -177
- package/src/quote-js-string.ts +0 -20
- package/src/ref-to-filename.ts +0 -116
- package/src/ref-to-name.ts +0 -54
- package/src/resolve-dynamic-refs.ts +0 -74
- package/src/resolve-ref.ts +0 -82
- package/src/safe-accessor.ts +0 -53
- package/src/schema-guards.ts +0 -234
- package/src/unknown-key-check.ts +0 -68
- package/src/upgrade-draft07-schema.ts +0 -179
- package/src/walk-ref-graph.ts +0 -257
|
@@ -1,784 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
-
|
|
3
|
-
import { getMjstBrand, getMjstInstanceOf, getMjstPrimitive } from './mjst-extension'
|
|
4
|
-
import { refToName } from './ref-to-name'
|
|
5
|
-
import { resolveRef } from './resolve-ref'
|
|
6
|
-
import { safeKey } from './safe-accessor'
|
|
7
|
-
import { isObjectSchema, isSchemaObject } from './schema-guards'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Any schema that is not a boolean shorthand. Broader than `JSONSchema.Object`,
|
|
11
|
-
* which narrows `type` to `'object'` and so hides array keywords like `items`.
|
|
12
|
-
*/
|
|
13
|
-
type SchemaNode = Exclude<JSONSchema, boolean>
|
|
14
|
-
|
|
15
|
-
type ConditionalObjectResult = {
|
|
16
|
-
schema: JSONSchema.Object
|
|
17
|
-
thenRef: string | null
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** Options controlling generated type output. */
|
|
21
|
-
export type TypeOptions = {
|
|
22
|
-
/** When true, every property, array, and record in the generated types is emitted as readonly. */
|
|
23
|
-
readonly readonly?: boolean
|
|
24
|
-
/**
|
|
25
|
-
* Suffix appended to every generated type name derived from a `$ref`.
|
|
26
|
-
* Defaults to `''` (no suffix). Set to e.g. `'Object'` to emit `ContactObject`.
|
|
27
|
-
*/
|
|
28
|
-
readonly typeSuffix?: string
|
|
29
|
-
/**
|
|
30
|
-
* The root document, used to decide whether a URI `$ref` names a type the
|
|
31
|
-
* generator also emits a file for. Without it such refs stay `unknown` — the
|
|
32
|
-
* conservative reading, since an unresolvable ref has no generated file to
|
|
33
|
-
* name. Pass it whenever the caller also emits imports for those refs, so the
|
|
34
|
-
* type and the import agree.
|
|
35
|
-
*/
|
|
36
|
-
readonly rootSchema?: Record<string, unknown>
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The type name for a `$ref`, or `unknown` when no file is generated for it.
|
|
41
|
-
*
|
|
42
|
-
* Internal refs always name a generated type. A URI ref does too — but only
|
|
43
|
-
* when it resolves inside the root document, which is precisely the rule
|
|
44
|
-
* `collectImports` uses to decide whether to import it. Keeping the two in step
|
|
45
|
-
* is what stops a file from importing `Channel` while typing the property that
|
|
46
|
-
* uses it as `unknown`.
|
|
47
|
-
*/
|
|
48
|
-
const refTypeName = (ref: string, options: TypeOptions): string => {
|
|
49
|
-
if (ref.startsWith('#')) return refToName(ref, options.typeSuffix)
|
|
50
|
-
if (ref.includes('#/properties/')) return 'unknown'
|
|
51
|
-
if (options.rootSchema && resolveRef(ref, options.rootSchema)) return refToName(ref, options.typeSuffix)
|
|
52
|
-
return 'unknown'
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const getConditionalObjectSchema = (schema: JSONSchema): ConditionalObjectResult | null => {
|
|
56
|
-
if (!isSchemaObject(schema)) {
|
|
57
|
-
return null
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!('if' in schema) || !('then' in schema)) {
|
|
61
|
-
return null
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const ifSchema = schema.if
|
|
65
|
-
const thenSchema = schema.then
|
|
66
|
-
|
|
67
|
-
if (!isSchemaObject(ifSchema) || !isSchemaObject(thenSchema)) {
|
|
68
|
-
return null
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const ownProperties = schema.properties
|
|
72
|
-
const ifProperties = ifSchema.properties
|
|
73
|
-
const thenProperties = thenSchema.properties
|
|
74
|
-
const hasOwnProperties = ownProperties && typeof ownProperties === 'object'
|
|
75
|
-
const hasIfProperties = ifProperties && typeof ifProperties === 'object'
|
|
76
|
-
const hasThenProperties = thenProperties && typeof thenProperties === 'object'
|
|
77
|
-
|
|
78
|
-
if (!hasIfProperties && !hasThenProperties) {
|
|
79
|
-
return null
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// The conditional fragments are merged *onto* the schema's own properties —
|
|
83
|
-
// a schema is allowed to declare `properties` alongside `if`/`then`, and
|
|
84
|
-
// reading only the conditional halves silently dropped everything it declared
|
|
85
|
-
// unconditionally.
|
|
86
|
-
const properties = {
|
|
87
|
-
...(hasOwnProperties ? ownProperties : {}),
|
|
88
|
-
...(hasIfProperties ? ifProperties : {}),
|
|
89
|
-
...(hasThenProperties ? thenProperties : {}),
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const required = new Set<string>()
|
|
93
|
-
|
|
94
|
-
if (Array.isArray(schema.required)) {
|
|
95
|
-
for (const key of schema.required) {
|
|
96
|
-
required.add(key)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (Array.isArray(ifSchema.required)) {
|
|
101
|
-
for (const key of ifSchema.required) {
|
|
102
|
-
required.add(key)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (hasIfProperties) {
|
|
107
|
-
for (const key in ifProperties) {
|
|
108
|
-
required.add(key)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (Array.isArray(thenSchema.required)) {
|
|
113
|
-
for (const key of thenSchema.required) {
|
|
114
|
-
required.add(key)
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (hasThenProperties) {
|
|
119
|
-
for (const key in thenProperties) {
|
|
120
|
-
required.add(key)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const thenRef = typeof thenSchema.$ref === 'string' ? thenSchema.$ref : null
|
|
125
|
-
|
|
126
|
-
return {
|
|
127
|
-
schema: {
|
|
128
|
-
type: 'object',
|
|
129
|
-
properties,
|
|
130
|
-
...(required.size > 0 ? { required: Array.from(required) } : {}),
|
|
131
|
-
},
|
|
132
|
-
thenRef,
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const isObjectLikeSchema = (schema: JSONSchema): schema is JSONSchema.Object => {
|
|
137
|
-
if (!isSchemaObject(schema)) {
|
|
138
|
-
return false
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (isObjectSchema(schema)) {
|
|
142
|
-
return true
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return 'patternProperties' in schema || 'additionalProperties' in schema || ('if' in schema && 'then' in schema)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const getBooleanSubSchemaType = (schema: boolean): string => {
|
|
149
|
-
return schema ? 'unknown' : 'never'
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Neutralizes comment terminators inside schema-authored prose. A description
|
|
154
|
-
* holding a star-slash pair — a glob like `**\/*.ts`, a C-style code sample —
|
|
155
|
-
* would close the JSDoc block early and leave the rest of the generated file
|
|
156
|
-
* unparseable. Escaping the slash is the conventional fix: editors and TypeDoc
|
|
157
|
-
* still render the original text, but the comment can no longer end there.
|
|
158
|
-
*/
|
|
159
|
-
const escapeCommentText = (text: string): string => text.replace(/\*\//g, '*\\/')
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Renders a possibly multi-line description as JSDoc body lines, prefixing each
|
|
163
|
-
* line with `${indent}* `. Blank lines become a bare `${indent}*` so we never
|
|
164
|
-
* emit trailing whitespace. Without this, embedded newlines would leave
|
|
165
|
-
* continuation lines unprefixed and break the comment block.
|
|
166
|
-
*/
|
|
167
|
-
const renderJsDocBody = (description: string, indent: string): string =>
|
|
168
|
-
escapeCommentText(description)
|
|
169
|
-
.split('\n')
|
|
170
|
-
.map((line) => (line.length > 0 ? `${indent}* ${line}\n` : `${indent}*\n`))
|
|
171
|
-
.join('')
|
|
172
|
-
|
|
173
|
-
const buildJsDocBlock = (title: string, description: string, commentUrl?: string): string => {
|
|
174
|
-
let block = `/**\n`
|
|
175
|
-
block += `* ${title}\n`
|
|
176
|
-
block += `*\n`
|
|
177
|
-
block += renderJsDocBody(description, '')
|
|
178
|
-
if (commentUrl?.startsWith('http')) {
|
|
179
|
-
block += `* \n`
|
|
180
|
-
block += `* @see {@link ${commentUrl}}\n`
|
|
181
|
-
}
|
|
182
|
-
block += `*/\n`
|
|
183
|
-
return block
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Builds the inline JSDoc comment that precedes a generated property. A
|
|
188
|
-
* single-line description stays compact (` /** text *\/`); a multi-line
|
|
189
|
-
* description expands into an asterisk-prefixed block so every line is
|
|
190
|
-
* commented. The returned string always ends with a newline, ready for the
|
|
191
|
-
* property declaration to follow.
|
|
192
|
-
*/
|
|
193
|
-
const buildInlinePropertyComment = (description: string): string => {
|
|
194
|
-
if (!description.includes('\n')) {
|
|
195
|
-
return ` /** ${escapeCommentText(description)} */\n`
|
|
196
|
-
}
|
|
197
|
-
return ` /**\n${renderJsDocBody(description, ' ')} */\n`
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Converts a JSON Schema type to its TypeScript equivalent, applying any
|
|
202
|
-
* `x-mjst` brand. Branding is type-level only, so we compute the underlying
|
|
203
|
-
* type and intersect it with a unique brand marker. This is the recursion entry
|
|
204
|
-
* point, so branded nested fields are wrapped too.
|
|
205
|
-
*/
|
|
206
|
-
const getTypeScriptType = (schema: JSONSchema, options: TypeOptions = {}): string => {
|
|
207
|
-
const base = getUnbrandedType(schema, options)
|
|
208
|
-
const brand = getMjstBrand(schema)
|
|
209
|
-
const branded = brand ? `(${base} & { readonly __brand: '${brand}' })` : base
|
|
210
|
-
// OpenAPI 3.0 spells nullability as a sibling flag instead of a `null` member
|
|
211
|
-
// of `type`. Without this widening the emitted type claims the value can never
|
|
212
|
-
// be null while the document says it can — and `generate-validators` builds an
|
|
213
|
-
// `input is T` predicate on top of a validator that *does* accept null, so the
|
|
214
|
-
// narrowing would be unsound.
|
|
215
|
-
return isNullableSchema(schema) ? withNull(branded) : branded
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** True for the OpenAPI 3.0 `nullable: true` widening (JSON Schema has no such keyword). */
|
|
219
|
-
const isNullableSchema = (schema: JSONSchema): boolean =>
|
|
220
|
-
isSchemaObject(schema) && (schema as Record<string, unknown>)['nullable'] === true
|
|
221
|
-
|
|
222
|
-
/** Parenthesizes a union so it composes safely inside `[]`, `&`, or an optional marker. */
|
|
223
|
-
const wrapUnion = (type: string): string => (type.includes(' | ') ? `(${type})` : type)
|
|
224
|
-
|
|
225
|
-
/** Appends `| null` unless the rendered type already admits null. */
|
|
226
|
-
const withNull = (type: string): string => (type.split(' | ').includes('null') ? type : `${type} | null`)
|
|
227
|
-
|
|
228
|
-
/** Joins members with ` | `, dropping duplicates while preserving order. */
|
|
229
|
-
const unionOf = (members: readonly string[]): string => [...new Set(members)].join(' | ')
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Joins members with ` & `, dropping duplicates and any `unknown` member —
|
|
233
|
-
* `X & unknown` is just `X`, and composition keywords routinely contribute one
|
|
234
|
-
* (a `oneOf` branch that only lists `required`, say). Returns `unknown` when
|
|
235
|
-
* every member was dropped.
|
|
236
|
-
*/
|
|
237
|
-
const intersectionOf = (members: readonly string[]): string => {
|
|
238
|
-
const kept = [...new Set(members)].filter((member) => member !== 'unknown')
|
|
239
|
-
if (kept.length === 0) return 'unknown'
|
|
240
|
-
// `X & object` is `X` for any object type, and a schema that declares
|
|
241
|
-
// `type: 'object'` next to a `oneOf` contributes exactly that bare member.
|
|
242
|
-
const meaningful = kept.length > 1 ? kept.filter((member) => member !== 'object') : kept
|
|
243
|
-
if (meaningful.length === 0) return 'object'
|
|
244
|
-
return meaningful.join(' & ')
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/** Wraps a `Record<...>` in `Readonly<...>` when readonly output is requested. */
|
|
248
|
-
const recordType = (keyType: string, valueType: string, options: TypeOptions): string =>
|
|
249
|
-
options.readonly ? `Readonly<Record<${keyType}, ${valueType}>>` : `Record<${keyType}, ${valueType}>`
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Builds the `Record<…>` type for a schema's `patternProperties`. Every pattern's
|
|
253
|
-
* value type is unioned (deduplicated) rather than only the first being used, so
|
|
254
|
-
* `{ '^a': string-schema, '^b': number-schema }` becomes `Record<string, string |
|
|
255
|
-
* number>` instead of silently dropping the second pattern. The template-literal
|
|
256
|
-
* `` `x-${string}` `` key is kept only when the sole pattern is the `^x-` vendor
|
|
257
|
-
* convention. Returns `undefined` when there are no usable patterns.
|
|
258
|
-
*/
|
|
259
|
-
const patternPropertiesRecordType = (
|
|
260
|
-
patternProperties: Record<string, JSONSchema | boolean>,
|
|
261
|
-
options: TypeOptions,
|
|
262
|
-
): string | undefined => {
|
|
263
|
-
const entries = Object.entries(patternProperties)
|
|
264
|
-
if (entries.length === 0) return undefined
|
|
265
|
-
|
|
266
|
-
const seen = new Set<string>()
|
|
267
|
-
const valueTypes: string[] = []
|
|
268
|
-
for (const [, value] of entries) {
|
|
269
|
-
const valueType = typeof value === 'boolean' ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options)
|
|
270
|
-
if (!seen.has(valueType)) {
|
|
271
|
-
seen.add(valueType)
|
|
272
|
-
valueTypes.push(valueType)
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
if (valueTypes.length === 0) return undefined
|
|
276
|
-
|
|
277
|
-
const valueType = valueTypes.join(' | ')
|
|
278
|
-
// The `^x-` vendor-extension convention maps to a template-literal key, but only
|
|
279
|
-
// when it is the single pattern (a union of key patterns can't be expressed).
|
|
280
|
-
const keyType = entries.length === 1 && entries[0]?.[0] === '^x-' ? '`x-${string}`' : 'string'
|
|
281
|
-
return recordType(keyType, valueType, options)
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* The tuple positions a schema declares: 2020-12 `prefixItems`, or the draft-07
|
|
286
|
-
* array form of `items`. Returns undefined for a plain (homogeneous) array.
|
|
287
|
-
*/
|
|
288
|
-
const getTuplePositions = (schema: SchemaNode): readonly JSONSchema[] | undefined => {
|
|
289
|
-
const record = schema as Record<string, unknown>
|
|
290
|
-
const prefixItems = record['prefixItems']
|
|
291
|
-
if (Array.isArray(prefixItems) && prefixItems.length > 0) return prefixItems as JSONSchema[]
|
|
292
|
-
if (Array.isArray(schema.items) && schema.items.length > 0) return schema.items as JSONSchema[]
|
|
293
|
-
return undefined
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Renders a tuple type for a schema carrying `prefixItems` (or the draft-07
|
|
298
|
-
* array `items`). A position is only required when `minItems` reaches it — JSON
|
|
299
|
-
* Schema lets a tuple stop short unless told otherwise — so the rest are
|
|
300
|
-
* emitted optional. Extra elements past the declared positions are typed from
|
|
301
|
-
* the sibling `items` (`additionalItems` in the draft-07 spelling) and dropped
|
|
302
|
-
* entirely when that sibling is `false`.
|
|
303
|
-
*/
|
|
304
|
-
const tupleTypeToTs = (schema: SchemaNode, positions: readonly JSONSchema[], options: TypeOptions): string => {
|
|
305
|
-
const record = schema as Record<string, unknown>
|
|
306
|
-
// With the draft-07 spelling `items` *is* the tuple, so the rest schema is
|
|
307
|
-
// `additionalItems`; with `prefixItems` it is the sibling `items`.
|
|
308
|
-
const rest = Array.isArray(schema.items) ? record['additionalItems'] : record['items']
|
|
309
|
-
const minItems = typeof schema.minItems === 'number' ? schema.minItems : 0
|
|
310
|
-
|
|
311
|
-
const parts: string[] = []
|
|
312
|
-
for (let i = 0; i < positions.length; i++) {
|
|
313
|
-
const positionType = wrapUnion(getTypeScriptType(positions[i] as JSONSchema, options))
|
|
314
|
-
parts.push(i < minItems ? positionType : `${positionType}?`)
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (rest !== false) {
|
|
318
|
-
const restType =
|
|
319
|
-
rest === undefined || rest === true ? 'unknown' : wrapUnion(getTypeScriptType(rest as JSONSchema, options))
|
|
320
|
-
parts.push(`...${restType}[]`)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const tuple = `[${parts.join(', ')}]`
|
|
324
|
-
return options.readonly ? `readonly ${tuple}` : tuple
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/** Renders the TypeScript type for an `array` schema — tuple, typed list, or bare list. */
|
|
328
|
-
const arrayTypeToTs = (schema: SchemaNode, options: TypeOptions): string => {
|
|
329
|
-
const positions = getTuplePositions(schema)
|
|
330
|
-
if (positions) return tupleTypeToTs(schema, positions, options)
|
|
331
|
-
|
|
332
|
-
if (schema.items !== undefined && !Array.isArray(schema.items)) {
|
|
333
|
-
const itemType = getTypeScriptType(schema.items, options)
|
|
334
|
-
// Wrap union types in parentheses so `(A | B)[]` is not misread as `A | B[]`
|
|
335
|
-
const wrappedItemType = wrapUnion(itemType)
|
|
336
|
-
return options.readonly ? `readonly ${wrappedItemType}[]` : `${wrappedItemType}[]`
|
|
337
|
-
}
|
|
338
|
-
return options.readonly ? 'readonly unknown[]' : 'unknown[]'
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* The open-ended key signature a schema declares, or undefined when it declares
|
|
343
|
-
* none. Only a *schema-valued* `additionalProperties` counts: `true` is the JSON
|
|
344
|
-
* Schema default, so emitting `[key: string]: unknown` for it would widen nearly
|
|
345
|
-
* every generated type into uselessness, and `false` is a closed object.
|
|
346
|
-
*/
|
|
347
|
-
const getIndexSignature = (schema: SchemaNode, options: TypeOptions): { key: string; value: string } | undefined => {
|
|
348
|
-
const additionalProperties = schema.additionalProperties
|
|
349
|
-
if (additionalProperties !== undefined && typeof additionalProperties !== 'boolean') {
|
|
350
|
-
return { key: 'string', value: getTypeScriptType(additionalProperties, options) }
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const patternProperties = schema.patternProperties
|
|
354
|
-
if (patternProperties && typeof patternProperties === 'object') {
|
|
355
|
-
const entries = Object.entries(patternProperties)
|
|
356
|
-
if (entries.length === 0) return undefined
|
|
357
|
-
const valueTypes = entries.map(([, value]) =>
|
|
358
|
-
typeof value === 'boolean' ? getBooleanSubSchemaType(value) : getTypeScriptType(value, options),
|
|
359
|
-
)
|
|
360
|
-
// The `^x-` vendor-extension convention maps to a template-literal key, but
|
|
361
|
-
// only when it is the single pattern (a union of key patterns can't be expressed).
|
|
362
|
-
const key = entries.length === 1 && entries[0]?.[0] === '^x-' ? '`x-${string}`' : 'string'
|
|
363
|
-
return { key, value: unionOf(valueTypes) }
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
return undefined
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Renders the index-signature line to sit alongside declared properties.
|
|
371
|
-
*
|
|
372
|
-
* A `string` index must accept every declared property's type or TypeScript
|
|
373
|
-
* rejects the whole type (TS2411), so the signature is widened with them — and
|
|
374
|
-
* with `undefined` when any declared property is optional. A template-literal
|
|
375
|
-
* key (`` `x-${string}` ``) constrains a disjoint set of keys, so it needs no
|
|
376
|
-
* widening.
|
|
377
|
-
*/
|
|
378
|
-
const buildIndexSignatureLine = (
|
|
379
|
-
index: { key: string; value: string },
|
|
380
|
-
declaredTypes: readonly string[],
|
|
381
|
-
hasOptionalProperty: boolean,
|
|
382
|
-
options: TypeOptions,
|
|
383
|
-
): string => {
|
|
384
|
-
const readonlyPrefix = options.readonly ? 'readonly ' : ''
|
|
385
|
-
if (index.key !== 'string') return `${readonlyPrefix}[key: ${index.key}]: ${index.value}`
|
|
386
|
-
const widened = unionOf([
|
|
387
|
-
...index.value.split(' | '),
|
|
388
|
-
...declaredTypes,
|
|
389
|
-
...(hasOptionalProperty ? ['undefined'] : []),
|
|
390
|
-
])
|
|
391
|
-
return `${readonlyPrefix}[key: string]: ${widened}`
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/** The description (or `$comment` fallback) to emit as JSDoc above a property. */
|
|
395
|
-
const propertyDescription = (propSchema: JSONSchema): string | undefined => {
|
|
396
|
-
if (!isSchemaObject(propSchema)) return undefined
|
|
397
|
-
if (typeof propSchema.description === 'string') return propSchema.description
|
|
398
|
-
if (typeof propSchema.$comment === 'string') return propSchema.$comment
|
|
399
|
-
return undefined
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Renders an `object` schema: a property literal when it declares properties, a
|
|
404
|
-
* `Record`/index type when it only declares open-ended keys, else the bare
|
|
405
|
-
* `object`. Open-ended keys declared *alongside* properties become an index
|
|
406
|
-
* signature inside the literal rather than being dropped.
|
|
407
|
-
*/
|
|
408
|
-
const objectTypeToTs = (schema: SchemaNode, options: TypeOptions): string => {
|
|
409
|
-
const index = getIndexSignature(schema, options)
|
|
410
|
-
|
|
411
|
-
if (schema.properties && Object.keys(schema.properties).length > 0) {
|
|
412
|
-
const readonlyPrefix = options.readonly ? 'readonly ' : ''
|
|
413
|
-
// Build the required-key set once instead of an O(required) `includes`
|
|
414
|
-
// per property (O(properties × required) for a wide object).
|
|
415
|
-
const requiredSet = new Set<string>(Array.isArray(schema.required) ? schema.required : [])
|
|
416
|
-
const hasDescriptions = Object.values(schema.properties).some((p) => propertyDescription(p) !== undefined)
|
|
417
|
-
|
|
418
|
-
const declaredTypes: string[] = []
|
|
419
|
-
let hasOptionalProperty = false
|
|
420
|
-
// Entries are emitted ready to place: multi-line layout wants each
|
|
421
|
-
// declaration indented and `;`-terminated (with its JSDoc block already
|
|
422
|
-
// carrying its own indent), the compact one wants bare declarations.
|
|
423
|
-
const entries: string[] = []
|
|
424
|
-
for (const key in schema.properties) {
|
|
425
|
-
// schema.properties[key] is safe: key comes from iterating schema.properties
|
|
426
|
-
const propSchema = schema.properties[key] as JSONSchema
|
|
427
|
-
const isRequired = requiredSet.has(key)
|
|
428
|
-
const optional = isRequired ? '' : '?'
|
|
429
|
-
if (!isRequired) hasOptionalProperty = true
|
|
430
|
-
const propType = getTypeScriptType(propSchema, options)
|
|
431
|
-
declaredTypes.push(propType)
|
|
432
|
-
const declaration = readonlyPrefix + safeKey(key) + optional + ': ' + propType
|
|
433
|
-
if (!hasDescriptions) {
|
|
434
|
-
entries.push(declaration)
|
|
435
|
-
continue
|
|
436
|
-
}
|
|
437
|
-
const inlineDescription = propertyDescription(propSchema)
|
|
438
|
-
entries.push((inlineDescription ? buildInlinePropertyComment(inlineDescription) : '') + ' ' + declaration + ';')
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
if (index) {
|
|
442
|
-
const line = buildIndexSignatureLine(index, declaredTypes, hasOptionalProperty, options)
|
|
443
|
-
entries.push(hasDescriptions ? ' ' + line + ';' : line)
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// Descriptions force the multi-line layout — an inline `/** … */` per
|
|
447
|
-
// property would otherwise run the whole literal together on one line.
|
|
448
|
-
if (hasDescriptions) return '{\n' + entries.join('\n') + '\n}'
|
|
449
|
-
return '{ ' + entries.join('; ') + ' }'
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
if (index) {
|
|
453
|
-
return index.key === 'string' && !options.readonly
|
|
454
|
-
? recordType('string', index.value, options)
|
|
455
|
-
: recordType(index.key, index.value, options)
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
return 'object'
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* Renders the shape a schema declares *itself* — its `type`, or the record
|
|
463
|
-
* keywords it carries without one. Returns undefined when the schema declares
|
|
464
|
-
* no shape of its own (so composition keywords alone decide the type).
|
|
465
|
-
*/
|
|
466
|
-
const getLocalShapeType = (schema: SchemaNode, options: TypeOptions): string | undefined => {
|
|
467
|
-
// Object-like conditional schemas (`if`/`then`) describe one merged shape.
|
|
468
|
-
const conditionalResult = getConditionalObjectSchema(schema)
|
|
469
|
-
if (conditionalResult) {
|
|
470
|
-
const baseType = objectTypeToTs(conditionalResult.schema, options)
|
|
471
|
-
return conditionalResult.thenRef
|
|
472
|
-
? `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}`
|
|
473
|
-
: baseType
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
if (!schema.type) {
|
|
477
|
-
const index = getIndexSignature(schema, options)
|
|
478
|
-
if (index) return objectTypeToTs(schema, options)
|
|
479
|
-
|
|
480
|
-
if (schema.additionalProperties !== undefined && typeof schema.additionalProperties === 'boolean') {
|
|
481
|
-
return recordType('string', getBooleanSubSchemaType(schema.additionalProperties), options)
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
if (schema.properties && Object.keys(schema.properties).length > 0) return objectTypeToTs(schema, options)
|
|
485
|
-
|
|
486
|
-
if (schema.default !== undefined) {
|
|
487
|
-
if (typeof schema.default === 'string') return 'string'
|
|
488
|
-
if (typeof schema.default === 'number') return 'number'
|
|
489
|
-
if (typeof schema.default === 'boolean') return 'boolean'
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
return undefined
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
// Array-form `type` (the multi-type / nullable idiom, e.g. `["object","null"]`)
|
|
496
|
-
// is a union of the per-type renderings. Each member is rendered from the SAME
|
|
497
|
-
// schema, so a nullable object keeps its properties and a nullable array keeps
|
|
498
|
-
// its item type instead of collapsing to `Record<string, unknown>` / `unknown[]`.
|
|
499
|
-
if (Array.isArray(schema.type)) {
|
|
500
|
-
return unionOf(schema.type.map((type) => singleTypeToTs(schema, type, options)))
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
return singleTypeToTs(schema, schema.type, options)
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
/** Renders one JSON Schema `type` value against the schema that declared it. */
|
|
507
|
-
const singleTypeToTs = (schema: SchemaNode, type: JSONSchema.TypeValue, options: TypeOptions): string => {
|
|
508
|
-
switch (type) {
|
|
509
|
-
case 'string':
|
|
510
|
-
return 'string'
|
|
511
|
-
case 'number':
|
|
512
|
-
case 'integer':
|
|
513
|
-
return 'number'
|
|
514
|
-
case 'boolean':
|
|
515
|
-
return 'boolean'
|
|
516
|
-
case 'null':
|
|
517
|
-
return 'null'
|
|
518
|
-
case 'array':
|
|
519
|
-
return arrayTypeToTs(schema, options)
|
|
520
|
-
case 'object': {
|
|
521
|
-
const rendered = objectTypeToTs(schema, options)
|
|
522
|
-
// A shapeless object inside a union reads better as an indexable record
|
|
523
|
-
// than as the bare `object`, which cannot be indexed at all.
|
|
524
|
-
return rendered === 'object' && Array.isArray(schema.type) ? recordType('string', 'unknown', options) : rendered
|
|
525
|
-
}
|
|
526
|
-
default:
|
|
527
|
-
return 'unknown'
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* Converts a JSON Schema type to its TypeScript equivalent, ignoring any brand.
|
|
533
|
-
* Recursively handles nested objects and arrays.
|
|
534
|
-
*/
|
|
535
|
-
const getUnbrandedType = (schema: JSONSchema, options: TypeOptions = {}): string => {
|
|
536
|
-
// Boolean schema: `true` means any value is valid (unknown), `false` means no value is valid (never)
|
|
537
|
-
if (typeof schema === 'boolean') {
|
|
538
|
-
return getBooleanSubSchemaType(schema)
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
// Check if schema is an object (not a boolean schema)
|
|
542
|
-
if (typeof schema !== 'object' || schema === null) {
|
|
543
|
-
return 'unknown'
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// An x-mjst instanceOf hint means the value is a runtime class (e.g. Date)
|
|
547
|
-
// that JSON Schema cannot describe — emit the class name as the type directly.
|
|
548
|
-
const instanceOf = getMjstInstanceOf(schema)
|
|
549
|
-
if (instanceOf) {
|
|
550
|
-
return instanceOf
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
// An x-mjst primitive hint (e.g. bigint) names a non-JSON primitive — emit it
|
|
554
|
-
// directly as the TypeScript type.
|
|
555
|
-
const primitive = getMjstPrimitive(schema)
|
|
556
|
-
if (primitive) {
|
|
557
|
-
return primitive
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
// Handle $ref
|
|
561
|
-
if (schema.$ref) {
|
|
562
|
-
return refTypeName(schema.$ref, options)
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
// Handle $dynamicRef (used for recursive schemas)
|
|
566
|
-
if (schema.$dynamicRef) {
|
|
567
|
-
// For #meta, this refers to the Schema type itself (JSON Schema 2020-12 $dynamicAnchor pattern)
|
|
568
|
-
if (schema.$dynamicRef === '#meta') {
|
|
569
|
-
return `Schema${options.typeSuffix ?? ''}`
|
|
570
|
-
}
|
|
571
|
-
return refToName(schema.$dynamicRef, options.typeSuffix)
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
// Handle const - literal type
|
|
575
|
-
if (schema.const !== undefined) {
|
|
576
|
-
return JSON.stringify(schema.const)
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// Handle enum - union of literal types
|
|
580
|
-
if (schema.enum && schema.enum.length > 0) {
|
|
581
|
-
return unionOf(schema.enum.map((value) => JSON.stringify(value)))
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
// A schema may declare a shape of its own *and* compose others. Returning
|
|
585
|
-
// either alone silently drops half of what the document says, so the local
|
|
586
|
-
// shape, the `oneOf`/`anyOf` union, and every `allOf` member are combined into
|
|
587
|
-
// one intersection.
|
|
588
|
-
const members: string[] = []
|
|
589
|
-
const localType = getLocalShapeType(schema, options)
|
|
590
|
-
if (localType !== undefined) members.push(localType)
|
|
591
|
-
|
|
592
|
-
if (Array.isArray(schema.allOf)) {
|
|
593
|
-
for (const entry of schema.allOf) members.push(wrapUnion(getTypeScriptType(entry, options)))
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
const branches = Array.isArray(schema.oneOf) && schema.oneOf.length > 0 ? schema.oneOf : undefined
|
|
597
|
-
const anyBranches = Array.isArray(schema.anyOf) && schema.anyOf.length > 0 ? schema.anyOf : undefined
|
|
598
|
-
const unionBranches = branches ?? anyBranches
|
|
599
|
-
if (unionBranches) {
|
|
600
|
-
const union = unionOf(unionBranches.map((branch) => getTypeScriptType(branch, options)))
|
|
601
|
-
// A lone union is the whole type; alongside other members it is one factor
|
|
602
|
-
// of an intersection and needs its own parentheses.
|
|
603
|
-
if (members.length === 0 && !Array.isArray(schema.allOf)) return union
|
|
604
|
-
members.push(wrapUnion(union))
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
if (members.length === 0) return 'unknown'
|
|
608
|
-
return intersectionOf(members)
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
/**
|
|
612
|
-
* The types a schema composes alongside its own property block: every `allOf`
|
|
613
|
-
* member, a sibling `$ref` (2019-09+ allows one next to other keywords), and a
|
|
614
|
-
* sibling `oneOf`/`anyOf` union. Each is intersected onto the body — an `allOf`
|
|
615
|
-
* member written inline rather than as a `$ref` used to be dropped outright,
|
|
616
|
-
* taking its properties and its `required` list with it.
|
|
617
|
-
*/
|
|
618
|
-
const getCompositionMembers = (schema: JSONSchema, options: TypeOptions): string[] => {
|
|
619
|
-
if (!isSchemaObject(schema)) return []
|
|
620
|
-
const members: string[] = []
|
|
621
|
-
|
|
622
|
-
if (Array.isArray(schema.allOf)) {
|
|
623
|
-
for (const entry of schema.allOf) members.push(wrapUnion(getTypeScriptType(entry, options)))
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
if (typeof schema.$ref === 'string') {
|
|
627
|
-
members.push(refTypeName(schema.$ref, options))
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
const unionBranches =
|
|
631
|
-
Array.isArray(schema.oneOf) && schema.oneOf.length > 0
|
|
632
|
-
? schema.oneOf
|
|
633
|
-
: Array.isArray(schema.anyOf) && schema.anyOf.length > 0
|
|
634
|
-
? schema.anyOf
|
|
635
|
-
: undefined
|
|
636
|
-
if (unionBranches) {
|
|
637
|
-
members.push(wrapUnion(unionOf(unionBranches.map((branch) => getTypeScriptType(branch, options)))))
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
// `X & unknown` is `X`: a branch that only lists `required`, or a ref with no
|
|
641
|
-
// generated file, contributes nothing and would just add noise.
|
|
642
|
-
return [...new Set(members)].filter((member) => member !== 'unknown')
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* Generates a TypeScript type definition from a JSON Schema.
|
|
647
|
-
* Handles required vs optional properties based on the schema's required array.
|
|
648
|
-
* Uses $comment as inline JSDoc description when present.
|
|
649
|
-
*/
|
|
650
|
-
export const generateTypeDefinition = (schema: JSONSchema, typeName: string, options: TypeOptions = {}): string => {
|
|
651
|
-
const readonlyPrefix = options.readonly ? 'readonly ' : ''
|
|
652
|
-
|
|
653
|
-
// Handle non-object schemas first. An array-form `type` goes here too: it is a
|
|
654
|
-
// union (`{ … } | null`), not a single object body, so it cannot be emitted as
|
|
655
|
-
// a property block even when one of its members is an object.
|
|
656
|
-
if (!isObjectLikeSchema(schema) || (isSchemaObject(schema) && Array.isArray(schema.type))) {
|
|
657
|
-
const tsType = getTypeScriptType(schema, options)
|
|
658
|
-
let result = ''
|
|
659
|
-
|
|
660
|
-
const topLevelComment =
|
|
661
|
-
(isSchemaObject(schema) && typeof schema.description === 'string' && schema.description) ||
|
|
662
|
-
(isSchemaObject(schema) && typeof schema.$comment === 'string' && schema.$comment) ||
|
|
663
|
-
undefined
|
|
664
|
-
if (topLevelComment) {
|
|
665
|
-
result += buildJsDocBlock(typeName, topLevelComment)
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
result += `export type ${typeName} = ${tsType};`
|
|
669
|
-
return result
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
if (isObjectLikeSchema(schema)) {
|
|
673
|
-
const conditionalResult = getConditionalObjectSchema(schema)
|
|
674
|
-
const normalizedSchema = conditionalResult?.schema ?? schema
|
|
675
|
-
const conditionalThenRef = conditionalResult?.thenRef ?? null
|
|
676
|
-
let jsDocTitle: string | undefined
|
|
677
|
-
let jsDocDescription: string | undefined
|
|
678
|
-
|
|
679
|
-
const topLevelComment =
|
|
680
|
-
(isSchemaObject(schema) && typeof schema.description === 'string' && schema.description) ||
|
|
681
|
-
(isSchemaObject(schema) && typeof schema.$comment === 'string' && schema.$comment) ||
|
|
682
|
-
undefined
|
|
683
|
-
if (topLevelComment) {
|
|
684
|
-
jsDocTitle = typeName
|
|
685
|
-
jsDocDescription = topLevelComment
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
const hasProperties = normalizedSchema.properties && Object.keys(normalizedSchema.properties).length > 0
|
|
689
|
-
const hasAdditionalProperties =
|
|
690
|
-
normalizedSchema.additionalProperties && typeof normalizedSchema.additionalProperties === 'object'
|
|
691
|
-
const hasPatternProperties =
|
|
692
|
-
normalizedSchema.patternProperties &&
|
|
693
|
-
typeof normalizedSchema.patternProperties === 'object' &&
|
|
694
|
-
Object.keys(normalizedSchema.patternProperties).length > 0
|
|
695
|
-
|
|
696
|
-
// Handle objects with only patternProperties (no fixed properties)
|
|
697
|
-
if (!hasProperties && hasPatternProperties && normalizedSchema.patternProperties) {
|
|
698
|
-
const record =
|
|
699
|
-
patternPropertiesRecordType(normalizedSchema.patternProperties, options) ?? 'Record<string, unknown>'
|
|
700
|
-
|
|
701
|
-
let result = ''
|
|
702
|
-
if (jsDocTitle && jsDocDescription) {
|
|
703
|
-
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
704
|
-
}
|
|
705
|
-
result += `export type ${typeName} = ${record};`
|
|
706
|
-
|
|
707
|
-
return result
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
// Handle objects with only additionalProperties (no fixed properties)
|
|
711
|
-
if (!hasProperties && hasAdditionalProperties && normalizedSchema.additionalProperties) {
|
|
712
|
-
const additionalPropType = getTypeScriptType(normalizedSchema.additionalProperties, options)
|
|
713
|
-
|
|
714
|
-
let result = ''
|
|
715
|
-
if (jsDocTitle && jsDocDescription) {
|
|
716
|
-
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
717
|
-
}
|
|
718
|
-
result += `export type ${typeName} = {\n ${readonlyPrefix}[key: string]: ${additionalPropType};\n};`
|
|
719
|
-
|
|
720
|
-
return result
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
const schemaProps = normalizedSchema.properties ?? {}
|
|
724
|
-
const requiredSet = new Set<string>(Array.isArray(normalizedSchema.required) ? normalizedSchema.required : [])
|
|
725
|
-
const declaredTypes: string[] = []
|
|
726
|
-
let hasOptionalProperty = false
|
|
727
|
-
let properties = ''
|
|
728
|
-
let isFirstProp = true
|
|
729
|
-
const appendLine = (line: string): void => {
|
|
730
|
-
if (!isFirstProp) properties += '\n'
|
|
731
|
-
isFirstProp = false
|
|
732
|
-
properties += line
|
|
733
|
-
}
|
|
734
|
-
for (const key in schemaProps) {
|
|
735
|
-
// schemaProps[key] is safe: key comes from iterating schemaProps
|
|
736
|
-
const propSchema = schemaProps[key]!
|
|
737
|
-
const isRequired = requiredSet.has(key)
|
|
738
|
-
const optional = isRequired ? '' : '?'
|
|
739
|
-
if (!isRequired) hasOptionalProperty = true
|
|
740
|
-
const propType = getTypeScriptType(propSchema, options)
|
|
741
|
-
declaredTypes.push(propType)
|
|
742
|
-
const quotedKey = readonlyPrefix + safeKey(key)
|
|
743
|
-
|
|
744
|
-
// Add JSDoc comment from $comment or description if available
|
|
745
|
-
const inlineDescription = propertyDescription(propSchema)
|
|
746
|
-
appendLine(
|
|
747
|
-
inlineDescription
|
|
748
|
-
? buildInlinePropertyComment(inlineDescription) + ' ' + quotedKey + optional + ': ' + propType + ';'
|
|
749
|
-
: ' ' + quotedKey + optional + ': ' + propType + ';',
|
|
750
|
-
)
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
// Open-ended keys declared alongside fixed properties become an index
|
|
754
|
-
// signature inside the same body — dropping them used to erase everything a
|
|
755
|
-
// schema said about the keys it does not name.
|
|
756
|
-
const index = getIndexSignature(normalizedSchema, options)
|
|
757
|
-
if (index) {
|
|
758
|
-
appendLine(' ' + buildIndexSignatureLine(index, declaredTypes, hasOptionalProperty, options) + ';')
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
let result = ''
|
|
762
|
-
if (jsDocTitle && jsDocDescription) {
|
|
763
|
-
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
let typeBody = properties === '' ? '{}' : '{\n' + properties + '\n}'
|
|
767
|
-
|
|
768
|
-
if (conditionalThenRef) {
|
|
769
|
-
typeBody += ' & ' + refToName(conditionalThenRef, options.typeSuffix)
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
for (const intersectionType of getCompositionMembers(schema, options)) {
|
|
773
|
-
typeBody += ' & ' + intersectionType
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
if (isNullableSchema(schema)) typeBody = withNull(typeBody)
|
|
777
|
-
|
|
778
|
-
result += 'export type ' + typeName + ' = ' + typeBody + ';'
|
|
779
|
-
|
|
780
|
-
return result
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
return 'export type ' + typeName + ' = unknown;'
|
|
784
|
-
}
|