UNPKG

4.48 kBTypeScriptView Raw
1import type { Keys, Keys1, Keys2, Keys3, Keys4, Keys5, Keys6, Keys7, ValN } from "./keyval.js";
2import type { Maybe } from "./null.js";
3import type { NumOrString } from "./prim.js";
4import type { Head, IsEmpty, Tail } from "./tuple.js";
5/**
6 * Unchecked lookup path for nested data structures.
7 */
8export type Path = readonly NumOrString[] | NumOrString;
9/**
10 * Empty lookup path.
11 */
12export type Path0 = readonly [];
13/**
14 * Type checked lookup path (depth 1)
15 */
16export type Path1<T, A> = A extends Keys<T> ? readonly [A] : never;
17/**
18 * Type checked lookup path (depth 2)
19 */
20export type Path2<T, A, B> = A extends Keys<T> ? B extends Keys1<T, A> ? readonly [A, B] : never : never;
21/**
22 * Type checked lookup path (depth 3)
23 */
24export type Path3<T, A, B, C> = A extends Keys<T> ? B extends Keys1<T, A> ? C extends Keys2<T, A, B> ? readonly [A, B, C] : never : never : never;
25/**
26 * Type checked lookup path (depth 4)
27 */
28export type Path4<T, A, B, C, D> = A extends Keys<T> ? B extends Keys1<T, A> ? C extends Keys2<T, A, B> ? D extends Keys3<T, A, B, C> ? readonly [A, B, C, D] : never : never : never : never;
29/**
30 * Type checked lookup path (depth 5)
31 */
32export type Path5<T, A, B, C, D, E> = A extends Keys<T> ? B extends Keys1<T, A> ? C extends Keys2<T, A, B> ? D extends Keys3<T, A, B, C> ? E extends Keys4<T, A, B, C, D> ? readonly [A, B, C, D, E] : never : never : never : never : never;
33/**
34 * Type checked lookup path (depth 6)
35 */
36export type Path6<T, A, B, C, D, E, F> = A extends Keys<T> ? B extends Keys1<T, A> ? C extends Keys2<T, A, B> ? D extends Keys3<T, A, B, C> ? E extends Keys4<T, A, B, C, D> ? F extends Keys5<T, A, B, C, D, E> ? readonly [A, B, C, D, E, F] : never : never : never : never : never : never;
37/**
38 * Type checked lookup path (depth 7)
39 */
40export type Path7<T, A, B, C, D, E, F, G> = A extends Keys<T> ? B extends Keys1<T, A> ? C extends Keys2<T, A, B> ? D extends Keys3<T, A, B, C> ? E extends Keys4<T, A, B, C, D> ? F extends Keys5<T, A, B, C, D, E> ? G extends Keys6<T, A, B, C, D, E, F> ? readonly [A, B, C, D, E, F, G] : never : never : never : never : never : never : never;
41/**
42 * Type checked lookup path (depth 8)
43 */
44export type Path8<T, A, B, C, D, E, F, G, H> = A extends Keys<T> ? B extends Keys1<T, A> ? C extends Keys2<T, A, B> ? D extends Keys3<T, A, B, C> ? E extends Keys4<T, A, B, C, D> ? F extends Keys5<T, A, B, C, D, E> ? G extends Keys6<T, A, B, C, D, E, F> ? H extends Keys7<T, A, B, C, D, E, F, G> ? readonly [A, B, C, D, E, F, G, H] : never : never : never : never : never : never : never : never;
45/**
46 * Semi-typechecked lookup path (depth > 8). Only the first 8 levels are
47 * checked.
48 */
49export type DeepPath<T, A, B, C, D, E, F, G, H> = A extends Keys<T> ? B extends Keys1<T, A> ? C extends Keys2<T, A, B> ? D extends Keys3<T, A, B, C> ? E extends Keys4<T, A, B, C, D> ? F extends Keys5<T, A, B, C, D, E> ? G extends Keys6<T, A, B, C, D, E, F> ? H extends Keys7<T, A, B, C, D, E, F, G> ? readonly [A, B, C, D, E, F, G, H, ...NumOrString[]] : never : never : never : never : never : never : never : never;
50/**
51 * Returns `RES` if `PRED` is `never`, else `Maybe<RES>`
52 */
53export type OptVal<PRED, RES> = [PRED] extends [never] ? RES : Maybe<RES>;
54/**
55 * Returns true if `T` includes undefined.
56 */
57export type IsOpt<T> = T extends undefined ? true : never;
58/**
59 * Internal recursive helper type for {@link IsOptPath}.
60 */
61type IsOptR<T, K, P extends unknown[]> = K extends Keys<T> ? [true] extends [IsOpt<T[K]>] ? true : {
62 0: IsOptR<Required<T>[K], Head<P>, Tail<P>>;
63 1: never;
64}[IsEmpty<P>] : never;
65/**
66 * Returns true if given path contains any intermediate properties
67 * declared as optional in type `T`.
68 *
69 * Reference:
70 * https://stackoverflow.com/q/60869412/294515
71 */
72export type IsOptPath<T, P extends unknown[]> = P extends [] ? never : IsOptR<T, Head<P>, Tail<P>>;
73/**
74 * Similar to {@link PathVal}, but also takes into account if given path
75 * contains any intermediate properties declared as optional in type
76 * `T`. If that's the case, returns union of `undefined` and inferred
77 * value for path, else just the latter.
78 *
79 * Context & reference: https://stackoverflow.com/q/60869412/294515
80 */
81export type OptPathVal<T, P extends unknown[]> = OptVal<IsOptPath<T, P>, ValN<T, P>>;
82/**
83 * Returns nested value type for given path into `T` or `never` if path
84 * is incompatible with `T`.
85 */
86export type PathVal<T, P extends unknown[]> = ValN<T, P>;
87export {};
88//# sourceMappingURL=path.d.ts.map
\No newline at end of file