UNPKG

@duplojs/zod-to-typescript

Version:
64 lines (63 loc) 2.62 kB
import { ZodType, z as zod } from "zod"; import ts, { type TypeNode, type JSDocContainer, type Declaration, type Identifier } from "typescript"; export interface NamedDeclaration extends Declaration, JSDocContainer { readonly name: Identifier; } export type MapContext = Map<ZodType, NamedDeclaration>; export interface ConvertIdentifier { zodSchema: ZodType; name: string; } export interface ConvertOptions { name?: string; context?: MapContext; /** * @deprecated careless mistake in the name - use `identifiers` instead */ indentifiers?: (ConvertIdentifier | ZodType)[]; identifiers?: (ConvertIdentifier | ZodType)[]; export?: boolean; zodSchemaHooks?: ZodSchemaHook[]; } export interface FindTypescriptTransformatorOptions { skipNextDeclarationStatement?: boolean; skipNextZodSchemaHooks?: boolean; } declare module "zod" { interface ZodType<Output = any, Def extends zod.ZodTypeDef = zod.ZodTypeDef, Input = Output> { _zttIdentifier?: string; _zttOverrideTypeNode?: TypeNode; identifier(name: string): ZodType<Output, Def, Input>; overrideTypeNode(typeNode: TypeNode | ((typescript: typeof ts) => TypeNode)): ZodType<Output, Def, Input>; } } export interface TypescriptTransformatorTools { context: MapContext; findTypescriptTransformator(zodSchema: ZodType): TypeNode; } export interface TypescriptTransformator { support(zodSchema: ZodType): boolean; makeTypeNode(zodSchema: ZodType, tools: TypescriptTransformatorTools): TypeNode; } export type ZodSchemaHookAction = "stop" | "next"; export interface ZodSchemaHookOutput { zodSchema: ZodType; action: ZodSchemaHookAction; } export type ZodSchemaHook = (zodSchema: ZodType, context: MapContext, output: (action: ZodSchemaHookAction, zodSchema: ZodType) => ZodSchemaHookOutput) => ZodSchemaHookOutput; export declare class ZodToTypescript { aliasContext: MapContext; zodSchemaHooks: ZodSchemaHook[]; typescriptTransformators: TypescriptTransformator[]; static typescriptTransformators: TypescriptTransformator[]; static zod: typeof zod; private static count; constructor(typescriptTransformators?: TypescriptTransformator[]); private findTypescriptTransformator; append(zodSchema: ZodType, name?: string): void; toString(exportType?: boolean): string; static stringifyContext(context: MapContext, exportType?: boolean): string; static convert(zodSchema: ZodType, options?: ConvertOptions): string; static getIdentifier(): string; static injectZod<Z extends typeof zod>(zod: Z): void; }