import type { FormValue, ParsedFormValue, ValidationTargets } from '../types' import type { UnionToIntersection } from '../utils/types' /** * 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 = [Exclude] extends [Base] ? [Exclude] extends [UnionToIntersection>] ? false : true : false // Check if type is an optional union (T | undefined) but not unknown/any type IsOptionalUnion = [unknown] extends [T] ? false // unknown or any : undefined extends T ? true : false // Helper to force TypeScript to expand type aliases type SimplifyDeep = { [K in keyof T]: T[K] } & {} type InferInputInner< Output, Target extends keyof ValidationTargets, T extends FormValue, > = SimplifyDeep<{ [K in keyof Output]: IsLiteralUnion extends true ? Output[K] : IsOptionalUnion 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, 'query'> * // { orderBy: 'asc' | 'desc', page: string | string[] } * ``` */ export type InferInput< Output, Target extends keyof ValidationTargets, T extends FormValue = ParsedFormValue, > = [Exclude] extends [never] ? {} : [Exclude] extends [object] ? undefined extends Output ? SimplifyDeep, Target, T>> | undefined : SimplifyDeep> : {}