@rexeus/typeweaver-zod-to-ts 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,355 +1,223 @@
1
- 'use strict';
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let typescript = require("typescript");
3
+ let zod_v4_core = require("zod/v4/core");
2
4
 
3
- var typescript = require('typescript');
4
- var core = require('zod/v4/core');
5
-
6
- // src/TsTypeGenerator.ts
7
- var TsTypeNode = class _TsTypeNode {
8
- static fromZod(zodType) {
9
- if (zodType instanceof core.$ZodString) {
10
- return _TsTypeNode.fromZodString(zodType);
11
- }
12
- if (zodType instanceof core.$ZodNumber) {
13
- return _TsTypeNode.fromZodNumber(zodType);
14
- }
15
- if (zodType instanceof core.$ZodBigInt) {
16
- return _TsTypeNode.fromZodBigInt(zodType);
17
- }
18
- if (zodType instanceof core.$ZodBoolean) {
19
- return _TsTypeNode.fromZodBoolean(zodType);
20
- }
21
- if (zodType instanceof core.$ZodDate) {
22
- return _TsTypeNode.fromZodDate(zodType);
23
- }
24
- if (zodType instanceof core.$ZodSymbol) {
25
- return _TsTypeNode.fromZodSymbol(zodType);
26
- }
27
- if (zodType instanceof core.$ZodUndefined) {
28
- return _TsTypeNode.fromZodUndefined(zodType);
29
- }
30
- if (zodType instanceof core.$ZodNullable) {
31
- return _TsTypeNode.fromZodNullable(zodType);
32
- }
33
- if (zodType instanceof core.$ZodNull) {
34
- return _TsTypeNode.fromZodNull(zodType);
35
- }
36
- if (zodType instanceof core.$ZodAny) {
37
- return _TsTypeNode.fromZodAny(zodType);
38
- }
39
- if (zodType instanceof core.$ZodUnknown) {
40
- return _TsTypeNode.fromZodUnknown(zodType);
41
- }
42
- if (zodType instanceof core.$ZodNever) {
43
- return _TsTypeNode.fromZodNever(zodType);
44
- }
45
- if (zodType instanceof core.$ZodVoid) {
46
- return _TsTypeNode.fromZodVoid(zodType);
47
- }
48
- if (zodType instanceof core.$ZodArray) {
49
- return _TsTypeNode.fromZodArray(zodType);
50
- }
51
- if (zodType instanceof core.$ZodObject) {
52
- return _TsTypeNode.fromZodObject(zodType);
53
- }
54
- if (zodType instanceof core.$ZodUnion) {
55
- return _TsTypeNode.fromZodUnion(zodType);
56
- }
57
- if (zodType instanceof core.$ZodIntersection) {
58
- return _TsTypeNode.fromZodIntersection(zodType);
59
- }
60
- if (zodType instanceof core.$ZodTuple) {
61
- return _TsTypeNode.fromZodTuple(zodType);
62
- }
63
- if (zodType instanceof core.$ZodRecord) {
64
- return _TsTypeNode.fromZodRecord(zodType);
65
- }
66
- if (zodType instanceof core.$ZodMap) {
67
- return _TsTypeNode.fromZodMap(zodType);
68
- }
69
- if (zodType instanceof core.$ZodSet) {
70
- return _TsTypeNode.fromZodSet(zodType);
71
- }
72
- if (zodType instanceof core.$ZodLiteral) {
73
- if (!_TsTypeNode) {
74
- throw new Error("ZodLiteral has no values");
75
- }
76
- return _TsTypeNode.fromZodLiteral(zodType);
77
- }
78
- if (zodType instanceof core.$ZodEnum) {
79
- return _TsTypeNode.fromZodEnum(zodType);
80
- }
81
- if (zodType instanceof core.$ZodPromise) {
82
- return _TsTypeNode.fromZodPromise(zodType);
83
- }
84
- if (zodType instanceof core.$ZodLazy) {
85
- return _TsTypeNode.fromZodLazy(zodType);
86
- }
87
- if (zodType instanceof core.$ZodOptional) {
88
- return _TsTypeNode.fromZodOptional(zodType);
89
- }
90
- if (zodType instanceof core.$ZodDefault) {
91
- return _TsTypeNode.fromZodDefault(zodType);
92
- }
93
- if (zodType instanceof core.$ZodTemplateLiteral) {
94
- return _TsTypeNode.fromZodTemplateLiteral(zodType);
95
- }
96
- if (zodType instanceof core.$ZodCustom) {
97
- return _TsTypeNode.fromZodCustom(zodType);
98
- }
99
- if (zodType instanceof core.$ZodTransform) {
100
- return _TsTypeNode.fromZodTransform(zodType);
101
- }
102
- if (zodType instanceof core.$ZodNonOptional) {
103
- return _TsTypeNode.fromZodNonOptional(zodType);
104
- }
105
- if (zodType instanceof core.$ZodReadonly) {
106
- return _TsTypeNode.fromZodReadonly(zodType);
107
- }
108
- if (zodType instanceof core.$ZodNaN) {
109
- return _TsTypeNode.fromZodNaN(zodType);
110
- }
111
- if (zodType instanceof core.$ZodPipe) {
112
- return _TsTypeNode.fromZodPipe(zodType);
113
- }
114
- if (zodType instanceof core.$ZodSuccess) {
115
- return _TsTypeNode.fromZodSuccess(zodType);
116
- }
117
- if (zodType instanceof core.$ZodCatch) {
118
- return _TsTypeNode.fromZodCatch(zodType);
119
- }
120
- if (zodType instanceof core.$ZodFile) {
121
- return _TsTypeNode.fromZodFile(zodType);
122
- }
123
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
124
- }
125
- static fromZodString(_zodString) {
126
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.StringKeyword);
127
- }
128
- static fromZodNumber(_zodNumber) {
129
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.NumberKeyword);
130
- }
131
- static fromZodBigInt(_zodBigInt) {
132
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.BigIntKeyword);
133
- }
134
- static fromZodBoolean(_zodBoolean) {
135
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.BooleanKeyword);
136
- }
137
- static fromZodDate(_zodDate) {
138
- return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Date"));
139
- }
140
- static fromZodSymbol(_zodSymbol) {
141
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.SymbolKeyword);
142
- }
143
- static fromZodUndefined(_zodUndefined) {
144
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword);
145
- }
146
- static fromZodNullable(zodNullable) {
147
- const innerType = _TsTypeNode.fromZod(zodNullable._zod.def.innerType);
148
- return typescript.factory.createUnionTypeNode([
149
- innerType,
150
- typescript.factory.createLiteralTypeNode(typescript.factory.createNull())
151
- ]);
152
- }
153
- static fromZodNull(_zodNull) {
154
- return typescript.factory.createLiteralTypeNode(typescript.factory.createNull());
155
- }
156
- static fromZodAny(_zodAny) {
157
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.AnyKeyword);
158
- }
159
- static fromZodUnknown(_zodUnknown) {
160
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
161
- }
162
- static fromZodNever(_zodNever) {
163
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.NeverKeyword);
164
- }
165
- static fromZodVoid(_zodVoid) {
166
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.VoidKeyword);
167
- }
168
- static fromZodArray(zodArray) {
169
- const innerType = _TsTypeNode.fromZod(zodArray._zod.def.element);
170
- return typescript.factory.createArrayTypeNode(innerType);
171
- }
172
- static fromZodObject(zodObject) {
173
- const entries = Object.entries(zodObject._zod.def.shape);
174
- const members = entries.map(([key, nextZodNode]) => {
175
- const type = _TsTypeNode.fromZod(nextZodNode);
176
- if (!nextZodNode._zod?.def) {
177
- console.warn(
178
- `Zod node for key "${key}" does not have a _zod.def property. This may indicate an issue with the Zod schema.`,
179
- {
180
- key
181
- }
182
- );
183
- }
184
- const { type: nextZodNodeTypeName } = nextZodNode._zod?.def ?? {};
185
- const isOptional = nextZodNodeTypeName === "optional";
186
- const propertySignature = typescript.factory.createPropertySignature(
187
- void 0,
188
- _TsTypeNode.createTsAstPropertyKey(key),
189
- isOptional ? typescript.factory.createToken(typescript.SyntaxKind.QuestionToken) : void 0,
190
- type
191
- );
192
- return propertySignature;
193
- });
194
- return typescript.factory.createTypeLiteralNode(members);
195
- }
196
- static fromZodUnion(zodUnion) {
197
- const options = zodUnion._zod.def.options.map(_TsTypeNode.fromZod);
198
- return typescript.factory.createUnionTypeNode(options);
199
- }
200
- static fromZodIntersection(zodIntersection) {
201
- const left = _TsTypeNode.fromZod(zodIntersection._zod.def.left);
202
- const right = _TsTypeNode.fromZod(zodIntersection._zod.def.right);
203
- return typescript.factory.createIntersectionTypeNode([left, right]);
204
- }
205
- static fromZodTuple(zodTuple) {
206
- const elements = zodTuple._zod.def.items.map(_TsTypeNode.fromZod);
207
- return typescript.factory.createTupleTypeNode(elements);
208
- }
209
- static fromZodRecord(zodRecord) {
210
- const keyType = _TsTypeNode.fromZod(zodRecord._zod.def.keyType);
211
- const valueType = _TsTypeNode.fromZod(zodRecord._zod.def.valueType);
212
- return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Record"), [
213
- keyType,
214
- valueType
215
- ]);
216
- }
217
- static fromZodMap(zodMap) {
218
- const keyType = _TsTypeNode.fromZod(zodMap._zod.def.keyType);
219
- const valueType = _TsTypeNode.fromZod(zodMap._zod.def.valueType);
220
- return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Map"), [
221
- keyType,
222
- valueType
223
- ]);
224
- }
225
- static fromZodSet(zodSet) {
226
- const innerType = _TsTypeNode.fromZod(zodSet._zod.def.valueType);
227
- return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Set"), [
228
- innerType
229
- ]);
230
- }
231
- static fromZodLiteral(zodLiteral) {
232
- if (zodLiteral._zod.def.values.length === 0) {
233
- throw new Error("ZodLiteral has no values");
234
- }
235
- const value = zodLiteral._zod.def.values[0];
236
- if (typeof value === "string") {
237
- return typescript.factory.createLiteralTypeNode(typescript.factory.createStringLiteral(value));
238
- }
239
- if (typeof value === "number") {
240
- return typescript.factory.createLiteralTypeNode(typescript.factory.createNumericLiteral(value));
241
- }
242
- if (typeof value === "boolean") {
243
- return typescript.factory.createLiteralTypeNode(
244
- value ? typescript.factory.createTrue() : typescript.factory.createFalse()
245
- );
246
- }
247
- if (typeof value === "bigint") {
248
- return typescript.factory.createLiteralTypeNode(
249
- typescript.factory.createBigIntLiteral(value.toString())
250
- );
251
- }
252
- if (value === null) {
253
- return typescript.factory.createLiteralTypeNode(typescript.factory.createNull());
254
- }
255
- if (value === void 0) {
256
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword);
257
- }
258
- throw new Error(`Unsupported literal type: ${typeof value}`);
259
- }
260
- static fromZodEnum(zodEnum) {
261
- const entries = Object.entries(zodEnum._zod.def.entries);
262
- const types = entries.map(([key, value]) => {
263
- return typescript.factory.createLiteralTypeNode(typescript.factory.createStringLiteral(key));
264
- });
265
- return typescript.factory.createUnionTypeNode(types);
266
- }
267
- static fromZodPromise(zodPromise) {
268
- const innerType = _TsTypeNode.fromZod(zodPromise._zod.def.innerType);
269
- return typescript.factory.createTypeReferenceNode(
270
- typescript.factory.createIdentifier("Promise"),
271
- [innerType]
272
- );
273
- }
274
- static fromZodLazy(_zodLazy) {
275
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
276
- }
277
- static fromZodOptional(zodOptional) {
278
- const innerType = _TsTypeNode.fromZod(zodOptional._zod.def.innerType);
279
- return typescript.factory.createUnionTypeNode([
280
- innerType,
281
- typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword)
282
- ]);
283
- }
284
- static fromZodDefault(zodDefault) {
285
- const innerType = _TsTypeNode.fromZod(zodDefault._zod.def.innerType);
286
- const filteredNodes = [];
287
- innerType.forEachChild((node) => {
288
- if (node.kind !== typescript.SyntaxKind.UndefinedKeyword) {
289
- filteredNodes.push(node);
290
- }
291
- });
292
- innerType.types = filteredNodes;
293
- return innerType;
294
- }
295
- static fromZodTemplateLiteral(_zodTemplateLiteral) {
296
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
297
- }
298
- static fromZodCustom(_zodCustom) {
299
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
300
- }
301
- static fromZodTransform(_zodTransform) {
302
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
303
- }
304
- static fromZodNonOptional(_zodNonOptional) {
305
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
306
- }
307
- static fromZodReadonly(_zodReadonly) {
308
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
309
- }
310
- static fromZodNaN(_zodNaN) {
311
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
312
- }
313
- static fromZodPipe(_zodPipe) {
314
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
315
- }
316
- static fromZodSuccess(_zodSuccess) {
317
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
318
- }
319
- static fromZodCatch(_zodCatch) {
320
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
321
- }
322
- static fromZodFile(_zodFile) {
323
- return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
324
- }
325
- /**
326
- * Returns a TypeScript AST node representing the property key.
327
- * If the key is a valid JavaScript identifier, returns an Identifier node.
328
- * Otherwise, returns a StringLiteral node.
329
- *
330
- * @param {string} key - The property key to convert.
331
- * @returns {Identifier | StringLiteral} The corresponding AST node for the property key.
332
- */
333
- static createTsAstPropertyKey(key) {
334
- if (/^[$A-Z_a-z][\w$]*$/.test(key)) {
335
- return typescript.factory.createIdentifier(key);
336
- }
337
- return typescript.factory.createStringLiteral(key);
338
- }
5
+ //#region src/TsTypeGenerator.ts
6
+ var TsTypeNode = class TsTypeNode {
7
+ static fromZod(zodType) {
8
+ if (zodType instanceof zod_v4_core.$ZodString) return TsTypeNode.fromZodString(zodType);
9
+ if (zodType instanceof zod_v4_core.$ZodNumber) return TsTypeNode.fromZodNumber(zodType);
10
+ if (zodType instanceof zod_v4_core.$ZodBigInt) return TsTypeNode.fromZodBigInt(zodType);
11
+ if (zodType instanceof zod_v4_core.$ZodBoolean) return TsTypeNode.fromZodBoolean(zodType);
12
+ if (zodType instanceof zod_v4_core.$ZodDate) return TsTypeNode.fromZodDate(zodType);
13
+ if (zodType instanceof zod_v4_core.$ZodSymbol) return TsTypeNode.fromZodSymbol(zodType);
14
+ if (zodType instanceof zod_v4_core.$ZodUndefined) return TsTypeNode.fromZodUndefined(zodType);
15
+ if (zodType instanceof zod_v4_core.$ZodNullable) return TsTypeNode.fromZodNullable(zodType);
16
+ if (zodType instanceof zod_v4_core.$ZodNull) return TsTypeNode.fromZodNull(zodType);
17
+ if (zodType instanceof zod_v4_core.$ZodAny) return TsTypeNode.fromZodAny(zodType);
18
+ if (zodType instanceof zod_v4_core.$ZodUnknown) return TsTypeNode.fromZodUnknown(zodType);
19
+ if (zodType instanceof zod_v4_core.$ZodNever) return TsTypeNode.fromZodNever(zodType);
20
+ if (zodType instanceof zod_v4_core.$ZodVoid) return TsTypeNode.fromZodVoid(zodType);
21
+ if (zodType instanceof zod_v4_core.$ZodArray) return TsTypeNode.fromZodArray(zodType);
22
+ if (zodType instanceof zod_v4_core.$ZodObject) return TsTypeNode.fromZodObject(zodType);
23
+ if (zodType instanceof zod_v4_core.$ZodUnion) return TsTypeNode.fromZodUnion(zodType);
24
+ if (zodType instanceof zod_v4_core.$ZodIntersection) return TsTypeNode.fromZodIntersection(zodType);
25
+ if (zodType instanceof zod_v4_core.$ZodTuple) return TsTypeNode.fromZodTuple(zodType);
26
+ if (zodType instanceof zod_v4_core.$ZodRecord) return TsTypeNode.fromZodRecord(zodType);
27
+ if (zodType instanceof zod_v4_core.$ZodMap) return TsTypeNode.fromZodMap(zodType);
28
+ if (zodType instanceof zod_v4_core.$ZodSet) return TsTypeNode.fromZodSet(zodType);
29
+ if (zodType instanceof zod_v4_core.$ZodLiteral) {
30
+ if (!TsTypeNode) throw new Error("ZodLiteral has no values");
31
+ return TsTypeNode.fromZodLiteral(zodType);
32
+ }
33
+ if (zodType instanceof zod_v4_core.$ZodEnum) return TsTypeNode.fromZodEnum(zodType);
34
+ if (zodType instanceof zod_v4_core.$ZodPromise) return TsTypeNode.fromZodPromise(zodType);
35
+ if (zodType instanceof zod_v4_core.$ZodLazy) return TsTypeNode.fromZodLazy(zodType);
36
+ if (zodType instanceof zod_v4_core.$ZodOptional) return TsTypeNode.fromZodOptional(zodType);
37
+ if (zodType instanceof zod_v4_core.$ZodDefault) return TsTypeNode.fromZodDefault(zodType);
38
+ if (zodType instanceof zod_v4_core.$ZodTemplateLiteral) return TsTypeNode.fromZodTemplateLiteral(zodType);
39
+ if (zodType instanceof zod_v4_core.$ZodCustom) return TsTypeNode.fromZodCustom(zodType);
40
+ if (zodType instanceof zod_v4_core.$ZodTransform) return TsTypeNode.fromZodTransform(zodType);
41
+ if (zodType instanceof zod_v4_core.$ZodNonOptional) return TsTypeNode.fromZodNonOptional(zodType);
42
+ if (zodType instanceof zod_v4_core.$ZodReadonly) return TsTypeNode.fromZodReadonly(zodType);
43
+ if (zodType instanceof zod_v4_core.$ZodNaN) return TsTypeNode.fromZodNaN(zodType);
44
+ if (zodType instanceof zod_v4_core.$ZodPipe) return TsTypeNode.fromZodPipe(zodType);
45
+ if (zodType instanceof zod_v4_core.$ZodSuccess) return TsTypeNode.fromZodSuccess(zodType);
46
+ if (zodType instanceof zod_v4_core.$ZodCatch) return TsTypeNode.fromZodCatch(zodType);
47
+ if (zodType instanceof zod_v4_core.$ZodFile) return TsTypeNode.fromZodFile(zodType);
48
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
49
+ }
50
+ static fromZodString(_zodString) {
51
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.StringKeyword);
52
+ }
53
+ static fromZodNumber(_zodNumber) {
54
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.NumberKeyword);
55
+ }
56
+ static fromZodBigInt(_zodBigInt) {
57
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.BigIntKeyword);
58
+ }
59
+ static fromZodBoolean(_zodBoolean) {
60
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.BooleanKeyword);
61
+ }
62
+ static fromZodDate(_zodDate) {
63
+ return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Date"));
64
+ }
65
+ static fromZodSymbol(_zodSymbol) {
66
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.SymbolKeyword);
67
+ }
68
+ static fromZodUndefined(_zodUndefined) {
69
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword);
70
+ }
71
+ static fromZodNullable(zodNullable) {
72
+ const innerType = TsTypeNode.fromZod(zodNullable._zod.def.innerType);
73
+ return typescript.factory.createUnionTypeNode([innerType, typescript.factory.createLiteralTypeNode(typescript.factory.createNull())]);
74
+ }
75
+ static fromZodNull(_zodNull) {
76
+ return typescript.factory.createLiteralTypeNode(typescript.factory.createNull());
77
+ }
78
+ static fromZodAny(_zodAny) {
79
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.AnyKeyword);
80
+ }
81
+ static fromZodUnknown(_zodUnknown) {
82
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
83
+ }
84
+ static fromZodNever(_zodNever) {
85
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.NeverKeyword);
86
+ }
87
+ static fromZodVoid(_zodVoid) {
88
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.VoidKeyword);
89
+ }
90
+ static fromZodArray(zodArray) {
91
+ const innerType = TsTypeNode.fromZod(zodArray._zod.def.element);
92
+ return typescript.factory.createArrayTypeNode(innerType);
93
+ }
94
+ static fromZodObject(zodObject) {
95
+ const members = Object.entries(zodObject._zod.def.shape).map(([key, nextZodNode]) => {
96
+ const type = TsTypeNode.fromZod(nextZodNode);
97
+ if (!nextZodNode._zod?.def) console.warn(`Zod node for key "${key}" does not have a _zod.def property. This may indicate an issue with the Zod schema.`, { key });
98
+ const { type: nextZodNodeTypeName } = nextZodNode._zod?.def ?? {};
99
+ const isOptional = nextZodNodeTypeName === "optional";
100
+ return typescript.factory.createPropertySignature(void 0, TsTypeNode.createTsAstPropertyKey(key), isOptional ? typescript.factory.createToken(typescript.SyntaxKind.QuestionToken) : void 0, type);
101
+ });
102
+ return typescript.factory.createTypeLiteralNode(members);
103
+ }
104
+ static fromZodUnion(zodUnion) {
105
+ const options = zodUnion._zod.def.options.map(TsTypeNode.fromZod);
106
+ return typescript.factory.createUnionTypeNode(options);
107
+ }
108
+ static fromZodIntersection(zodIntersection) {
109
+ const left = TsTypeNode.fromZod(zodIntersection._zod.def.left);
110
+ const right = TsTypeNode.fromZod(zodIntersection._zod.def.right);
111
+ return typescript.factory.createIntersectionTypeNode([left, right]);
112
+ }
113
+ static fromZodTuple(zodTuple) {
114
+ const elements = zodTuple._zod.def.items.map(TsTypeNode.fromZod);
115
+ return typescript.factory.createTupleTypeNode(elements);
116
+ }
117
+ static fromZodRecord(zodRecord) {
118
+ const keyType = TsTypeNode.fromZod(zodRecord._zod.def.keyType);
119
+ const valueType = TsTypeNode.fromZod(zodRecord._zod.def.valueType);
120
+ return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Record"), [keyType, valueType]);
121
+ }
122
+ static fromZodMap(zodMap) {
123
+ const keyType = TsTypeNode.fromZod(zodMap._zod.def.keyType);
124
+ const valueType = TsTypeNode.fromZod(zodMap._zod.def.valueType);
125
+ return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Map"), [keyType, valueType]);
126
+ }
127
+ static fromZodSet(zodSet) {
128
+ const innerType = TsTypeNode.fromZod(zodSet._zod.def.valueType);
129
+ return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Set"), [innerType]);
130
+ }
131
+ static fromZodLiteral(zodLiteral) {
132
+ if (zodLiteral._zod.def.values.length === 0) throw new Error("ZodLiteral has no values");
133
+ const value = zodLiteral._zod.def.values[0];
134
+ if (typeof value === "string") return typescript.factory.createLiteralTypeNode(typescript.factory.createStringLiteral(value));
135
+ if (typeof value === "number") return typescript.factory.createLiteralTypeNode(typescript.factory.createNumericLiteral(value));
136
+ if (typeof value === "boolean") return typescript.factory.createLiteralTypeNode(value ? typescript.factory.createTrue() : typescript.factory.createFalse());
137
+ if (typeof value === "bigint") return typescript.factory.createLiteralTypeNode(typescript.factory.createBigIntLiteral(value.toString()));
138
+ if (value === null) return typescript.factory.createLiteralTypeNode(typescript.factory.createNull());
139
+ if (value === void 0) return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword);
140
+ throw new Error(`Unsupported literal type: ${typeof value}`);
141
+ }
142
+ static fromZodEnum(zodEnum) {
143
+ const types = Object.entries(zodEnum._zod.def.entries).map(([key, _value]) => {
144
+ return typescript.factory.createLiteralTypeNode(typescript.factory.createStringLiteral(key));
145
+ });
146
+ return typescript.factory.createUnionTypeNode(types);
147
+ }
148
+ static fromZodPromise(zodPromise) {
149
+ const innerType = TsTypeNode.fromZod(zodPromise._zod.def.innerType);
150
+ return typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("Promise"), [innerType]);
151
+ }
152
+ static fromZodLazy(_zodLazy) {
153
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
154
+ }
155
+ static fromZodOptional(zodOptional) {
156
+ const innerType = TsTypeNode.fromZod(zodOptional._zod.def.innerType);
157
+ return typescript.factory.createUnionTypeNode([innerType, typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword)]);
158
+ }
159
+ static fromZodDefault(zodDefault) {
160
+ const innerType = TsTypeNode.fromZod(zodDefault._zod.def.innerType);
161
+ const filteredNodes = [];
162
+ innerType.forEachChild((node) => {
163
+ if (node.kind !== typescript.SyntaxKind.UndefinedKeyword) filteredNodes.push(node);
164
+ });
165
+ innerType.types = filteredNodes;
166
+ return innerType;
167
+ }
168
+ static fromZodTemplateLiteral(_zodTemplateLiteral) {
169
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
170
+ }
171
+ static fromZodCustom(_zodCustom) {
172
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
173
+ }
174
+ static fromZodTransform(_zodTransform) {
175
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
176
+ }
177
+ static fromZodNonOptional(_zodNonOptional) {
178
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
179
+ }
180
+ static fromZodReadonly(_zodReadonly) {
181
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
182
+ }
183
+ static fromZodNaN(_zodNaN) {
184
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
185
+ }
186
+ static fromZodPipe(_zodPipe) {
187
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
188
+ }
189
+ static fromZodSuccess(_zodSuccess) {
190
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
191
+ }
192
+ static fromZodCatch(_zodCatch) {
193
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
194
+ }
195
+ static fromZodFile(_zodFile) {
196
+ return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword);
197
+ }
198
+ /**
199
+ * Returns a TypeScript AST node representing the property key.
200
+ * If the key is a valid JavaScript identifier, returns an Identifier node.
201
+ * Otherwise, returns a StringLiteral node.
202
+ *
203
+ * @param {string} key - The property key to convert.
204
+ * @returns {Identifier | StringLiteral} The corresponding AST node for the property key.
205
+ */
206
+ static createTsAstPropertyKey(key) {
207
+ if (/^[$A-Z_a-z][\w$]*$/.test(key)) return typescript.factory.createIdentifier(key);
208
+ return typescript.factory.createStringLiteral(key);
209
+ }
339
210
  };
211
+
212
+ //#endregion
213
+ //#region src/TsTypePrinter.ts
340
214
  var TsTypePrinter = class {
341
- static print(tsType) {
342
- const sourceFile = typescript.createSourceFile(
343
- "print.ts",
344
- "",
345
- typescript.ScriptTarget.Latest,
346
- false,
347
- typescript.ScriptKind.TS
348
- );
349
- const printer = typescript.createPrinter();
350
- return printer.printNode(typescript.EmitHint.Unspecified, tsType, sourceFile);
351
- }
215
+ static print(tsType) {
216
+ const sourceFile = (0, typescript.createSourceFile)("print.ts", "", typescript.ScriptTarget.Latest, false, typescript.ScriptKind.TS);
217
+ return (0, typescript.createPrinter)().printNode(typescript.EmitHint.Unspecified, tsType, sourceFile);
218
+ }
352
219
  };
353
220
 
221
+ //#endregion
354
222
  exports.TsTypeNode = TsTypeNode;
355
- exports.TsTypePrinter = TsTypePrinter;
223
+ exports.TsTypePrinter = TsTypePrinter;