// #region utility types export type MaybePromise = T | Promise; type MaybeArray = T | T[]; // #endregion // #region form input types /** * valid structure for form input data */ export interface FormInput { [key: string]: MaybeArray; } /** * a validation issue with path information */ export interface FormIssue { message: string; path: (string | number)[]; } /** * internal representation of a form validation issue with computed path info */ export interface InternalFormIssue { /** dot/bracket notation path string (e.g., "user.emails[0]") */ name: string; /** path segments as array */ path: (string | number)[]; /** error message */ message: string; /** whether this issue came from server validation */ server: boolean; } // #endregion // #region input type mapping /** * maps input types to their corresponding value types */ export type InputTypeMap = { text: string; email: string; password: string; url: string; tel: string; search: string; number: number; range: number; date: string; 'datetime-local': string; time: string; month: string; week: string; color: string; checkbox: boolean | string[]; radio: string; file: File; 'file multiple': File[]; hidden: string; submit: string; button: string; reset: string; image: string; select: string; 'select multiple': string[]; }; /** * all valid input type strings */ export type InputType = keyof InputTypeMap; /** * valid input types for a given value type */ export type FieldInputType = { [K in keyof InputTypeMap]: T extends InputTypeMap[K] ? K : never; }[keyof InputTypeMap]; /** * value argument for the `as()` method based on input type. * returns `[value]` tuple for types that require a value, or `[]` otherwise. * * note: separating `type` from `value` in the `.as()` signature ensures TypeScript * can properly infer the type parameter before resolving the value constraint. * using `...args: [type, value?]` with a union of tuple types causes inference issues. */ export type AsValueArgs = Type extends 'checkbox' ? Value extends string[] ? [value: Value[number] | (string & {})] : [] : Type extends 'radio' | 'submit' | 'hidden' ? [value: Value | (string & {})] : []; // #endregion // #region input element props interface CheckboxRadioProps { name: string; type: T; value: string; 'aria-invalid'?: 'true'; readonly checked: boolean; } interface FileProps { name: string; type: 'file'; 'aria-invalid'?: 'true'; } interface FileMultipleProps { name: string; type: 'file'; multiple: true; 'aria-invalid'?: 'true'; } interface SelectProps { name: string; multiple: false; 'aria-invalid'?: 'true'; readonly value: string; } interface SelectMultipleProps { name: string; multiple: true; 'aria-invalid'?: 'true'; readonly value: string[]; } interface TextProps { name: string; 'aria-invalid'?: 'true'; readonly value: string; } interface TypedInputProps { name: string; type: T; 'aria-invalid'?: 'true'; readonly value: string; } /** * input element properties based on input type */ export type InputElementProps = T extends 'checkbox' | 'radio' ? CheckboxRadioProps : T extends 'file' ? FileProps : T extends 'file multiple' ? FileMultipleProps : T extends 'select' ? SelectProps : T extends 'select multiple' ? SelectMultipleProps : T extends 'text' ? TextProps : TypedInputProps; // #endregion // #region field types /** valid leaf value types for form fields */ export type FormFieldValue = string | string[] | number | boolean | File | File[]; /** guard to prevent infinite recursion when T is unknown or has an index signature */ type WillRecurseIndefinitely = unknown extends T ? true : string extends keyof T ? true : false; /** base methods available on all form fields */ interface FieldMethods { /** get the current value */ value(): T | undefined; /** set the value */ set(value: T): T; /** get validation issues for this field */ issues(): FormIssue[] | undefined; } /** * leaf field type for primitive values with `.as()` method */ export type FormFieldLeaf = FieldMethods & { /** * get props for binding to an input element. * returns an object with `name`, `aria-invalid`, and type-specific props. * * @example * ```ts * * * * * ``` */ as>(type: K, ...value: AsValueArgs): InputElementProps; }; /** * container field type for objects/arrays with `.allIssues()` method */ export type FormFieldContainer = FieldMethods & { /** get all issues for this field and descendants */ allIssues(): FormIssue[] | undefined; }; /** * fallback field type when recursion would be infinite */ type UnknownField = FieldMethods & { /** get all issues for this field and descendants */ allIssues(): FormIssue[] | undefined; /** get props for an input element */ as>( type: K, ...value: AsValueArgs ): InputElementProps; } & { [key: string | number]: UnknownField; }; // by breaking this out into its own type, we avoid the TS recursion depth limit type RecursiveFormFields = FormFieldContainer & { [key: string | number]: UnknownField; }; /** * recursive type to build form fields structure with proxy access. * preserves type information through the object hierarchy. */ export type FormFields = WillRecurseIndefinitely extends true ? RecursiveFormFields : NonNullable extends string | number | boolean | File ? FormFieldLeaf> : [T] extends [string[] | File[]] ? FormFieldLeaf & { [K in number]: FormFieldLeaf } : [T] extends [Array] ? FormFieldContainer & { [K in number]: FormFields } : FormFieldContainer & { [K in keyof T]-?: FormFields }; // #endregion