@@ -7,11 +7,11 @@ import JsonToTypeScriptConversionOptions from './JsonToTypeScriptConversionOptio
7
7
import isValidJsIdentifier , { isValidTsTypeFieldName } from '../../../utils/isValidJsIdentifier' ;
8
8
9
9
export interface ITypeScriptType {
10
- stringifyReference ( ) : string ;
10
+ stringifyReference ( options : JsonToTypeScriptConversionOptions ) : string ;
11
11
}
12
12
13
13
export interface IDeclarable {
14
- stringifyDeclarationBody ( ) : string ;
14
+ stringifyDeclarationBody ( options : JsonToTypeScriptConversionOptions ) : string ;
15
15
}
16
16
17
17
export interface IDeclarableTypeScriptType extends IDeclarable {
@@ -35,67 +35,67 @@ export class DeclarableTypeScriptType implements IDeclarableTypeScriptType {
35
35
36
36
return `${ getExportKeyword ( options . exportType ) } type ${ stringifyTypeName (
37
37
this . name
38
- ) } = ${ this . stringifyDeclarationBody ( ) } ;`;
38
+ ) } = ${ this . stringifyDeclarationBody ( options ) } ;`;
39
39
}
40
40
41
- stringifyDeclarationBody ( ) : string {
41
+ stringifyDeclarationBody ( options : JsonToTypeScriptConversionOptions ) : string {
42
42
if ( isString ( this . type ) ) {
43
- return this . type ;
43
+ return getTypeScriptTypeReference ( this . type , options ) ;
44
44
}
45
45
46
46
return 'stringifyDeclarationBody' in this . type
47
- ? this . type . stringifyDeclarationBody ( )
47
+ ? this . type . stringifyDeclarationBody ( options )
48
48
: 'stringifyReference' in this . type
49
- ? this . type . stringifyReference ( )
49
+ ? this . type . stringifyReference ( options )
50
50
: '' ;
51
51
}
52
52
}
53
53
54
54
export class TypeScriptUnknown implements ITypeScriptType {
55
55
public readonly isUnknown = true ;
56
56
57
- stringifyReference ( ) : string {
58
- return 'unknown' ;
57
+ stringifyReference ( options : JsonToTypeScriptConversionOptions ) : string {
58
+ return options . unknownType ?? 'unknown' ;
59
59
}
60
60
}
61
61
62
62
export class TypeScriptObjectField implements IDeclarable {
63
63
public constructor ( public readonly type : TypeScriptType , public readonly isOptional = false ) { }
64
64
65
- stringifyDeclarationBody ( ) : string {
66
- return `${ this . isOptional ? '?' : '' } : ${ getTypeScriptTypeReference ( this . type ) } ` ;
65
+ stringifyDeclarationBody ( options : JsonToTypeScriptConversionOptions ) : string {
66
+ return `${ this . isOptional ? '?' : '' } : ${ getTypeScriptTypeReference ( this . type , options ) } ` ;
67
67
}
68
68
}
69
69
70
70
export class TypeScriptInterface implements ITypeScriptType , IDeclarableTypeScriptType {
71
71
public constructor ( public name : string , public readonly fields : Record < string , TypeScriptObjectField > ) { }
72
72
73
- stringifyDeclaration ( { exportType } : JsonToTypeScriptConversionOptions ) : string {
74
- return `${ getExportKeyword (
75
- exportType
76
- ) } interface ${ this . stringifyReference ( ) } ${ this . stringifyDeclarationBody ( ) } `;
73
+ stringifyDeclaration ( options : JsonToTypeScriptConversionOptions ) : string {
74
+ return `${ getExportKeyword ( options . exportType ) } interface ${ this . stringifyReference (
75
+ options
76
+ ) } ${ this . stringifyDeclarationBody ( options ) } `;
77
77
}
78
78
79
- stringifyDeclarationBody ( ) : string {
79
+ stringifyDeclarationBody ( options : JsonToTypeScriptConversionOptions ) : string {
80
80
return (
81
81
'{\n' +
82
82
mapObject ( this . fields , ( key , field ) => {
83
- return ` ${ stringifyFieldName ( key ) } ${ field . stringifyDeclarationBody ( ) } ;` ;
83
+ return ` ${ stringifyFieldName ( key ) } ${ field . stringifyDeclarationBody ( options ) } ;` ;
84
84
} ) . join ( '\n' ) +
85
85
'\n}'
86
86
) ;
87
87
}
88
88
89
- stringifyReference ( ) : string {
89
+ stringifyReference ( options : JsonToTypeScriptConversionOptions ) : string {
90
90
return stringifyTypeName ( this . name ) ;
91
91
}
92
92
}
93
93
94
94
export class TypeScriptArray implements ITypeScriptType {
95
95
public constructor ( public readonly type : TypeScriptType ) { }
96
96
97
- stringifyReference ( ) : string {
98
- const typeReference = getTypeScriptTypeReference ( this . type ) ;
97
+ stringifyReference ( options : JsonToTypeScriptConversionOptions ) : string {
98
+ const typeReference = getTypeScriptTypeReference ( this . type , options ) ;
99
99
100
100
if ( this . type instanceof TypeScriptUnion ) {
101
101
return `Array<${ typeReference } >` ;
@@ -108,7 +108,7 @@ export class TypeScriptArray implements ITypeScriptType {
108
108
export abstract class TypeScriptTypesCombination implements ITypeScriptType {
109
109
protected constructor ( public name : string , public readonly types : TypeScriptType [ ] ) { }
110
110
111
- abstract stringifyReference ( ) : string ;
111
+ abstract stringifyReference ( options : JsonToTypeScriptConversionOptions ) : string ;
112
112
}
113
113
114
114
export class TypeScriptUnion extends TypeScriptTypesCombination {
@@ -121,12 +121,12 @@ export class TypeScriptUnion extends TypeScriptTypesCombination {
121
121
// return `${getExportKeyword(exportType)}type ${this.name} = ${this.stringifyDeclarationBody()};`;
122
122
// }
123
123
124
- stringifyDeclarationBody ( ) : string {
125
- return this . types . map ( getTypeScriptTypeReference ) . join ( ' | ' ) ;
124
+ stringifyDeclarationBody ( options : JsonToTypeScriptConversionOptions ) : string {
125
+ return this . types . map ( ( type ) => getTypeScriptTypeReference ( type , options ) ) . join ( ' | ' ) ;
126
126
}
127
127
128
- stringifyReference ( ) : string {
129
- return this . stringifyDeclarationBody ( ) ;
128
+ stringifyReference ( options : JsonToTypeScriptConversionOptions ) : string {
129
+ return this . stringifyDeclarationBody ( options ) ;
130
130
}
131
131
}
132
132
@@ -136,12 +136,12 @@ export class TypeScriptTuple extends TypeScriptTypesCombination {
136
136
super ( name , types ) ;
137
137
}
138
138
139
- stringifyDeclarationBody ( ) : string {
140
- return '[' + this . types . map ( getTypeScriptTypeReference ) . join ( ', ' ) + ']' ;
139
+ stringifyDeclarationBody ( options : JsonToTypeScriptConversionOptions ) : string {
140
+ return '[' + this . types . map ( ( type ) => getTypeScriptTypeReference ( type , options ) ) . join ( ', ' ) + ']' ;
141
141
}
142
142
143
- stringifyReference ( ) : string {
144
- return this . stringifyDeclarationBody ( ) ;
143
+ stringifyReference ( options : JsonToTypeScriptConversionOptions ) : string {
144
+ return this . stringifyDeclarationBody ( options ) ;
145
145
}
146
146
}
147
147
0 commit comments