Skip to content

Commit

Permalink
feat(api): update Path alias, add doc strings
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 24, 2020
1 parent b51efc6 commit e2b35bc
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions packages/api/src/api/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@ import type {
} from "./keyval";

/**
* Lookup path for nested data structures.
* Unchecked lookup path for nested data structures.
*/
export type Path = PropertyKey | PropertyKey[];
export type Path = readonly NumOrString[] | NumOrString;

/**
* Empty lookup path.
*/
export type Path0 = readonly [];

/**
* Type checked lookup path (depth 1)
*/
export type Path1<T, A> = A extends Keys<T> ? readonly [A] : never;

/**
* Type checked lookup path (depth 2)
*/
export type Path2<T, A, B> = A extends Keys<T>
? B extends Keys1<T, A>
? readonly [A, B]
: never
: never;

/**
* Type checked lookup path (depth 3)
*/
export type Path3<T, A, B, C> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -41,6 +53,9 @@ export type Path3<T, A, B, C> = A extends Keys<T>
: never
: never;

/**
* Type checked lookup path (depth 4)
*/
export type Path4<T, A, B, C, D> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -51,6 +66,9 @@ export type Path4<T, A, B, C, D> = A extends Keys<T>
: never
: never;

/**
* Type checked lookup path (depth 5)
*/
export type Path5<T, A, B, C, D, E> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -63,6 +81,9 @@ export type Path5<T, A, B, C, D, E> = A extends Keys<T>
: never
: never;

/**
* Type checked lookup path (depth 6)
*/
export type Path6<T, A, B, C, D, E, F> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -77,6 +98,9 @@ export type Path6<T, A, B, C, D, E, F> = A extends Keys<T>
: never
: never;

/**
* Type checked lookup path (depth 7)
*/
export 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>
Expand All @@ -93,6 +117,9 @@ export type Path7<T, A, B, C, D, E, F, G> = A extends Keys<T>
: never
: never;

/**
* Type checked lookup path (depth 8)
*/
export 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>
Expand All @@ -111,6 +138,10 @@ export type Path8<T, A, B, C, D, E, F, G, H> = A extends Keys<T>
: never
: never;

/**
* Semi-typechecked lookup path (depth > 8). Only the first 8 levels are
* checked.
*/
// prettier-ignore
export type DeepPath<T, A, B, C, D, E, F, G, H> = A extends Keys<T>
? B extends Keys1<T, A>
Expand All @@ -130,14 +161,23 @@ export type DeepPath<T, A, B, C, D, E, F, G, H> = A extends Keys<T>
: never
: never;

/**
* Value type for lookup path (depth 1)
*/
export type PathVal1<T, A> = A extends Keys<T> ? Val1<T, A> : never;

/**
* Value type for lookup path (depth 2)
*/
export type PathVal2<T, A, B> = A extends Keys<T>
? B extends Keys1<T, A>
? Val2<T, A, B>
: never
: never;

/**
* Value type for lookup path (depth 3)
*/
export type PathVal3<T, A, B, C> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -146,6 +186,9 @@ export type PathVal3<T, A, B, C> = A extends Keys<T>
: never
: never;

/**
* Value type for lookup path (depth 4)
*/
export type PathVal4<T, A, B, C, D> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -156,6 +199,9 @@ export type PathVal4<T, A, B, C, D> = A extends Keys<T>
: never
: never;

/**
* Value type for lookup path (depth 5)
*/
export type PathVal5<T, A, B, C, D, E> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -168,6 +214,9 @@ export type PathVal5<T, A, B, C, D, E> = A extends Keys<T>
: never
: never;

/**
* Value type for lookup path (depth 6)
*/
export type PathVal6<T, A, B, C, D, E, F> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -182,6 +231,9 @@ export type PathVal6<T, A, B, C, D, E, F> = A extends Keys<T>
: never
: never;

/**
* Value type for lookup path (depth 7)
*/
export type PathVal7<T, A, B, C, D, E, F, G> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand All @@ -198,6 +250,9 @@ export type PathVal7<T, A, B, C, D, E, F, G> = A extends Keys<T>
: never
: never;

/**
* Value type for lookup path (depth 8)
*/
export type PathVal8<T, A, B, C, D, E, F, G, H> = A extends Keys<T>
? B extends Keys1<T, A>
? C extends Keys2<T, A, B>
Expand Down

0 comments on commit e2b35bc

Please sign in to comment.