@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
34 lines (33 loc) • 780 B
TypeScript
export default function assertOptions<Options extends Record<string, any>, Path extends Paths<Options> = Paths<Options>>(options: Options, toCheck: Path[], friendlyMessage?: string): Options & {
[P in Path]: NonNullable<Options[P]>;
};
type Join<K, P> = K extends string | number ? P extends string | number ? `${K}${'' extends P ? '' : '.'}${P}` : never : never;
type Prev = [
never,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
...0[]
];
type Paths<T, D extends number = 3> = [D] extends [never] ? never : T extends object ? {
[K in keyof T]-?: K extends string | number ? `${K}` | Join<K, Paths<T[K], Prev[D]>> : never;
}[keyof T] : '';
export {};