@@ -33,6 +33,22 @@ const createObjectMemberType = ({ factory }: ts.TransformationContext, members:
3333 return factory . createTypeLiteralNode ( members ) ;
3434} ;
3535
36+ const createOptional = ( { factory } : ts . TransformationContext ) => {
37+ return factory . createToken ( ts . SyntaxKind . QuestionToken ) ;
38+ } ;
39+
40+ const createPropertySignatures = ( context : ts . TransformationContext , properties : { [ key : string ] : JSONSchema4 } ) : ts . PropertySignature [ ] => {
41+ return Object . entries ( properties ) . map ( ( [ key , childSchema ] ) => {
42+ childSchema . required ;
43+ return context . factory . createPropertySignature (
44+ undefined ,
45+ context . factory . createIdentifier ( key ) ,
46+ childSchema . required === false ? createOptional ( context ) : undefined ,
47+ createTypeNode ( context , childSchema ) ,
48+ ) ;
49+ } ) ;
50+ } ;
51+
3652/**
3753 * Don't use root schema root
3854 */
@@ -41,14 +57,7 @@ const createTypeNode = (context: ts.TransformationContext, schema: JSONSchema4):
4157 if ( ! schema . properties ) {
4258 return createObjectMemberType ( context , [ ] ) ;
4359 }
44- const members = Object . entries ( schema . properties ) . map ( ( [ key , childSchema ] ) => {
45- return context . factory . createPropertySignature (
46- undefined ,
47- context . factory . createIdentifier ( key ) ,
48- undefined ,
49- createTypeNode ( context , childSchema ) ,
50- ) ;
51- } ) ;
60+ const members = createPropertySignatures ( context , schema . properties ) ;
5261 return createObjectMemberType ( context , members ) ;
5362 } else if ( schema . type === "string" ) {
5463 return createStringType ( context ) ;
@@ -78,14 +87,7 @@ const createInterfaceFromRootObjectSchema = (context: ts.TransformationContext,
7887 if ( schema . type !== "object" || ! schema . properties ) {
7988 return [ ] ;
8089 }
81- return Object . entries ( schema . properties ) . map ( ( [ key , childSchema ] ) => {
82- return context . factory . createPropertySignature (
83- undefined ,
84- context . factory . createIdentifier ( key ) ,
85- undefined ,
86- createTypeNode ( context , childSchema ) ,
87- ) ;
88- } ) ;
90+ return createPropertySignatures ( context , schema . properties ) ;
8991} ;
9092
9193/**
0 commit comments