UNPKG

zod-to-x

Version:

Multi language types generation from Zod schemas.

107 lines (106 loc) 3.97 kB
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTNode, ASTObject, ASTUnion, Zod2X } from "../../core"; import { IZod2TsOpt } from "./options"; export declare class Zod2Ts extends Zod2X<IZod2TsOpt> { protected readonly commentKey = "//"; constructor(opt?: IZod2TsOpt); protected runAfter(): void; protected runBefore(): void; protected addImportFromFile(filename: string, namespace: string): string; protected getTypeFromExternalNamespace(namespace: string, typeName: string): string; protected addExtendedType(name: string, parentNamespace: string, aliasOf: string, opt?: { type?: "union" | "d-union" | "alias"; isInternal?: boolean; templates?: string; declaredTemplates?: string; }): void; protected getGenericTemplatesTranslation(data: ASTNode): string | undefined; /** * Emits an alias/extension declaration early when a node references another layered type. * It preserves concrete template translations and falls back to declared templates (e.g. <T>) * for aliases of generic templates. */ protected checkExtendedTypeInclusion(data: ASTNode, type?: "alias" | "union" | "d-union"): boolean; protected getAnyType: () => string; protected getBooleanType: () => string; protected getDateType: () => string; /** Ex: Set<TypeA> */ protected getSetType: (itemType: string) => string; protected getStringType: () => string; /** Ex: [TypeA, TypeB] */ protected getTupleType: (itemsType: string[]) => string; /** Ex: TypeA | TypeB */ protected getUnionType: (itemsType: string[]) => string; /** Ex: TypeA & TypeB */ protected getIntersectionType: (itemsType: string[]) => string; protected getNumberType: () => string; /** Ex: Array<Array<TypeA[]>> */ protected getArrayType(arrayType: string, arrayDeep: number): string; protected getLiteralStringType(value: string | number, parentEnumNameKey?: [string, string]): string | number; /** Ex: Map<TypeA, TypeB> */ protected getMapType(keyType: string, valueType: string): string; /** Ex: Record<TypeA, TypeB> */ protected getRecordType(keyType: string, valueType: string): string; protected transpileAliasedType(data: ASTAliasedTypes): void; /** Ex: * enum { * ItemKey1: 0, // case of nativeEnum * ItemKey2: "ItemValue2" // case of Enum * } */ protected transpileEnum(data: ASTEnum): void; /** Ex: * // Interface output * // Class output if non-object intersection * type TypeC = TypeA & TypeB * * // Class output all-object intersection * class TypeC { * ...attributesTypeA, * ...attributesTypeB * * constructor(data: TypeC) { * ...attributesAssignment * } * } * */ protected transpileIntersection(data: ASTIntersection): void; protected transpileStruct(data: ASTObject): void; /** Ex: * // Interface output * // Class output for Discriminated Union or non-objects union * type TypeC = TypeA | TypeB * * // Class output for all-object Union * class TypeC { * ...attributesTypeA, * ...attributesTypeB * * constructor(data: TypeC) { * ...attributesAssignment * } * } * */ protected transpileUnion(data: ASTUnion): void; /** Ex: * interface MyStruct { * att1: TypeA; * att2?: TypeB; * } * */ private _transpileStructuAsInterface; /** Ex: * class MyStruct { * att1: TypeA; * att2?: TypeB; * * constructor(data: MyStruct) { * this.att1 = data.att1; * this.att2 = data.att2; * } * } * */ private _transpileStructAsClass; /** For Interface/Class attributes. * Ex: attribute1?: TypeA | null */ private _transpileMember; }