@@ -11,7 +11,7 @@ import {
11
11
} from "openapi3-ts/oas30" ;
12
12
import { singular } from "pluralize" ;
13
13
import { isValidIdentifier } from "tsutils" ;
14
- import ts , { factory as f } from "typescript" ;
14
+ import ts , { factory as f , isIdentifierStart } from "typescript" ;
15
15
import { getReferenceSchema } from "./getReference" ;
16
16
17
17
type RemoveIndex < T > = {
@@ -56,11 +56,24 @@ export const schemaToTypeAliasDeclaration = (
56
56
const jsDocNode = isSchemaObject ( schema )
57
57
? getJSDocComment ( schema , context )
58
58
: undefined ;
59
+
60
+ let identifier = pascal ( name ) ;
61
+
62
+ // If the identifier does not start with a valid character, prefix it with an underscore.
63
+ if ( ! isIdentifierStart ( identifier . charCodeAt ( 0 ) , ts . ScriptTarget . Latest ) ) {
64
+ identifier = `_${ identifier } ` ;
65
+ }
66
+
67
+ // If the identifier is still not valid, remove invalid characters.
68
+ if ( ! isValidIdentifier ( identifier ) ) {
69
+ identifier = identifier . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, "" ) ;
70
+ }
71
+
59
72
const declarationNode = f . createTypeAliasDeclaration (
60
73
[ f . createModifier ( ts . SyntaxKind . ExportKeyword ) ] ,
61
- pascal ( name ) ,
74
+ identifier ,
62
75
undefined ,
63
- getType ( schema , context , name )
76
+ getType ( schema , context , identifier )
64
77
) ;
65
78
66
79
return jsDocNode ? [ jsDocNode , declarationNode ] : [ declarationNode ] ;
@@ -89,10 +102,10 @@ export const getType = (
89
102
90
103
let refNode : ts . TypeNode = f . createTypeReferenceNode (
91
104
namespace === context . currentComponent
92
- ? f . createIdentifier ( pascal ( name ) )
105
+ ? f . createIdentifier ( name )
93
106
: f . createQualifiedName (
94
107
f . createIdentifier ( pascal ( namespace ) ) ,
95
- f . createIdentifier ( pascal ( name ) )
108
+ f . createIdentifier ( name )
96
109
)
97
110
) ;
98
111
@@ -173,7 +186,7 @@ export const getType = (
173
186
174
187
if ( schema . enum ) {
175
188
if ( isNodeEnum ) {
176
- return f . createTypeReferenceNode ( f . createIdentifier ( pascal ( name || "" ) ) ) ;
189
+ return f . createTypeReferenceNode ( f . createIdentifier ( name || "" ) ) ;
177
190
}
178
191
179
192
const unionTypes = f . createUnionTypeNode ( [
0 commit comments