@squidcloud/client
Version:
A typescript implementation of the Squid client
21 lines (20 loc) • 1.3 kB
TypeScript
/** Makes the specified keys in a type optional, leaving the rest unchanged. */
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
type PropertiesWithPrefix<Prefix extends string> = {
[key in `${Prefix}${string}`]: any;
};
type Pred = [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
/**
* Recursively flattens all keys of a type into dot notation paths up to a fixed depth.
* Used to map deeply nested fields with their access paths.
*/
export type DeepRecord<T, RemainingDepth extends number & keyof Pred = 5, Prefix extends string = ''> = RemainingDepth extends 0 ? PropertiesWithPrefix<Prefix> : {
[k in keyof T]-?: k extends string | number ? Record<`${Prefix}${k}`, T[k]> | (Required<T>[k] extends any[] ? never : Required<T>[k] extends object ? DeepRecord<Required<T>[k], Pred[RemainingDepth], `${Prefix}${k}.`> : never) : never;
}[keyof T];
/** Gets the union of keys from a union of object types. */
export type UnionKeys<T> = T extends any ? keyof T : never;
/** Extracts the type of specific field `K` from a union of object types `T`. */
export type FieldOf<T, K extends UnionKeys<T>> = Extract<T, Record<K, any>>[K];
/** All possible deeply nested field paths of a type, joined by dot notation. */
export type Paths<T> = UnionKeys<DeepRecord<T>>;
export {};