|
8 | 8 | * @typedef {Recursive<T>[]} RecursiveArray |
9 | 9 | */ |
10 | 10 |
|
| 11 | +/** |
| 12 | + * @template Value |
| 13 | + * @template Fallback |
| 14 | + * @typedef {undefined extends Value ? Fallback : Value} Get |
| 15 | + */ |
| 16 | + |
11 | 17 | /** |
12 | 18 | * @typedef {{ |
13 | 19 | * context?: any; |
14 | | - * extensions?: { [K: string]: any }; |
| 20 | + * extensions?: {}; |
15 | 21 | * input?: any; |
16 | 22 | * object?: any; |
17 | 23 | * resolvedValue?: {}; |
18 | | - * typeName?: string | unknown; |
| 24 | + * typeName?: string; |
19 | 25 | * value?: any; |
20 | 26 | * }} TypeOptions |
21 | 27 | */ |
22 | 28 |
|
23 | | -/** |
24 | | - * @typedef {{ |
25 | | - * context: unknown; |
26 | | - * extensions: {}; |
27 | | - * input: unknown; |
28 | | - * object: unknown; |
29 | | - * resolvedValue: {}; |
30 | | - * typeName: unknown; |
31 | | - * value: unknown; |
32 | | - * }} DefaultTypeOptions |
33 | | - */ |
34 | | - |
35 | | -/** |
36 | | - * @template {TypeOptions} Obj |
37 | | - * @template {keyof TypeOptions} Key |
38 | | - * @typedef {undefined extends Obj[Key] ? DefaultTypeOptions[Key] : Obj[Key]} GetTypeOption |
39 | | - */ |
40 | | - |
41 | 29 | /** |
42 | 30 | * @template {TypeOptions} O |
43 | | - * @typedef {Pick<O, 'context' | 'extensions' | 'typeName'>} SharedTypeOptions |
| 31 | + * @typedef {Type<Pick<O, 'context' | 'extensions' | 'typeName'>>} SubType |
44 | 32 | */ |
45 | 33 |
|
46 | 34 | /** |
47 | | - * @template {TypeOptions} [O=DefaultTypeOptions] Default is |
48 | | - * `DefaultTypeOptions` |
| 35 | + * @template {TypeOptions} [O={}] Default is `{}` |
49 | 36 | * @typedef {Recursive< |
50 | | - * | GetTypeOption<O, 'typeName'> |
| 37 | + * | Get<O['typeName'], never> |
51 | 38 | * | (( |
52 | | - * | { optional: Type<SharedTypeOptions<O>> } |
53 | | - * | { nullable: Type<SharedTypeOptions<O>> } |
| 39 | + * | { optional: SubType<O> } |
| 40 | + * | { nullable: SubType<O> } |
| 41 | + * | { arrayOf: SubType<O>; minLength?: number; maxLength?: number } |
54 | 42 | * | { |
55 | | - * arrayOf: Type<SharedTypeOptions<O>>; |
56 | | - * minLength?: number; |
57 | | - * maxLength?: number; |
58 | | - * } |
59 | | - * | { |
60 | | - * oneOf: { [K: string]: Type<SharedTypeOptions<O>> }; |
| 43 | + * oneOf: { [K: string]: SubType<O> }; |
61 | 44 | * resolveType: (value: {}) => string; |
62 | 45 | * } |
| 46 | + * | { object: { [K: string]: SubType<O> }; defaultType?: SubType<O> } |
63 | 47 | * | { |
64 | | - * object: { [K: string]: Type<SharedTypeOptions<O>> }; |
65 | | - * defaultType?: Type<SharedTypeOptions<O>>; |
66 | | - * } |
67 | | - * | { |
68 | | - * input?: Type<SharedTypeOptions<O>>; |
69 | | - * type?: Type<SharedTypeOptions<O>>; |
| 48 | + * input?: SubType<O>; |
| 49 | + * type?: SubType<O>; |
70 | 50 | * typeInput?: any; |
71 | 51 | * resolve?: |
72 | 52 | * | ((options: { |
73 | | - * context: GetTypeOption<O, 'context'>; |
74 | | - * input: GetTypeOption<O, 'input'>; |
75 | | - * object: GetTypeOption<O, 'object'>; |
| 53 | + * context: Get<O['context'], any>; |
| 54 | + * input: Get<O['input'], any>; |
| 55 | + * object: Get<O['object'], any>; |
76 | 56 | * path: string[]; |
77 | 57 | * query: Query; |
78 | | - * schema: Schema<SharedTypeOptions<O>>; |
79 | | - * type: Type<SharedTypeOptions<O>>; |
80 | | - * value: GetTypeOption<O, 'typeName'>; |
| 58 | + * schema: Schema<O>; |
| 59 | + * type: SubType<O>; |
| 60 | + * value: Get<O['value'], any>; |
81 | 61 | * }) => any) |
82 | 62 | * | {} |
83 | 63 | * | null; |
|
86 | 66 | * cost?: |
87 | 67 | * | number |
88 | 68 | * | ((options: { |
89 | | - * context: GetTypeOption<O, 'context'>; |
| 69 | + * context: Get<O['context'], any>; |
90 | 70 | * cost: number; |
91 | | - * input: GetTypeOption<O, 'input'>; |
92 | | - * object: GetTypeOption<O, 'object'>; |
| 71 | + * input: Get<O['input'], any>; |
| 72 | + * object: Get<O['object'], any>; |
93 | 73 | * path: string[]; |
94 | 74 | * query: Query; |
95 | | - * schema: Schema<SharedTypeOptions<O>>; |
96 | | - * type: Type<SharedTypeOptions<O>>; |
97 | | - * value: GetTypeOption<O, 'typeName'>; |
| 75 | + * schema: Schema<O>; |
| 76 | + * type: SubType<O>; |
| 77 | + * value: Get<O['value'], any>; |
98 | 78 | * }) => number); |
99 | 79 | * defaultValue?: any; |
100 | 80 | * validate?: (options: { |
101 | | - * context: GetTypeOption<O, 'context'>; |
102 | | - * input: GetTypeOption<O, 'input'>; |
103 | | - * object: GetTypeOption<O, 'object'>; |
| 81 | + * context: Get<O['context'], any>; |
| 82 | + * input: Get<O['input'], any>; |
| 83 | + * object: Get<O['object'], any>; |
104 | 84 | * path: string[]; |
105 | 85 | * query: Query; |
106 | | - * schema: Schema<SharedTypeOptions<O>>; |
107 | | - * type: Type<SharedTypeOptions<O>>; |
108 | | - * value: GetTypeOption<O, 'resolvedValue'>; |
| 86 | + * schema: Schema<O>; |
| 87 | + * type: SubType<O>; |
| 88 | + * value: Get<O['resolvedValue'], any>; |
109 | 89 | * }) => any; |
110 | | - * } & GetTypeOption<O, 'extensions'>) |
| 90 | + * } & Get<O['extensions'], {}>) |
111 | 91 | * >} Type |
112 | 92 | */ |
113 | 93 |
|
114 | 94 | /** |
115 | | - * @template {TypeOptions} [O=DefaultTypeOptions] Default is |
116 | | - * `DefaultTypeOptions` |
117 | | - * @typedef {GetTypeOption<O, 'typeName'> extends string |
118 | | - * ? { [K in GetTypeOption<O, 'typeName'>]: Type<SharedTypeOptions<O>> } |
119 | | - * : { [K in keyof any]: never }} Schema |
| 95 | + * @template {TypeOptions} [O={}] Default is `{}` |
| 96 | + * @typedef {O['typeName'] extends string |
| 97 | + * ? { [K in O['typeName']]: SubType<O> } |
| 98 | + * : { [K in keyof any]: never }} Schema |
120 | 99 | */ |
121 | 100 |
|
122 | 101 | /** |
|
0 commit comments