@@ -47,11 +47,13 @@ describe(SchemaTypeGenerator.name, () => {
4747 members : [
4848 {
4949 key : { name : '_id' , type : 'Identifier' } ,
50+ optional : undefined ,
5051 type : 'TSPropertySignature' ,
5152 typeAnnotation : { type : 'TSTypeAnnotation' , typeAnnotation : { type : 'TSStringKeyword' } } ,
5253 } ,
5354 {
5455 key : { name : '_type' , type : 'Identifier' } ,
56+ optional : undefined ,
5557 type : 'TSPropertySignature' ,
5658 typeAnnotation : {
5759 type : 'TSTypeAnnotation' ,
@@ -360,6 +362,52 @@ describe(SchemaTypeGenerator.name, () => {
360362 )
361363 } )
362364
365+ test ( 'quotes non-identifier keys, preserves valid identifier keys' , ( ) => {
366+ const schema = new SchemaTypeGenerator ( [
367+ {
368+ type : 'type' ,
369+ name : 'objectWithMixedKeys' ,
370+ value : {
371+ type : 'object' ,
372+ attributes : {
373+ // Valid identifiers - should NOT be quoted
374+ 'normalKey' : { type : 'objectAttribute' , value : { type : 'string' } } ,
375+ '_privateKey' : { type : 'objectAttribute' , value : { type : 'string' } } ,
376+ '$dollarKey' : { type : 'objectAttribute' , value : { type : 'string' } } ,
377+ 'camelCase' : { type : 'objectAttribute' , value : { type : 'string' } } ,
378+ 'PascalCase' : { type : 'objectAttribute' , value : { type : 'string' } } ,
379+ 'UPPER_SNAKE' : { type : 'objectAttribute' , value : { type : 'string' } } ,
380+ // Invalid identifiers - MUST be quoted
381+ 'kebab-case' : { type : 'objectAttribute' , value : { type : 'string' } } ,
382+ 'dot.notation' : { type : 'objectAttribute' , value : { type : 'string' } } ,
383+ 'with spaces' : { type : 'objectAttribute' , value : { type : 'string' } } ,
384+ '123startsWithNumber' : { type : 'objectAttribute' , value : { type : 'string' } } ,
385+ 'special@char' : { type : 'objectAttribute' , value : { type : 'string' } } ,
386+ '' : { type : 'objectAttribute' , value : { type : 'string' } } ,
387+ } ,
388+ } ,
389+ } ,
390+ ] )
391+
392+ const objectType = schema . getType ( 'objectWithMixedKeys' ) ?. tsType
393+ expect ( generateCode ( objectType ) ) . toMatchInlineSnapshot ( `
394+ "{
395+ normalKey: string;
396+ _privateKey: string;
397+ $dollarKey: string;
398+ camelCase: string;
399+ PascalCase: string;
400+ UPPER_SNAKE: string;
401+ "kebab-case": string;
402+ "dot.notation": string;
403+ "with spaces": string;
404+ "123startsWithNumber": string;
405+ "special@char": string;
406+ "": string;
407+ }"
408+ ` )
409+ } )
410+
363411 test ( 'generates TS Types for objects' , ( ) => {
364412 const schema = new SchemaTypeGenerator ( [
365413 {
0 commit comments