@rexeus/typeweaver-zod-to-ts 0.2.0 → 0.3.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 +384 -0
- package/dist/index.d.cts +58 -0
- package/dist/index.js +111 -63
- package/package.json +14 -7
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
TsTypeNode: () => TsTypeNode,
|
|
24
|
+
TsTypePrinter: () => TsTypePrinter
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/TsTypeGenerator.ts
|
|
29
|
+
var import_typescript = require("typescript");
|
|
30
|
+
var import_core = require("zod/v4/core");
|
|
31
|
+
var TsTypeNode = class _TsTypeNode {
|
|
32
|
+
static fromZod(zodType) {
|
|
33
|
+
if (zodType instanceof import_core.$ZodString) {
|
|
34
|
+
return _TsTypeNode.fromZodString(zodType);
|
|
35
|
+
}
|
|
36
|
+
if (zodType instanceof import_core.$ZodNumber) {
|
|
37
|
+
return _TsTypeNode.fromZodNumber(zodType);
|
|
38
|
+
}
|
|
39
|
+
if (zodType instanceof import_core.$ZodBigInt) {
|
|
40
|
+
return _TsTypeNode.fromZodBigInt(zodType);
|
|
41
|
+
}
|
|
42
|
+
if (zodType instanceof import_core.$ZodBoolean) {
|
|
43
|
+
return _TsTypeNode.fromZodBoolean(zodType);
|
|
44
|
+
}
|
|
45
|
+
if (zodType instanceof import_core.$ZodDate) {
|
|
46
|
+
return _TsTypeNode.fromZodDate(zodType);
|
|
47
|
+
}
|
|
48
|
+
if (zodType instanceof import_core.$ZodSymbol) {
|
|
49
|
+
return _TsTypeNode.fromZodSymbol(zodType);
|
|
50
|
+
}
|
|
51
|
+
if (zodType instanceof import_core.$ZodUndefined) {
|
|
52
|
+
return _TsTypeNode.fromZodUndefined(zodType);
|
|
53
|
+
}
|
|
54
|
+
if (zodType instanceof import_core.$ZodNullable) {
|
|
55
|
+
return _TsTypeNode.fromZodNullable(zodType);
|
|
56
|
+
}
|
|
57
|
+
if (zodType instanceof import_core.$ZodNull) {
|
|
58
|
+
return _TsTypeNode.fromZodNull(zodType);
|
|
59
|
+
}
|
|
60
|
+
if (zodType instanceof import_core.$ZodAny) {
|
|
61
|
+
return _TsTypeNode.fromZodAny(zodType);
|
|
62
|
+
}
|
|
63
|
+
if (zodType instanceof import_core.$ZodUnknown) {
|
|
64
|
+
return _TsTypeNode.fromZodUnknown(zodType);
|
|
65
|
+
}
|
|
66
|
+
if (zodType instanceof import_core.$ZodNever) {
|
|
67
|
+
return _TsTypeNode.fromZodNever(zodType);
|
|
68
|
+
}
|
|
69
|
+
if (zodType instanceof import_core.$ZodVoid) {
|
|
70
|
+
return _TsTypeNode.fromZodVoid(zodType);
|
|
71
|
+
}
|
|
72
|
+
if (zodType instanceof import_core.$ZodArray) {
|
|
73
|
+
return _TsTypeNode.fromZodArray(zodType);
|
|
74
|
+
}
|
|
75
|
+
if (zodType instanceof import_core.$ZodObject) {
|
|
76
|
+
return _TsTypeNode.fromZodObject(zodType);
|
|
77
|
+
}
|
|
78
|
+
if (zodType instanceof import_core.$ZodUnion) {
|
|
79
|
+
return _TsTypeNode.fromZodUnion(zodType);
|
|
80
|
+
}
|
|
81
|
+
if (zodType instanceof import_core.$ZodIntersection) {
|
|
82
|
+
return _TsTypeNode.fromZodIntersection(zodType);
|
|
83
|
+
}
|
|
84
|
+
if (zodType instanceof import_core.$ZodTuple) {
|
|
85
|
+
return _TsTypeNode.fromZodTuple(zodType);
|
|
86
|
+
}
|
|
87
|
+
if (zodType instanceof import_core.$ZodRecord) {
|
|
88
|
+
return _TsTypeNode.fromZodRecord(zodType);
|
|
89
|
+
}
|
|
90
|
+
if (zodType instanceof import_core.$ZodMap) {
|
|
91
|
+
return _TsTypeNode.fromZodMap(zodType);
|
|
92
|
+
}
|
|
93
|
+
if (zodType instanceof import_core.$ZodSet) {
|
|
94
|
+
return _TsTypeNode.fromZodSet(zodType);
|
|
95
|
+
}
|
|
96
|
+
if (zodType instanceof import_core.$ZodLiteral) {
|
|
97
|
+
if (!_TsTypeNode) {
|
|
98
|
+
throw new Error("ZodLiteral has no values");
|
|
99
|
+
}
|
|
100
|
+
return _TsTypeNode.fromZodLiteral(zodType);
|
|
101
|
+
}
|
|
102
|
+
if (zodType instanceof import_core.$ZodEnum) {
|
|
103
|
+
return _TsTypeNode.fromZodEnum(zodType);
|
|
104
|
+
}
|
|
105
|
+
if (zodType instanceof import_core.$ZodPromise) {
|
|
106
|
+
return _TsTypeNode.fromZodPromise(zodType);
|
|
107
|
+
}
|
|
108
|
+
if (zodType instanceof import_core.$ZodLazy) {
|
|
109
|
+
return _TsTypeNode.fromZodLazy(zodType);
|
|
110
|
+
}
|
|
111
|
+
if (zodType instanceof import_core.$ZodOptional) {
|
|
112
|
+
return _TsTypeNode.fromZodOptional(zodType);
|
|
113
|
+
}
|
|
114
|
+
if (zodType instanceof import_core.$ZodDefault) {
|
|
115
|
+
return _TsTypeNode.fromZodDefault(zodType);
|
|
116
|
+
}
|
|
117
|
+
if (zodType instanceof import_core.$ZodTemplateLiteral) {
|
|
118
|
+
return _TsTypeNode.fromZodTemplateLiteral(zodType);
|
|
119
|
+
}
|
|
120
|
+
if (zodType instanceof import_core.$ZodCustom) {
|
|
121
|
+
return _TsTypeNode.fromZodCustom(zodType);
|
|
122
|
+
}
|
|
123
|
+
if (zodType instanceof import_core.$ZodTransform) {
|
|
124
|
+
return _TsTypeNode.fromZodTransform(zodType);
|
|
125
|
+
}
|
|
126
|
+
if (zodType instanceof import_core.$ZodNonOptional) {
|
|
127
|
+
return _TsTypeNode.fromZodNonOptional(zodType);
|
|
128
|
+
}
|
|
129
|
+
if (zodType instanceof import_core.$ZodReadonly) {
|
|
130
|
+
return _TsTypeNode.fromZodReadonly(zodType);
|
|
131
|
+
}
|
|
132
|
+
if (zodType instanceof import_core.$ZodNaN) {
|
|
133
|
+
return _TsTypeNode.fromZodNaN(zodType);
|
|
134
|
+
}
|
|
135
|
+
if (zodType instanceof import_core.$ZodPipe) {
|
|
136
|
+
return _TsTypeNode.fromZodPipe(zodType);
|
|
137
|
+
}
|
|
138
|
+
if (zodType instanceof import_core.$ZodSuccess) {
|
|
139
|
+
return _TsTypeNode.fromZodSuccess(zodType);
|
|
140
|
+
}
|
|
141
|
+
if (zodType instanceof import_core.$ZodCatch) {
|
|
142
|
+
return _TsTypeNode.fromZodCatch(zodType);
|
|
143
|
+
}
|
|
144
|
+
if (zodType instanceof import_core.$ZodFile) {
|
|
145
|
+
return _TsTypeNode.fromZodFile(zodType);
|
|
146
|
+
}
|
|
147
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
148
|
+
}
|
|
149
|
+
static fromZodString(_zodString) {
|
|
150
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.StringKeyword);
|
|
151
|
+
}
|
|
152
|
+
static fromZodNumber(_zodNumber) {
|
|
153
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.NumberKeyword);
|
|
154
|
+
}
|
|
155
|
+
static fromZodBigInt(_zodBigInt) {
|
|
156
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.BigIntKeyword);
|
|
157
|
+
}
|
|
158
|
+
static fromZodBoolean(_zodBoolean) {
|
|
159
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.BooleanKeyword);
|
|
160
|
+
}
|
|
161
|
+
static fromZodDate(_zodDate) {
|
|
162
|
+
return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("Date"));
|
|
163
|
+
}
|
|
164
|
+
static fromZodSymbol(_zodSymbol) {
|
|
165
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.SymbolKeyword);
|
|
166
|
+
}
|
|
167
|
+
static fromZodUndefined(_zodUndefined) {
|
|
168
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UndefinedKeyword);
|
|
169
|
+
}
|
|
170
|
+
static fromZodNullable(zodNullable) {
|
|
171
|
+
const innerType = _TsTypeNode.fromZod(zodNullable._zod.def.innerType);
|
|
172
|
+
return import_typescript.factory.createUnionTypeNode([
|
|
173
|
+
innerType,
|
|
174
|
+
import_typescript.factory.createLiteralTypeNode(import_typescript.factory.createNull())
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
static fromZodNull(_zodNull) {
|
|
178
|
+
return import_typescript.factory.createLiteralTypeNode(import_typescript.factory.createNull());
|
|
179
|
+
}
|
|
180
|
+
static fromZodAny(_zodAny) {
|
|
181
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.AnyKeyword);
|
|
182
|
+
}
|
|
183
|
+
static fromZodUnknown(_zodUnknown) {
|
|
184
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
185
|
+
}
|
|
186
|
+
static fromZodNever(_zodNever) {
|
|
187
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.NeverKeyword);
|
|
188
|
+
}
|
|
189
|
+
static fromZodVoid(_zodVoid) {
|
|
190
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.VoidKeyword);
|
|
191
|
+
}
|
|
192
|
+
static fromZodArray(zodArray) {
|
|
193
|
+
const innerType = _TsTypeNode.fromZod(zodArray._zod.def.element);
|
|
194
|
+
return import_typescript.factory.createArrayTypeNode(innerType);
|
|
195
|
+
}
|
|
196
|
+
static fromZodObject(zodObject) {
|
|
197
|
+
const entries = Object.entries(zodObject._zod.def.shape);
|
|
198
|
+
const members = entries.map(([key, nextZodNode]) => {
|
|
199
|
+
const type = _TsTypeNode.fromZod(nextZodNode);
|
|
200
|
+
if (!nextZodNode._zod?.def) {
|
|
201
|
+
console.warn(
|
|
202
|
+
`Zod node for key "${key}" does not have a _zod.def property. This may indicate an issue with the Zod schema.`,
|
|
203
|
+
{
|
|
204
|
+
key
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
const { type: nextZodNodeTypeName } = nextZodNode._zod?.def ?? {};
|
|
209
|
+
const isOptional = nextZodNodeTypeName === "optional";
|
|
210
|
+
const propertySignature = import_typescript.factory.createPropertySignature(
|
|
211
|
+
void 0,
|
|
212
|
+
_TsTypeNode.createTsAstPropertyKey(key),
|
|
213
|
+
isOptional ? import_typescript.factory.createToken(import_typescript.SyntaxKind.QuestionToken) : void 0,
|
|
214
|
+
type
|
|
215
|
+
);
|
|
216
|
+
return propertySignature;
|
|
217
|
+
});
|
|
218
|
+
return import_typescript.factory.createTypeLiteralNode(members);
|
|
219
|
+
}
|
|
220
|
+
static fromZodUnion(zodUnion) {
|
|
221
|
+
const options = zodUnion._zod.def.options.map(_TsTypeNode.fromZod);
|
|
222
|
+
return import_typescript.factory.createUnionTypeNode(options);
|
|
223
|
+
}
|
|
224
|
+
static fromZodIntersection(zodIntersection) {
|
|
225
|
+
const left = _TsTypeNode.fromZod(zodIntersection._zod.def.left);
|
|
226
|
+
const right = _TsTypeNode.fromZod(zodIntersection._zod.def.right);
|
|
227
|
+
return import_typescript.factory.createIntersectionTypeNode([left, right]);
|
|
228
|
+
}
|
|
229
|
+
static fromZodTuple(zodTuple) {
|
|
230
|
+
const elements = zodTuple._zod.def.items.map(_TsTypeNode.fromZod);
|
|
231
|
+
return import_typescript.factory.createTupleTypeNode(elements);
|
|
232
|
+
}
|
|
233
|
+
static fromZodRecord(zodRecord) {
|
|
234
|
+
const keyType = _TsTypeNode.fromZod(zodRecord._zod.def.keyType);
|
|
235
|
+
const valueType = _TsTypeNode.fromZod(zodRecord._zod.def.valueType);
|
|
236
|
+
return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("Record"), [
|
|
237
|
+
keyType,
|
|
238
|
+
valueType
|
|
239
|
+
]);
|
|
240
|
+
}
|
|
241
|
+
static fromZodMap(zodMap) {
|
|
242
|
+
const keyType = _TsTypeNode.fromZod(zodMap._zod.def.keyType);
|
|
243
|
+
const valueType = _TsTypeNode.fromZod(zodMap._zod.def.valueType);
|
|
244
|
+
return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("Map"), [
|
|
245
|
+
keyType,
|
|
246
|
+
valueType
|
|
247
|
+
]);
|
|
248
|
+
}
|
|
249
|
+
static fromZodSet(zodSet) {
|
|
250
|
+
const innerType = _TsTypeNode.fromZod(zodSet._zod.def.valueType);
|
|
251
|
+
return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier("Set"), [
|
|
252
|
+
innerType
|
|
253
|
+
]);
|
|
254
|
+
}
|
|
255
|
+
static fromZodLiteral(zodLiteral) {
|
|
256
|
+
if (zodLiteral._zod.def.values.length === 0) {
|
|
257
|
+
throw new Error("ZodLiteral has no values");
|
|
258
|
+
}
|
|
259
|
+
const value = zodLiteral._zod.def.values[0];
|
|
260
|
+
if (typeof value === "string") {
|
|
261
|
+
return import_typescript.factory.createLiteralTypeNode(import_typescript.factory.createStringLiteral(value));
|
|
262
|
+
}
|
|
263
|
+
if (typeof value === "number") {
|
|
264
|
+
return import_typescript.factory.createLiteralTypeNode(import_typescript.factory.createNumericLiteral(value));
|
|
265
|
+
}
|
|
266
|
+
if (typeof value === "boolean") {
|
|
267
|
+
return import_typescript.factory.createLiteralTypeNode(
|
|
268
|
+
value ? import_typescript.factory.createTrue() : import_typescript.factory.createFalse()
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
if (typeof value === "bigint") {
|
|
272
|
+
return import_typescript.factory.createLiteralTypeNode(
|
|
273
|
+
import_typescript.factory.createBigIntLiteral(value.toString())
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
if (value === null) {
|
|
277
|
+
return import_typescript.factory.createLiteralTypeNode(import_typescript.factory.createNull());
|
|
278
|
+
}
|
|
279
|
+
if (value === void 0) {
|
|
280
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UndefinedKeyword);
|
|
281
|
+
}
|
|
282
|
+
throw new Error(`Unsupported literal type: ${typeof value}`);
|
|
283
|
+
}
|
|
284
|
+
static fromZodEnum(zodEnum) {
|
|
285
|
+
const entries = Object.entries(zodEnum._zod.def.entries);
|
|
286
|
+
const types = entries.map(([key, value]) => {
|
|
287
|
+
return import_typescript.factory.createLiteralTypeNode(import_typescript.factory.createStringLiteral(key));
|
|
288
|
+
});
|
|
289
|
+
return import_typescript.factory.createUnionTypeNode(types);
|
|
290
|
+
}
|
|
291
|
+
static fromZodPromise(zodPromise) {
|
|
292
|
+
const innerType = _TsTypeNode.fromZod(zodPromise._zod.def.innerType);
|
|
293
|
+
return import_typescript.factory.createTypeReferenceNode(
|
|
294
|
+
import_typescript.factory.createIdentifier("Promise"),
|
|
295
|
+
[innerType]
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
static fromZodLazy(_zodLazy) {
|
|
299
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
300
|
+
}
|
|
301
|
+
static fromZodOptional(zodOptional) {
|
|
302
|
+
const innerType = _TsTypeNode.fromZod(zodOptional._zod.def.innerType);
|
|
303
|
+
return import_typescript.factory.createUnionTypeNode([
|
|
304
|
+
innerType,
|
|
305
|
+
import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UndefinedKeyword)
|
|
306
|
+
]);
|
|
307
|
+
}
|
|
308
|
+
static fromZodDefault(zodDefault) {
|
|
309
|
+
const innerType = _TsTypeNode.fromZod(zodDefault._zod.def.innerType);
|
|
310
|
+
const filteredNodes = [];
|
|
311
|
+
innerType.forEachChild((node) => {
|
|
312
|
+
if (node.kind !== import_typescript.SyntaxKind.UndefinedKeyword) {
|
|
313
|
+
filteredNodes.push(node);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
innerType.types = filteredNodes;
|
|
317
|
+
return innerType;
|
|
318
|
+
}
|
|
319
|
+
static fromZodTemplateLiteral(_zodTemplateLiteral) {
|
|
320
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
321
|
+
}
|
|
322
|
+
static fromZodCustom(_zodCustom) {
|
|
323
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
324
|
+
}
|
|
325
|
+
static fromZodTransform(_zodTransform) {
|
|
326
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
327
|
+
}
|
|
328
|
+
static fromZodNonOptional(_zodNonOptional) {
|
|
329
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
330
|
+
}
|
|
331
|
+
static fromZodReadonly(_zodReadonly) {
|
|
332
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
333
|
+
}
|
|
334
|
+
static fromZodNaN(_zodNaN) {
|
|
335
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
336
|
+
}
|
|
337
|
+
static fromZodPipe(_zodPipe) {
|
|
338
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
339
|
+
}
|
|
340
|
+
static fromZodSuccess(_zodSuccess) {
|
|
341
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
342
|
+
}
|
|
343
|
+
static fromZodCatch(_zodCatch) {
|
|
344
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
345
|
+
}
|
|
346
|
+
static fromZodFile(_zodFile) {
|
|
347
|
+
return import_typescript.factory.createKeywordTypeNode(import_typescript.SyntaxKind.UnknownKeyword);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Returns a TypeScript AST node representing the property key.
|
|
351
|
+
* If the key is a valid JavaScript identifier, returns an Identifier node.
|
|
352
|
+
* Otherwise, returns a StringLiteral node.
|
|
353
|
+
*
|
|
354
|
+
* @param {string} key - The property key to convert.
|
|
355
|
+
* @returns {Identifier | StringLiteral} The corresponding AST node for the property key.
|
|
356
|
+
*/
|
|
357
|
+
static createTsAstPropertyKey(key) {
|
|
358
|
+
if (/^[$A-Z_a-z][\w$]*$/.test(key)) {
|
|
359
|
+
return import_typescript.factory.createIdentifier(key);
|
|
360
|
+
}
|
|
361
|
+
return import_typescript.factory.createStringLiteral(key);
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
// src/TsTypePrinter.ts
|
|
366
|
+
var import_typescript2 = require("typescript");
|
|
367
|
+
var TsTypePrinter = class {
|
|
368
|
+
static print(tsType) {
|
|
369
|
+
const sourceFile = (0, import_typescript2.createSourceFile)(
|
|
370
|
+
"print.ts",
|
|
371
|
+
"",
|
|
372
|
+
import_typescript2.ScriptTarget.Latest,
|
|
373
|
+
false,
|
|
374
|
+
import_typescript2.ScriptKind.TS
|
|
375
|
+
);
|
|
376
|
+
const printer = (0, import_typescript2.createPrinter)();
|
|
377
|
+
return printer.printNode(import_typescript2.EmitHint.Unspecified, tsType, sourceFile);
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
381
|
+
0 && (module.exports = {
|
|
382
|
+
TsTypeNode,
|
|
383
|
+
TsTypePrinter
|
|
384
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { TypeNode } from 'typescript';
|
|
2
|
+
import { $ZodType } from 'zod/v4/core';
|
|
3
|
+
|
|
4
|
+
declare class TsTypeNode {
|
|
5
|
+
static fromZod(zodType: $ZodType): TypeNode;
|
|
6
|
+
private static fromZodString;
|
|
7
|
+
private static fromZodNumber;
|
|
8
|
+
private static fromZodBigInt;
|
|
9
|
+
private static fromZodBoolean;
|
|
10
|
+
private static fromZodDate;
|
|
11
|
+
private static fromZodSymbol;
|
|
12
|
+
private static fromZodUndefined;
|
|
13
|
+
private static fromZodNullable;
|
|
14
|
+
private static fromZodNull;
|
|
15
|
+
private static fromZodAny;
|
|
16
|
+
private static fromZodUnknown;
|
|
17
|
+
private static fromZodNever;
|
|
18
|
+
private static fromZodVoid;
|
|
19
|
+
private static fromZodArray;
|
|
20
|
+
private static fromZodObject;
|
|
21
|
+
private static fromZodUnion;
|
|
22
|
+
private static fromZodIntersection;
|
|
23
|
+
private static fromZodTuple;
|
|
24
|
+
private static fromZodRecord;
|
|
25
|
+
private static fromZodMap;
|
|
26
|
+
private static fromZodSet;
|
|
27
|
+
private static fromZodLiteral;
|
|
28
|
+
private static fromZodEnum;
|
|
29
|
+
private static fromZodPromise;
|
|
30
|
+
private static fromZodLazy;
|
|
31
|
+
private static fromZodOptional;
|
|
32
|
+
private static fromZodDefault;
|
|
33
|
+
private static fromZodTemplateLiteral;
|
|
34
|
+
private static fromZodCustom;
|
|
35
|
+
private static fromZodTransform;
|
|
36
|
+
private static fromZodNonOptional;
|
|
37
|
+
private static fromZodReadonly;
|
|
38
|
+
private static fromZodNaN;
|
|
39
|
+
private static fromZodPipe;
|
|
40
|
+
private static fromZodSuccess;
|
|
41
|
+
private static fromZodCatch;
|
|
42
|
+
private static fromZodFile;
|
|
43
|
+
/**
|
|
44
|
+
* Returns a TypeScript AST node representing the property key.
|
|
45
|
+
* If the key is a valid JavaScript identifier, returns an Identifier node.
|
|
46
|
+
* Otherwise, returns a StringLiteral node.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} key - The property key to convert.
|
|
49
|
+
* @returns {Identifier | StringLiteral} The corresponding AST node for the property key.
|
|
50
|
+
*/
|
|
51
|
+
private static createTsAstPropertyKey;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
declare class TsTypePrinter {
|
|
55
|
+
static print(tsType: TypeNode): string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { TsTypeNode, TsTypePrinter };
|
package/dist/index.js
CHANGED
|
@@ -1,121 +1,159 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/TsTypeGenerator.ts
|
|
2
|
+
import { factory, SyntaxKind } from "typescript";
|
|
3
|
+
import {
|
|
4
|
+
$ZodAny,
|
|
5
|
+
$ZodArray,
|
|
6
|
+
$ZodBigInt,
|
|
7
|
+
$ZodBoolean,
|
|
8
|
+
$ZodCatch,
|
|
9
|
+
$ZodCustom,
|
|
10
|
+
$ZodDate,
|
|
11
|
+
$ZodDefault,
|
|
12
|
+
$ZodEnum,
|
|
13
|
+
$ZodFile,
|
|
14
|
+
$ZodIntersection,
|
|
15
|
+
$ZodLazy,
|
|
16
|
+
$ZodLiteral,
|
|
17
|
+
$ZodMap,
|
|
18
|
+
$ZodNaN,
|
|
19
|
+
$ZodNever,
|
|
20
|
+
$ZodNonOptional,
|
|
21
|
+
$ZodNull,
|
|
22
|
+
$ZodNullable,
|
|
23
|
+
$ZodNumber,
|
|
24
|
+
$ZodObject,
|
|
25
|
+
$ZodOptional,
|
|
26
|
+
$ZodPipe,
|
|
27
|
+
$ZodPromise,
|
|
28
|
+
$ZodReadonly,
|
|
29
|
+
$ZodRecord,
|
|
30
|
+
$ZodSet,
|
|
31
|
+
$ZodString,
|
|
32
|
+
$ZodSuccess,
|
|
33
|
+
$ZodSymbol,
|
|
34
|
+
$ZodTemplateLiteral,
|
|
35
|
+
$ZodTransform,
|
|
36
|
+
$ZodTuple,
|
|
37
|
+
$ZodUndefined,
|
|
38
|
+
$ZodUnion,
|
|
39
|
+
$ZodUnknown,
|
|
40
|
+
$ZodVoid
|
|
41
|
+
} from "zod/v4/core";
|
|
42
|
+
var TsTypeNode = class _TsTypeNode {
|
|
5
43
|
static fromZod(zodType) {
|
|
6
44
|
if (zodType instanceof $ZodString) {
|
|
7
|
-
return
|
|
45
|
+
return _TsTypeNode.fromZodString(zodType);
|
|
8
46
|
}
|
|
9
47
|
if (zodType instanceof $ZodNumber) {
|
|
10
|
-
return
|
|
48
|
+
return _TsTypeNode.fromZodNumber(zodType);
|
|
11
49
|
}
|
|
12
50
|
if (zodType instanceof $ZodBigInt) {
|
|
13
|
-
return
|
|
51
|
+
return _TsTypeNode.fromZodBigInt(zodType);
|
|
14
52
|
}
|
|
15
53
|
if (zodType instanceof $ZodBoolean) {
|
|
16
|
-
return
|
|
54
|
+
return _TsTypeNode.fromZodBoolean(zodType);
|
|
17
55
|
}
|
|
18
56
|
if (zodType instanceof $ZodDate) {
|
|
19
|
-
return
|
|
57
|
+
return _TsTypeNode.fromZodDate(zodType);
|
|
20
58
|
}
|
|
21
59
|
if (zodType instanceof $ZodSymbol) {
|
|
22
|
-
return
|
|
60
|
+
return _TsTypeNode.fromZodSymbol(zodType);
|
|
23
61
|
}
|
|
24
62
|
if (zodType instanceof $ZodUndefined) {
|
|
25
|
-
return
|
|
63
|
+
return _TsTypeNode.fromZodUndefined(zodType);
|
|
26
64
|
}
|
|
27
65
|
if (zodType instanceof $ZodNullable) {
|
|
28
|
-
return
|
|
66
|
+
return _TsTypeNode.fromZodNullable(zodType);
|
|
29
67
|
}
|
|
30
68
|
if (zodType instanceof $ZodNull) {
|
|
31
|
-
return
|
|
69
|
+
return _TsTypeNode.fromZodNull(zodType);
|
|
32
70
|
}
|
|
33
71
|
if (zodType instanceof $ZodAny) {
|
|
34
|
-
return
|
|
72
|
+
return _TsTypeNode.fromZodAny(zodType);
|
|
35
73
|
}
|
|
36
74
|
if (zodType instanceof $ZodUnknown) {
|
|
37
|
-
return
|
|
75
|
+
return _TsTypeNode.fromZodUnknown(zodType);
|
|
38
76
|
}
|
|
39
77
|
if (zodType instanceof $ZodNever) {
|
|
40
|
-
return
|
|
78
|
+
return _TsTypeNode.fromZodNever(zodType);
|
|
41
79
|
}
|
|
42
80
|
if (zodType instanceof $ZodVoid) {
|
|
43
|
-
return
|
|
81
|
+
return _TsTypeNode.fromZodVoid(zodType);
|
|
44
82
|
}
|
|
45
83
|
if (zodType instanceof $ZodArray) {
|
|
46
|
-
return
|
|
84
|
+
return _TsTypeNode.fromZodArray(zodType);
|
|
47
85
|
}
|
|
48
86
|
if (zodType instanceof $ZodObject) {
|
|
49
|
-
return
|
|
87
|
+
return _TsTypeNode.fromZodObject(zodType);
|
|
50
88
|
}
|
|
51
89
|
if (zodType instanceof $ZodUnion) {
|
|
52
|
-
return
|
|
90
|
+
return _TsTypeNode.fromZodUnion(zodType);
|
|
53
91
|
}
|
|
54
92
|
if (zodType instanceof $ZodIntersection) {
|
|
55
|
-
return
|
|
93
|
+
return _TsTypeNode.fromZodIntersection(zodType);
|
|
56
94
|
}
|
|
57
95
|
if (zodType instanceof $ZodTuple) {
|
|
58
|
-
return
|
|
96
|
+
return _TsTypeNode.fromZodTuple(zodType);
|
|
59
97
|
}
|
|
60
98
|
if (zodType instanceof $ZodRecord) {
|
|
61
|
-
return
|
|
99
|
+
return _TsTypeNode.fromZodRecord(zodType);
|
|
62
100
|
}
|
|
63
101
|
if (zodType instanceof $ZodMap) {
|
|
64
|
-
return
|
|
102
|
+
return _TsTypeNode.fromZodMap(zodType);
|
|
65
103
|
}
|
|
66
104
|
if (zodType instanceof $ZodSet) {
|
|
67
|
-
return
|
|
105
|
+
return _TsTypeNode.fromZodSet(zodType);
|
|
68
106
|
}
|
|
69
107
|
if (zodType instanceof $ZodLiteral) {
|
|
70
|
-
if (!
|
|
108
|
+
if (!_TsTypeNode) {
|
|
71
109
|
throw new Error("ZodLiteral has no values");
|
|
72
110
|
}
|
|
73
|
-
return
|
|
111
|
+
return _TsTypeNode.fromZodLiteral(zodType);
|
|
74
112
|
}
|
|
75
113
|
if (zodType instanceof $ZodEnum) {
|
|
76
|
-
return
|
|
114
|
+
return _TsTypeNode.fromZodEnum(zodType);
|
|
77
115
|
}
|
|
78
116
|
if (zodType instanceof $ZodPromise) {
|
|
79
|
-
return
|
|
117
|
+
return _TsTypeNode.fromZodPromise(zodType);
|
|
80
118
|
}
|
|
81
119
|
if (zodType instanceof $ZodLazy) {
|
|
82
|
-
return
|
|
120
|
+
return _TsTypeNode.fromZodLazy(zodType);
|
|
83
121
|
}
|
|
84
122
|
if (zodType instanceof $ZodOptional) {
|
|
85
|
-
return
|
|
123
|
+
return _TsTypeNode.fromZodOptional(zodType);
|
|
86
124
|
}
|
|
87
125
|
if (zodType instanceof $ZodDefault) {
|
|
88
|
-
return
|
|
126
|
+
return _TsTypeNode.fromZodDefault(zodType);
|
|
89
127
|
}
|
|
90
128
|
if (zodType instanceof $ZodTemplateLiteral) {
|
|
91
|
-
return
|
|
129
|
+
return _TsTypeNode.fromZodTemplateLiteral(zodType);
|
|
92
130
|
}
|
|
93
131
|
if (zodType instanceof $ZodCustom) {
|
|
94
|
-
return
|
|
132
|
+
return _TsTypeNode.fromZodCustom(zodType);
|
|
95
133
|
}
|
|
96
134
|
if (zodType instanceof $ZodTransform) {
|
|
97
|
-
return
|
|
135
|
+
return _TsTypeNode.fromZodTransform(zodType);
|
|
98
136
|
}
|
|
99
137
|
if (zodType instanceof $ZodNonOptional) {
|
|
100
|
-
return
|
|
138
|
+
return _TsTypeNode.fromZodNonOptional(zodType);
|
|
101
139
|
}
|
|
102
140
|
if (zodType instanceof $ZodReadonly) {
|
|
103
|
-
return
|
|
141
|
+
return _TsTypeNode.fromZodReadonly(zodType);
|
|
104
142
|
}
|
|
105
143
|
if (zodType instanceof $ZodNaN) {
|
|
106
|
-
return
|
|
144
|
+
return _TsTypeNode.fromZodNaN(zodType);
|
|
107
145
|
}
|
|
108
146
|
if (zodType instanceof $ZodPipe) {
|
|
109
|
-
return
|
|
147
|
+
return _TsTypeNode.fromZodPipe(zodType);
|
|
110
148
|
}
|
|
111
149
|
if (zodType instanceof $ZodSuccess) {
|
|
112
|
-
return
|
|
150
|
+
return _TsTypeNode.fromZodSuccess(zodType);
|
|
113
151
|
}
|
|
114
152
|
if (zodType instanceof $ZodCatch) {
|
|
115
|
-
return
|
|
153
|
+
return _TsTypeNode.fromZodCatch(zodType);
|
|
116
154
|
}
|
|
117
155
|
if (zodType instanceof $ZodFile) {
|
|
118
|
-
return
|
|
156
|
+
return _TsTypeNode.fromZodFile(zodType);
|
|
119
157
|
}
|
|
120
158
|
return factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword);
|
|
121
159
|
}
|
|
@@ -141,7 +179,7 @@ class TsTypeNode {
|
|
|
141
179
|
return factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword);
|
|
142
180
|
}
|
|
143
181
|
static fromZodNullable(zodNullable) {
|
|
144
|
-
const innerType =
|
|
182
|
+
const innerType = _TsTypeNode.fromZod(zodNullable._zod.def.innerType);
|
|
145
183
|
return factory.createUnionTypeNode([
|
|
146
184
|
innerType,
|
|
147
185
|
factory.createLiteralTypeNode(factory.createNull())
|
|
@@ -163,13 +201,13 @@ class TsTypeNode {
|
|
|
163
201
|
return factory.createKeywordTypeNode(SyntaxKind.VoidKeyword);
|
|
164
202
|
}
|
|
165
203
|
static fromZodArray(zodArray) {
|
|
166
|
-
const innerType =
|
|
204
|
+
const innerType = _TsTypeNode.fromZod(zodArray._zod.def.element);
|
|
167
205
|
return factory.createArrayTypeNode(innerType);
|
|
168
206
|
}
|
|
169
207
|
static fromZodObject(zodObject) {
|
|
170
208
|
const entries = Object.entries(zodObject._zod.def.shape);
|
|
171
209
|
const members = entries.map(([key, nextZodNode]) => {
|
|
172
|
-
const type =
|
|
210
|
+
const type = _TsTypeNode.fromZod(nextZodNode);
|
|
173
211
|
if (!nextZodNode._zod?.def) {
|
|
174
212
|
console.warn(
|
|
175
213
|
`Zod node for key "${key}" does not have a _zod.def property. This may indicate an issue with the Zod schema.`,
|
|
@@ -182,7 +220,7 @@ class TsTypeNode {
|
|
|
182
220
|
const isOptional = nextZodNodeTypeName === "optional";
|
|
183
221
|
const propertySignature = factory.createPropertySignature(
|
|
184
222
|
void 0,
|
|
185
|
-
|
|
223
|
+
_TsTypeNode.createTsAstPropertyKey(key),
|
|
186
224
|
isOptional ? factory.createToken(SyntaxKind.QuestionToken) : void 0,
|
|
187
225
|
type
|
|
188
226
|
);
|
|
@@ -191,36 +229,36 @@ class TsTypeNode {
|
|
|
191
229
|
return factory.createTypeLiteralNode(members);
|
|
192
230
|
}
|
|
193
231
|
static fromZodUnion(zodUnion) {
|
|
194
|
-
const options = zodUnion._zod.def.options.map(
|
|
232
|
+
const options = zodUnion._zod.def.options.map(_TsTypeNode.fromZod);
|
|
195
233
|
return factory.createUnionTypeNode(options);
|
|
196
234
|
}
|
|
197
235
|
static fromZodIntersection(zodIntersection) {
|
|
198
|
-
const left =
|
|
199
|
-
const right =
|
|
236
|
+
const left = _TsTypeNode.fromZod(zodIntersection._zod.def.left);
|
|
237
|
+
const right = _TsTypeNode.fromZod(zodIntersection._zod.def.right);
|
|
200
238
|
return factory.createIntersectionTypeNode([left, right]);
|
|
201
239
|
}
|
|
202
240
|
static fromZodTuple(zodTuple) {
|
|
203
|
-
const elements = zodTuple._zod.def.items.map(
|
|
241
|
+
const elements = zodTuple._zod.def.items.map(_TsTypeNode.fromZod);
|
|
204
242
|
return factory.createTupleTypeNode(elements);
|
|
205
243
|
}
|
|
206
244
|
static fromZodRecord(zodRecord) {
|
|
207
|
-
const keyType =
|
|
208
|
-
const valueType =
|
|
245
|
+
const keyType = _TsTypeNode.fromZod(zodRecord._zod.def.keyType);
|
|
246
|
+
const valueType = _TsTypeNode.fromZod(zodRecord._zod.def.valueType);
|
|
209
247
|
return factory.createTypeReferenceNode(factory.createIdentifier("Record"), [
|
|
210
248
|
keyType,
|
|
211
249
|
valueType
|
|
212
250
|
]);
|
|
213
251
|
}
|
|
214
252
|
static fromZodMap(zodMap) {
|
|
215
|
-
const keyType =
|
|
216
|
-
const valueType =
|
|
253
|
+
const keyType = _TsTypeNode.fromZod(zodMap._zod.def.keyType);
|
|
254
|
+
const valueType = _TsTypeNode.fromZod(zodMap._zod.def.valueType);
|
|
217
255
|
return factory.createTypeReferenceNode(factory.createIdentifier("Map"), [
|
|
218
256
|
keyType,
|
|
219
257
|
valueType
|
|
220
258
|
]);
|
|
221
259
|
}
|
|
222
260
|
static fromZodSet(zodSet) {
|
|
223
|
-
const innerType =
|
|
261
|
+
const innerType = _TsTypeNode.fromZod(zodSet._zod.def.valueType);
|
|
224
262
|
return factory.createTypeReferenceNode(factory.createIdentifier("Set"), [
|
|
225
263
|
innerType
|
|
226
264
|
]);
|
|
@@ -262,7 +300,7 @@ class TsTypeNode {
|
|
|
262
300
|
return factory.createUnionTypeNode(types);
|
|
263
301
|
}
|
|
264
302
|
static fromZodPromise(zodPromise) {
|
|
265
|
-
const innerType =
|
|
303
|
+
const innerType = _TsTypeNode.fromZod(zodPromise._zod.def.innerType);
|
|
266
304
|
return factory.createTypeReferenceNode(
|
|
267
305
|
factory.createIdentifier("Promise"),
|
|
268
306
|
[innerType]
|
|
@@ -272,14 +310,14 @@ class TsTypeNode {
|
|
|
272
310
|
return factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword);
|
|
273
311
|
}
|
|
274
312
|
static fromZodOptional(zodOptional) {
|
|
275
|
-
const innerType =
|
|
313
|
+
const innerType = _TsTypeNode.fromZod(zodOptional._zod.def.innerType);
|
|
276
314
|
return factory.createUnionTypeNode([
|
|
277
315
|
innerType,
|
|
278
316
|
factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword)
|
|
279
317
|
]);
|
|
280
318
|
}
|
|
281
319
|
static fromZodDefault(zodDefault) {
|
|
282
|
-
const innerType =
|
|
320
|
+
const innerType = _TsTypeNode.fromZod(zodDefault._zod.def.innerType);
|
|
283
321
|
const filteredNodes = [];
|
|
284
322
|
innerType.forEachChild((node) => {
|
|
285
323
|
if (node.kind !== SyntaxKind.UndefinedKeyword) {
|
|
@@ -333,9 +371,17 @@ class TsTypeNode {
|
|
|
333
371
|
}
|
|
334
372
|
return factory.createStringLiteral(key);
|
|
335
373
|
}
|
|
336
|
-
}
|
|
374
|
+
};
|
|
337
375
|
|
|
338
|
-
|
|
376
|
+
// src/TsTypePrinter.ts
|
|
377
|
+
import {
|
|
378
|
+
createPrinter,
|
|
379
|
+
createSourceFile,
|
|
380
|
+
EmitHint,
|
|
381
|
+
ScriptKind,
|
|
382
|
+
ScriptTarget
|
|
383
|
+
} from "typescript";
|
|
384
|
+
var TsTypePrinter = class {
|
|
339
385
|
static print(tsType) {
|
|
340
386
|
const sourceFile = createSourceFile(
|
|
341
387
|
"print.ts",
|
|
@@ -347,6 +393,8 @@ class TsTypePrinter {
|
|
|
347
393
|
const printer = createPrinter();
|
|
348
394
|
return printer.printNode(EmitHint.Unspecified, tsType, sourceFile);
|
|
349
395
|
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
|
|
396
|
+
};
|
|
397
|
+
export {
|
|
398
|
+
TsTypeNode,
|
|
399
|
+
TsTypePrinter
|
|
400
|
+
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rexeus/typeweaver-zod-to-ts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Transforms Zod v4 schemas into precise TypeScript shapes. Powered by Typeweaver 🧵✨",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/index.
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
7
8
|
"types": "dist/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
|
-
"import":
|
|
11
|
-
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
12
19
|
}
|
|
13
20
|
},
|
|
14
21
|
"files": [
|
|
@@ -41,15 +48,15 @@
|
|
|
41
48
|
"typescript": "^5.9.3"
|
|
42
49
|
},
|
|
43
50
|
"peerDependencies": {
|
|
44
|
-
"zod": "^4.
|
|
51
|
+
"zod": "^4.3.5"
|
|
45
52
|
},
|
|
46
53
|
"devDependencies": {
|
|
47
|
-
"zod": "^4.
|
|
54
|
+
"zod": "^4.3.5"
|
|
48
55
|
},
|
|
49
56
|
"scripts": {
|
|
50
57
|
"typecheck": "tsc --noEmit",
|
|
51
58
|
"format": "prettier --write .",
|
|
52
|
-
"build": "
|
|
59
|
+
"build": "tsup && cp ../../LICENSE ../../NOTICE ./dist/",
|
|
53
60
|
"preversion": "npm run build"
|
|
54
61
|
}
|
|
55
62
|
}
|