Files
SnowLuma/.pnpm-store/v11/files/00/669c891277ce843918e3d688b8926eaf26c0fd54c4f7e1554fb810d5be5144fc27c12430eef960da035c7118989d27f76fc16acf72b8c8a0c40b2e445ee84e
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

28 lines
1.9 KiB
Plaintext

import { UnionToIntersection } from "../utils/types.js";
import { FormValue, ParsedFormValue, ValidationTargets } from "../types.js";
//#region src/validator/utils.d.ts
/**
* Checks if T is a literal union type (e.g., 'asc' | 'desc')
* that should be preserved in input types.
* Returns true for union literals, false for single literals or wide types.
*/
export type IsLiteralUnion<T, Base> = [Exclude<T, undefined>] extends [Base] ? [Exclude<T, undefined>] extends [UnionToIntersection<Exclude<T, undefined>>] ? false : true : false;
type IsOptionalUnion<T> = [unknown] extends [T] ? false : undefined extends T ? true : false;
type SimplifyDeep<T> = { [K in keyof T]: T[K]; } & {};
type InferInputInner<Output, Target extends keyof ValidationTargets, T extends FormValue> = SimplifyDeep<{ [K in keyof Output]: IsLiteralUnion<Output[K], string> extends true ? Output[K] : IsOptionalUnion<Output[K]> extends true ? Output[K] : Target extends 'form' ? T | T[] : Target extends 'query' ? string | string[] : Target extends 'param' ? string : Target extends 'header' ? string : Target extends 'cookie' ? string : unknown; }>;
/**
* Utility type to infer input types for validation targets.
* Preserves literal union types (e.g., 'asc' | 'desc') while using
* the default ValidationTargets type for other values.
*
* @example
* ```ts
* // In @hono/zod-validator or similar:
* type Input = InferInput<z.input<Schema>, 'query'>
* // { orderBy: 'asc' | 'desc', page: string | string[] }
* ```
*/
export type InferInput<Output, Target extends keyof ValidationTargets, T extends FormValue = ParsedFormValue> = [Exclude<Output, undefined>] extends [never] ? {} : [Exclude<Output, undefined>] extends [object] ? undefined extends Output ? SimplifyDeep<InferInputInner<Exclude<Output, undefined>, Target, T>> | undefined : SimplifyDeep<InferInputInner<Output, Target, T>> : {};
//#endregion
export {};