svelte-hook-form
Version:
A better version of form validation.
66 lines (57 loc) • 1.57 kB
text/typescript
export type Primitive =
| null
| undefined
| string
| number
| boolean
| symbol
| bigint
| Date;
export type IsArray<T> = T extends ReadonlyArray<any> ? true : false;
/**
* Type which given a tuple type returns its own keys, i.e. only its indices.
* @typeParam T - tuple type
* @example
* ```
* TupleKeys<[number, string]> = '0' | '1'
* ```
*/
export type TupleKeys<T extends ReadonlyArray<any>> = Exclude<keyof T, keyof any[]>;
/**
* Type to query whether an array type T is a tuple type.
* @typeParam T - type which may be an array or tuple
* @example
* ```
* IsTuple<[number]> = true
* IsTuple<number[]> = false
* ```
*/
export type IsTuple<T extends ReadonlyArray<any>> = number extends T["length"]
? false
: true;
/**
* Helper type for recursively constructing paths through a type.
* See {@link Path}
*/
type PathImpl<K extends string | number, V> = V extends Primitive
? `${K}`
: V extends ReadonlyArray<infer KV>
? `${K}` | `${K}${ArrayPathImpl<KV>}`
: `${K}` | `${K}.${Path<V>}`;
type ArrayPathImpl<V extends any> = V extends Primitive
? `[${number}]`
: `[${number}]` | `[${number}].${Path<V>}`;
export type Path<T> = T extends ReadonlyArray<infer V>
? IsTuple<T> extends true
? {
[K in TupleKeys<T>]-?: PathImpl<K & string, T[K]>;
}[TupleKeys<T>]
: ArrayPathImpl<V>
: {
[K in keyof T]-?: PathImpl<K & string, T[K]>;
}[keyof T];
export type FieldValues<T = any> = Record<string, T>;
/**
* See {@link Path}
*/
export type FieldPath<T extends FieldValues> = Path<T>;