@@ -167,28 +167,17 @@ export function escapeId(name: string) {
167167}
168168
169169/**
170- * Returns the TypeScript type for the given type and options
170+ * Appends | null to the given type
171171 */
172- export function tsType ( schemaOrRef : SchemaOrRef | undefined , options : Options , openApi : OpenAPIObject , container ?: Model ) : string {
173- if ( ! schemaOrRef ) {
174- // No schema
175- return 'any' ;
176- }
177- if ( schemaOrRef . $ref ) {
178- // A reference
179- const resolved = resolveRef ( openApi , schemaOrRef . $ref ) ;
180- const nullable = ! ! ( resolved && ( resolved as SchemaObject ) . nullable ) ;
181- const prefix = nullable ? 'null | ' : '' ;
182- const name = simpleName ( schemaOrRef . $ref ) ;
183- if ( container && container . name === name ) {
184- // When referencing the same container, use its type name
185- return prefix + container . typeName ;
186- } else {
187- return prefix + qualifiedName ( name , options ) ;
188- }
172+ function maybeAppendNull ( type : string , nullable : boolean ) {
173+ if ( ` ${ type } ` . includes ( 'null' ) || ! nullable ) {
174+ // The type itself already includes null
175+ return type ;
189176 }
190- const schema = schemaOrRef as SchemaObject ;
177+ return ( type . includes ( ' ' ) ? `(${ type } )` : type ) + ( nullable ? ' | null' : '' ) ;
178+ }
191179
180+ function rawTsType ( schema : SchemaObject , options : Options , openApi : OpenAPIObject , container ?: Model ) : string {
192181 // An union of types
193182 const union = schema . oneOf || schema . anyOf || [ ] ;
194183 if ( union . length > 0 ) {
@@ -204,10 +193,7 @@ export function tsType(schemaOrRef: SchemaOrRef | undefined, options: Options, o
204193 // An array
205194 if ( type === 'array' || schema . items ) {
206195 const items = schema . items || { } ;
207- let itemsType = tsType ( items , options , openApi , container ) ;
208- if ( ( items as any ) [ 'nullable' ] && ! itemsType . includes ( ' | null' ) ) {
209- itemsType += ' | null' ;
210- }
196+ const itemsType = tsType ( items , options , openApi , container ) ;
211197 return `Array<${ itemsType } >` ;
212198 }
213199
@@ -244,10 +230,7 @@ export function tsType(schemaOrRef: SchemaOrRef | undefined, options: Options, o
244230 if ( ! propRequired ) {
245231 result += '?' ;
246232 }
247- let propertyType = tsType ( property , options , openApi , container ) ;
248- if ( ( property as SchemaObject ) . nullable ) {
249- propertyType = `${ propertyType } | null` ;
250- }
233+ const propertyType = tsType ( property , options , openApi , container ) ;
251234 result += `: ${ propertyType } ;\n` ;
252235 }
253236 if ( schema . additionalProperties ) {
@@ -277,10 +260,33 @@ export function tsType(schemaOrRef: SchemaOrRef | undefined, options: Options, o
277260 return 'Blob' ;
278261 }
279262
280- // A simple type
263+ // A simple type (integer doesn't exist as type in JS, use number instead)
281264 return type === 'integer' ? 'number' : type ;
282265}
283266
267+ /**
268+ * Returns the TypeScript type for the given type and options
269+ */
270+ export function tsType ( schemaOrRef : SchemaOrRef | undefined , options : Options , openApi : OpenAPIObject , container ?: Model ) : string {
271+ if ( ! schemaOrRef ) {
272+ // No schema
273+ return 'any' ;
274+ }
275+
276+ if ( schemaOrRef . $ref ) {
277+ // A reference
278+ const resolved = resolveRef ( openApi , schemaOrRef . $ref ) as SchemaObject ;
279+ const name = simpleName ( schemaOrRef . $ref ) ;
280+ // When referencing the same container, use its type name
281+ return maybeAppendNull ( ( container && container . name === name ) ? container . typeName : qualifiedName ( name , options ) , ! ! resolved . nullable ) ;
282+ }
283+
284+ // Resolve the actual type (maybe nullable)
285+ const schema = schemaOrRef as SchemaObject ;
286+ const type = rawTsType ( schema , options , openApi , container ) ;
287+ return maybeAppendNull ( type , ! ! schema . nullable ) ;
288+ }
289+
284290/**
285291 * Resolves a reference
286292 * @param ref The reference name, such as #/components/schemas/Name, or just Name
0 commit comments