@@ -149,6 +149,9 @@ export interface Definition extends Omit<JSONSchema7, RedefinedFields> {
149
149
} ;
150
150
}
151
151
152
+ /** A looser Definition type that allows for indexing with arbitrary strings. */
153
+ type DefinitionIndex = { [ key : string ] : Definition [ keyof Definition ] } ;
154
+
152
155
export type SymbolRef = {
153
156
name : string ;
154
157
typeName : string ;
@@ -181,7 +184,7 @@ function extend(target: any, ..._: any[]): any {
181
184
}
182
185
183
186
function unique ( arr : string [ ] ) : string [ ] {
184
- const temp = { } ;
187
+ const temp : Record < string , true > = { } ;
185
188
for ( const e of arr ) {
186
189
temp [ e ] = true ;
187
190
}
@@ -284,7 +287,7 @@ function resolveTupleType(propertyType: ts.Type): ts.TupleTypeNode | null {
284
287
return propertyType as any ;
285
288
}
286
289
287
- const simpleTypesAllowedProperties = {
290
+ const simpleTypesAllowedProperties : Record < string , true > = {
288
291
type : true ,
289
292
description : true ,
290
293
} ;
@@ -328,11 +331,11 @@ function makeNullable(def: Definition): Definition {
328
331
if ( union ) {
329
332
union . push ( { type : "null" } ) ;
330
333
} else {
331
- const subdef = { } ;
332
- for ( var k in def ) {
334
+ const subdef : DefinitionIndex = { } ;
335
+ for ( var k in def as any ) {
333
336
if ( def . hasOwnProperty ( k ) ) {
334
- subdef [ k ] = def [ k ] ;
335
- delete def [ k ] ;
337
+ subdef [ k ] = def [ k as keyof Definition ] ;
338
+ delete def [ k as keyof typeof def ] ;
336
339
}
337
340
}
338
341
def . anyOf = [ subdef , { type : "null" } ] ;
@@ -441,7 +444,7 @@ const annotationKeywords: { [k in keyof typeof validationKeywords]?: true } = {
441
444
$ref : true ,
442
445
} ;
443
446
444
- const subDefinitions = {
447
+ const subDefinitions : Record < string , true > = {
445
448
items : true ,
446
449
additionalProperties : true ,
447
450
contains : true ,
@@ -550,7 +553,7 @@ export class JsonSchemaGenerator {
550
553
/**
551
554
* Parse the comments of a symbol into the definition and other annotations.
552
555
*/
553
- private parseCommentsIntoDefinition ( symbol : ts . Symbol , definition : Definition , otherAnnotations : { } ) : void {
556
+ private parseCommentsIntoDefinition ( symbol : ts . Symbol , definition : Definition , otherAnnotations : Record < string , true > ) : void {
554
557
if ( ! symbol ) {
555
558
return ;
556
559
}
@@ -613,24 +616,25 @@ export class JsonSchemaGenerator {
613
616
if ( match ) {
614
617
const k = match [ 1 ] ;
615
618
const v = match [ 2 ] ;
616
- definition [ name ] = { ...definition [ name ] , [ k ] : v ? parseValue ( symbol , k , v ) : true } ;
619
+ ( definition as DefinitionIndex ) [ name ] = { ...( definition as Record < string , Record < string , unknown > > ) [ name ] , [ k ] : v ? parseValue ( symbol , k , v ) : true } ;
617
620
return ;
618
621
}
619
622
}
620
623
621
624
// In TypeScript 3.7+, the "." is kept as part of the annotation name
622
625
if ( name . includes ( "." ) ) {
623
626
const parts = name . split ( "." ) ;
624
- if ( parts . length === 2 && subDefinitions [ parts [ 0 ] ] ) {
625
- definition [ parts [ 0 ] ] = {
626
- ...definition [ parts [ 0 ] ] ,
627
+ const key = parts [ 0 ] as keyof Definition ;
628
+ if ( parts . length === 2 && subDefinitions [ key ] ) {
629
+ ( definition as DefinitionIndex ) [ key ] = {
630
+ ...definition [ key ] as Record < string , unknown > ,
627
631
[ parts [ 1 ] ] : text ? parseValue ( symbol , name , text ) : true ,
628
632
} ;
629
633
}
630
634
}
631
635
632
- if ( validationKeywords [ name ] || this . userValidationKeywords [ name ] ) {
633
- definition [ name ] = text === undefined ? "" : parseValue ( symbol , name , text ) ;
636
+ if ( validationKeywords [ name as keyof typeof validationKeywords ] || this . userValidationKeywords [ name ] ) {
637
+ ( definition as DefinitionIndex ) [ name ] = text === undefined ? "" : parseValue ( symbol , name , text ) ;
634
638
} else {
635
639
// special annotations
636
640
otherAnnotations [ doc . name ] = true ;
@@ -939,7 +943,7 @@ export class JsonSchemaGenerator {
939
943
940
944
private getUnionDefinition (
941
945
unionType : ts . UnionType ,
942
- unionModifier : string ,
946
+ unionModifier : keyof Definition ,
943
947
definition : Definition
944
948
) : Definition {
945
949
const enumValues : PrimitiveType [ ] = [ ] ;
@@ -1030,11 +1034,11 @@ export class JsonSchemaGenerator {
1030
1034
// If we already have a more specific description, don't overwrite it.
1031
1035
continue ;
1032
1036
}
1033
- definition [ k ] = schemas [ 0 ] [ k ] ;
1037
+ ( definition as DefinitionIndex ) [ k ] = schemas [ 0 ] [ k as keyof Definition ] ;
1034
1038
}
1035
1039
}
1036
1040
} else {
1037
- definition [ unionModifier ] = schemas ;
1041
+ ( definition as DefinitionIndex ) [ unionModifier ] = schemas ;
1038
1042
}
1039
1043
return definition ;
1040
1044
}
@@ -1070,7 +1074,7 @@ export class JsonSchemaGenerator {
1070
1074
if ( schemas . length === 1 ) {
1071
1075
for ( const k in schemas [ 0 ] ) {
1072
1076
if ( schemas [ 0 ] . hasOwnProperty ( k ) ) {
1073
- definition [ k ] = schemas [ 0 ] [ k ] ;
1077
+ ( definition as DefinitionIndex ) [ k ] = schemas [ 0 ] [ k as keyof Definition ] ;
1074
1078
}
1075
1079
}
1076
1080
} else {
@@ -1175,7 +1179,7 @@ export class JsonSchemaGenerator {
1175
1179
}
1176
1180
}
1177
1181
1178
- const propertyDefinitions = props . reduce ( ( all , prop ) => {
1182
+ const propertyDefinitions = props . reduce < Record < string , Definition > > ( ( all , prop ) => {
1179
1183
const propertyName = prop . getName ( ) ;
1180
1184
const propDef = this . getDefinitionForProperty ( prop , node ) ;
1181
1185
if ( propDef != null ) {
@@ -1274,7 +1278,7 @@ export class JsonSchemaGenerator {
1274
1278
private getTypeDefinition (
1275
1279
typ : ts . Type ,
1276
1280
asRef = this . args . ref ,
1277
- unionModifier : string = "anyOf" ,
1281
+ unionModifier : keyof Definition = "anyOf" ,
1278
1282
prop ?: ts . Symbol ,
1279
1283
reffedType ?: ts . Symbol ,
1280
1284
pairedSymbol ?: ts . Symbol ,
@@ -1400,7 +1404,7 @@ export class JsonSchemaGenerator {
1400
1404
}
1401
1405
1402
1406
// Parse comments
1403
- const otherAnnotations = { } ;
1407
+ const otherAnnotations : Record < string , true > = { } ;
1404
1408
this . parseCommentsIntoDefinition ( reffedType ! , definition , otherAnnotations ) ; // handle comments in the type alias declaration
1405
1409
this . parseCommentsIntoDefinition ( symbol ! , definition , otherAnnotations ) ;
1406
1410
this . parseCommentsIntoDefinition ( typ . aliasSymbol ! , definition , otherAnnotations ) ;
@@ -1492,8 +1496,8 @@ export class JsonSchemaGenerator {
1492
1496
this . recursiveTypeRef . delete ( fullTypeName ) ;
1493
1497
// If the type was recursive (there is reffedDefinitions) - lets replace it to reference
1494
1498
if ( this . reffedDefinitions [ fullTypeName ] ) {
1495
- const annotations = Object . entries ( returnedDefinition ) . reduce ( ( acc , [ key , value ] ) => {
1496
- if ( annotationKeywords [ key ] && typeof value !== undefined ) {
1499
+ const annotations = Object . entries ( returnedDefinition ) . reduce < Record < string , unknown > > ( ( acc , [ key , value ] ) => {
1500
+ if ( annotationKeywords [ key as keyof typeof annotationKeywords ] && typeof value !== undefined ) {
1497
1501
acc [ key ] = value ;
1498
1502
}
1499
1503
return acc ;
@@ -1551,7 +1555,11 @@ export class JsonSchemaGenerator {
1551
1555
}
1552
1556
1553
1557
public getSchemaForSymbols ( symbolNames : string [ ] , includeReffedDefinitions : boolean = true , includeAllOverrides : boolean = false ) : Definition {
1554
- const root = {
1558
+ const root : {
1559
+ $id ?: string ,
1560
+ $schema : string ,
1561
+ definitions : Record < string , Definition >
1562
+ } = {
1555
1563
$schema : "http://json-schema.org/draft-07/schema#" ,
1556
1564
definitions : { } ,
1557
1565
} ;
@@ -1660,7 +1668,7 @@ export function buildGenerator(
1660
1668
1661
1669
for ( const pref in args ) {
1662
1670
if ( args . hasOwnProperty ( pref ) ) {
1663
- settings [ pref ] = args [ pref ] ;
1671
+ ( settings as Record < string , Partial < Args > [ keyof Args ] > ) [ pref as keyof Args ] = args [ pref as keyof Args ] ;
1664
1672
}
1665
1673
}
1666
1674
0 commit comments