@@ -248,24 +248,32 @@ class SchemaUtils {
248248 ) ;
249249 }
250250
251- const primitiveType = this . getSchemaPrimitiveType ( schema ) ;
252-
253- if ( primitiveType == null ) return this . config . Ts . Keyword . Any ;
254-
255251 let resultType ;
256252
257- const typeAlias =
258- _ . get ( this . config . primitiveTypes , [ primitiveType , schema . format ] ) ||
259- _ . get ( this . config . primitiveTypes , [ primitiveType , '$default' ] ) ||
260- this . config . primitiveTypes [ primitiveType ] ;
261-
262- if ( _ . isFunction ( typeAlias ) ) {
263- resultType = typeAlias ( schema , this ) ;
253+ if ( this . isConstantSchema ( schema ) ) {
254+ resultType = this . formatJsValue ( schema . const ) ;
264255 } else {
265- resultType = typeAlias || primitiveType ;
256+ const primitiveType = this . getSchemaPrimitiveType ( schema ) ;
257+
258+ if ( primitiveType == null ) {
259+ return this . config . Ts . Keyword . Any ;
260+ }
261+
262+ const typeAlias =
263+ _ . get ( this . config . primitiveTypes , [ primitiveType , schema . format ] ) ||
264+ _ . get ( this . config . primitiveTypes , [ primitiveType , '$default' ] ) ||
265+ this . config . primitiveTypes [ primitiveType ] ;
266+
267+ if ( _ . isFunction ( typeAlias ) ) {
268+ resultType = typeAlias ( schema , this ) ;
269+ } else {
270+ resultType = typeAlias || primitiveType ;
271+ }
266272 }
267273
268- if ( ! resultType ) return this . config . Ts . Keyword . Any ;
274+ if ( ! resultType ) {
275+ return this . config . Ts . Keyword . Any ;
276+ }
269277
270278 return this . checkAndAddRequiredKeys (
271279 schema ,
@@ -284,6 +292,31 @@ class SchemaUtils {
284292 ) ,
285293 ) ;
286294 } ;
295+
296+ isConstantSchema ( schema ) {
297+ return 'const' in schema ;
298+ }
299+
300+ formatJsValue = ( value ) => {
301+ switch ( typeof value ) {
302+ case 'string' : {
303+ return this . config . Ts . StringValue ( value ) ;
304+ }
305+ case 'boolean' : {
306+ return this . config . Ts . BooleanValue ( value ) ;
307+ }
308+ case 'number' : {
309+ return this . config . Ts . NumberValue ( value ) ;
310+ }
311+ default : {
312+ if ( value === null ) {
313+ return this . config . Ts . NullValue ( value ) ;
314+ }
315+
316+ return this . config . Ts . Keyword . Any ;
317+ }
318+ }
319+ } ;
287320}
288321
289322module . exports = {
0 commit comments