UNPKG

9.21 kBTypeScriptView Raw
1import type { Head, Tail } from "./tuple.js";
2/**
3 * A version of `T` in which only keys in K are mandatory and the rest are
4 * considered optional.
5 */
6export type SomeRequired<T, K extends keyof T> = Partial<Omit<T, K>> & Required<Pick<T, K>>;
7/**
8 * Extracts from A all keys which have values assignable to type B.
9 */
10export type TypedKeys<A, B> = {
11 [P in Keys<A>]: B extends A[P] ? P : never;
12}[keyof A];
13export type NumericKeys<T> = TypedKeys<T, number>;
14export type StringKeys<T> = TypedKeys<T, string>;
15export type DeepPartial<T> = Partial<{
16 [k in keyof T]: DeepPartial<T[k]>;
17}>;
18export type Keys<T> = keyof Required<T>;
19export type Keys1<T, A extends Keys<T>> = Keys<Required<T>[A]>;
20export type Keys2<T, A extends Keys<T>, B extends Keys1<T, A>> = Keys1<Required<T>[A], B>;
21export type Keys3<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>> = Keys2<Required<T>[A], B, C>;
22export type Keys4<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>, D extends Keys3<T, A, B, C>> = Keys3<Required<T>[A], B, C, D>;
23export type Keys5<T, 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>> = Keys4<Required<T>[A], B, C, D, E>;
24export type Keys6<T, 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>> = Keys5<Required<T>[A], B, C, D, E, F>;
25export type Keys7<T, 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>> = Keys6<Required<T>[A], B, C, D, E, F, G>;
26export type Keys8<T, 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>> = Keys7<Required<T>[A], B, C, D, E, F, G, H>;
27/**
28 * Internal type used as a reducer for the KeyN type.
29 *
30 * @internal
31 *
32 * @param T - structure to validate the key against.
33 * @param L - Current value.
34 * @param R - Remaining values.
35 */
36type KeysNReducer<T, L, R extends unknown[]> = L extends keyof T ? {
37 0: keyof Required<T>[L];
38 1: KeysNReducer<Required<T>[L], Head<R>, Tail<R>>;
39}[R extends [] ? 0 : 1] : never;
40/**
41 * Generalised version of Keys0 - Keys7.
42 */
43export type KeysN<T, L extends unknown[]> = L extends [] ? Keys<T> : KeysNReducer<T, Head<L>, Tail<L>>;
44/**
45 * Type union of values of all keys in T.
46 */
47export type Values<T> = Val1<T, Keys<T>>;
48export type Val1<T, A extends Keys<T>> = T[A];
49export type Val2<T, A extends Keys<T>, B extends Keys1<T, A>> = ValN<T, [A, B]>;
50export type Val3<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>> = ValN<T, [A, B, C]>;
51export type Val4<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>, D extends Keys3<T, A, B, C>> = ValN<T, [A, B, C, D]>;
52export type Val5<T, 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>> = ValN<T, [A, B, C, D, E]>;
53export type Val6<T, 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>> = ValN<T, [A, B, C, D, E, F]>;
54export type Val7<T, 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>> = ValN<T, [A, B, C, D, E, F, G]>;
55export type Val8<T, 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>> = ValN<T, [A, B, C, D, E, F, G, H]>;
56/**
57 * Internal reducer for ValN.
58 *
59 * @internal
60 *
61 * @param T - he structure to get the values from.
62 * @param C - he current key.
63 * @param R - he remaining keys
64 */
65type ValNReducer<T, C, R extends unknown[]> = C extends keyof T ? {
66 0: T[C];
67 1: ValNReducer<Required<T>[C], Head<R>, Tail<R>>;
68}[R extends [] ? 0 : 1] : never;
69/**
70 * Generalised version of Val1-Val7
71 */
72export type ValN<T, L extends unknown[]> = L extends [] ? T : ValNReducer<T, Head<L>, Tail<L>>;
73/**
74 * Utilities for constructing types with nested keys removed.
75 */
76export type Without<T, A extends Keys<T>> = Omit<T, A>;
77export type Without2<T, A extends Keys<T>, B extends Keys1<T, A>> = Without<T, A> & {
78 [id in A]: Without<Val1<T, A>, B>;
79};
80export type Without3<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>> = Without<T, A> & {
81 [id in A]: Without2<Val1<T, A>, B, C>;
82};
83export type Without4<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>, D extends Keys3<T, A, B, C>> = Without<T, A> & {
84 [id in A]: Without3<Val1<T, A>, B, C, D>;
85};
86export type Without5<T, 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>> = Without<T, A> & {
87 [id in A]: Without4<Val1<T, A>, B, C, D, E>;
88};
89export type Without6<T, 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>> = Without<T, A> & {
90 [id in A]: Without5<Val1<T, A>, B, C, D, E, F>;
91};
92export type Without7<T, 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>> = Without<T, A> & {
93 [id in A]: Without6<Val1<T, A>, B, C, D, E, F, G>;
94};
95export type Without8<T, 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>> = Without<T, A> & {
96 [id in A]: Without7<Val1<T, A>, B, C, D, E, F, G, H>;
97};
98/**
99 * Internal reducer used as a building block for WithoutN.
100 *
101 * @internal
102 *
103 * @param T - he structure to remove keys from.
104 * @param C - he current key.
105 * @param R - he remaining keys.
106 */
107type WithoutNReducer<T, C, R extends unknown[]> = C extends keyof T ? {
108 0: Without<T, C>;
109 1: Without<T, C> & Record<C, WithoutNReducer<T[C], Head<R>, Tail<R>>>;
110}[R extends [] ? 0 : 1] : never;
111/**
112 * Generalised version of Without0-Without8.
113 */
114export type WithoutN<T, P extends unknown[]> = WithoutNReducer<T, Head<P>, Tail<P>>;
115/**
116 * Utilities for replacing types of nested keys.
117 */
118export type Replace<T, A extends Keys<T>, V> = Without<T, A> & {
119 [id in A]: V;
120};
121export type Replace2<T, A extends Keys<T>, B extends Keys1<T, A>, V> = Without<T, A> & {
122 [id in A]: Replace<Val1<T, A>, B, V>;
123};
124export type Replace3<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>, V> = Without<T, A> & {
125 [id in A]: Replace2<Val1<T, A>, B, C, V>;
126};
127export type Replace4<T, A extends Keys<T>, B extends Keys1<T, A>, C extends Keys2<T, A, B>, D extends Keys3<T, A, B, C>, V> = Without<T, A> & {
128 [id in A]: Replace3<Val1<T, A>, B, C, D, V>;
129};
130export type Replace5<T, 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>, V> = Without<T, A> & {
131 [id in A]: Replace4<Val1<T, A>, B, C, D, E, V>;
132};
133export type Replace6<T, 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>, V> = Without<T, A> & {
134 [id in A]: Replace5<Val1<T, A>, B, C, D, E, F, V>;
135};
136export type Replace7<T, 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>, V> = Without<T, A> & {
137 [id in A]: Replace6<Val1<T, A>, B, C, D, E, F, G, V>;
138};
139export type Replace8<T, 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>, V> = Without<T, A> & {
140 [id in A]: Replace7<Val1<T, A>, B, C, D, E, F, G, H, V>;
141};
142/**
143 * Internal reducer used as a building block for ReduceN.
144 *
145 * @internal
146 *
147 * @param T - he structure to remove keys from.
148 * @param C - he current key.
149 * @param R - he remaining keys.
150 * @param V - he type to use for the replacement.
151 */
152type ReplaceNReducer<T, C, R extends unknown[], V> = C extends keyof T ? {
153 0: Replace<T, C, V>;
154 1: Without<T, C> & Record<C, ReplaceNReducer<T[C], Head<R>, Tail<R>, V>>;
155}[R extends [] ? 0 : 1] : never;
156/**
157 * Generalised version of Replace0-Replace8.
158 */
159export type ReplaceN<T, P extends unknown[], V> = ReplaceNReducer<T, Head<P>, Tail<P>, V>;
160export {};
161//# sourceMappingURL=keyval.d.ts.map
\No newline at end of file