@tolki/types 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/index.d.ts +1 -1
- package/src/path-resolve.d.ts +263 -0
- package/src/path-type.d.ts +0 -74
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolki/types",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Utility TypeScript types for @tolki packages and Laravel HTTP responses.",
|
|
5
5
|
"author": "Abraham Arango <https://github.com/abetwothree>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"./pagination": {
|
|
49
49
|
"types": "./src/pagination.d.ts"
|
|
50
50
|
},
|
|
51
|
-
"./path-
|
|
52
|
-
"types": "./src/path-
|
|
51
|
+
"./path-resolve": {
|
|
52
|
+
"types": "./src/path-resolve.d.ts"
|
|
53
53
|
},
|
|
54
54
|
"./responses": {
|
|
55
55
|
"types": "./src/responses.d.ts"
|
package/src/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type * from "./data";
|
|
|
4
4
|
export type * from "./models";
|
|
5
5
|
export type * from "./objects";
|
|
6
6
|
export type * from "./pagination";
|
|
7
|
-
export type * from "./path-
|
|
7
|
+
export type * from "./path-resolve";
|
|
8
8
|
export type * from "./resource-responses";
|
|
9
9
|
export type * from "./strings";
|
|
10
10
|
export type * from "./utils";
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A key used to access a path in an object or array.
|
|
3
|
+
*/
|
|
4
|
+
export type PathKey = number | string | null | undefined;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A set of keys used to access a path in an object or array.
|
|
8
|
+
*/
|
|
9
|
+
export type PathKeys = number | string | null | undefined | Array<PathKey>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Unwraps a value or function that returns a value.
|
|
13
|
+
*/
|
|
14
|
+
export type UnwrapFn<T> = T extends (...args: unknown[]) => infer R ? R : T;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Path resolution types for resolving values at dot-delimited paths
|
|
18
|
+
* in arrays and objects. Inspired by Lodash's GetFieldType but tailored
|
|
19
|
+
* for array-first contexts (like Laravel's Arr helpers).
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the type of a value at a single key in a type.
|
|
24
|
+
*
|
|
25
|
+
* For arrays: numeric keys resolve to the element type.
|
|
26
|
+
* For objects: string keys resolve to the property type.
|
|
27
|
+
* For tuples: specific numeric keys resolve to the element at that position.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* type A = ResolveKey<string[], 0>; // string
|
|
32
|
+
* type B = ResolveKey<{ name: string; age: number }, "name">; // string
|
|
33
|
+
* type C = ResolveKey<[string, number], 1>; // number
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export type ResolveKey<T, K> =
|
|
37
|
+
// If K is directly a key of T (handles objects and tuple numeric indices)
|
|
38
|
+
K extends keyof T
|
|
39
|
+
? T[K]
|
|
40
|
+
: // If K is a number and T is an array, resolve element type
|
|
41
|
+
T extends readonly (infer U)[]
|
|
42
|
+
? K extends number
|
|
43
|
+
? U
|
|
44
|
+
: // If K is a numeric string like "0", resolve element type
|
|
45
|
+
K extends `${number}`
|
|
46
|
+
? U
|
|
47
|
+
: undefined
|
|
48
|
+
: // If K is a numeric string and T is an object with that as key
|
|
49
|
+
K extends `${infer N extends number}`
|
|
50
|
+
? N extends keyof T
|
|
51
|
+
? T[N]
|
|
52
|
+
: undefined
|
|
53
|
+
: // If K is a number and the stringified version is a key
|
|
54
|
+
K extends number
|
|
55
|
+
? `${K}` extends keyof T
|
|
56
|
+
? T[`${K}`]
|
|
57
|
+
: undefined
|
|
58
|
+
: undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resolves the type at a dot-delimited path within a type.
|
|
62
|
+
* Recursively walks through the path segments.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* type A = ResolvePath<{ user: { name: string } }, "user.name">; // string
|
|
67
|
+
* type B = ResolvePath<{ items: number[] }, "items.0">; // number
|
|
68
|
+
* type C = ResolvePath<[{ name: string }], "0.name">; // string
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export type ResolvePath<T, P> =
|
|
72
|
+
// If P is a string with dots, split and recurse
|
|
73
|
+
P extends `${infer First}.${infer Rest}`
|
|
74
|
+
? ResolveKey<T, First> extends infer Resolved
|
|
75
|
+
? Resolved extends undefined
|
|
76
|
+
? undefined
|
|
77
|
+
: ResolvePath<NonNullable<Resolved>, Rest>
|
|
78
|
+
: undefined
|
|
79
|
+
: // No dots: just resolve a single key
|
|
80
|
+
ResolveKey<T, P>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Resolves the type at a path within an array context.
|
|
84
|
+
* This is the main type for Arr helper functions that operate on arrays
|
|
85
|
+
* and use dot-notation paths to access nested values.
|
|
86
|
+
*
|
|
87
|
+
* When the path is a non-literal `string` or `number`, falls back to TDefault
|
|
88
|
+
* since the path cannot be statically resolved.
|
|
89
|
+
*
|
|
90
|
+
* When the path is `null` or `undefined`, returns the entire array.
|
|
91
|
+
*
|
|
92
|
+
* @template TArray - The array type to resolve from (e.g., `string[]`, `{name: string}[]`)
|
|
93
|
+
* @template TPath - The path to resolve (e.g., `0`, `"0.name"`, `"0.items.1"`)
|
|
94
|
+
* @template TDefault - The fallback type when the path cannot be resolved (defaults to `unknown`)
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* type A = ArrayResolvePath<string[], 0>; // string
|
|
99
|
+
* type B = ArrayResolvePath<{name: string}[], "0.name">; // string
|
|
100
|
+
* type C = ArrayResolvePath<{users: {name: string}[]}[], "0.users.0.name">; // string
|
|
101
|
+
* type D = ArrayResolvePath<string[], null>; // string[]
|
|
102
|
+
* type E = ArrayResolvePath<string[], string>; // unknown (non-literal)
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export type ArrayResolvePath<
|
|
106
|
+
TArray extends readonly unknown[],
|
|
107
|
+
TPath,
|
|
108
|
+
TDefault = unknown,
|
|
109
|
+
> =
|
|
110
|
+
// null/undefined key → return the entire array
|
|
111
|
+
TPath extends null | undefined
|
|
112
|
+
? TArray
|
|
113
|
+
: // Literal number → resolve element type
|
|
114
|
+
number extends TPath
|
|
115
|
+
? TDefault
|
|
116
|
+
: // Literal string check — non-literal strings should fallback
|
|
117
|
+
TPath extends string
|
|
118
|
+
? string extends TPath
|
|
119
|
+
? TDefault
|
|
120
|
+
: ResolvePath<TArray, TPath> extends infer R
|
|
121
|
+
? [R] extends [undefined]
|
|
122
|
+
? TDefault
|
|
123
|
+
: R
|
|
124
|
+
: TDefault
|
|
125
|
+
: TPath extends number
|
|
126
|
+
? ResolvePath<TArray, `${TPath}`> extends infer R
|
|
127
|
+
? [R] extends [undefined]
|
|
128
|
+
? TDefault
|
|
129
|
+
: R
|
|
130
|
+
: TDefault
|
|
131
|
+
: TDefault;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Ensures a resolved path type is an array type.
|
|
135
|
+
* If the resolved type is already an array, returns it as-is.
|
|
136
|
+
* Otherwise falls back to `unknown[]`.
|
|
137
|
+
*
|
|
138
|
+
* Handles the `never` case (e.g., from empty arrays) by falling back
|
|
139
|
+
* to `unknown[]` instead of propagating `never`.
|
|
140
|
+
*
|
|
141
|
+
* Useful for functions like `arrayItem` that validate the resolved
|
|
142
|
+
* value is an array at runtime and throw if not.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* type A = EnsureArray<string[]>; // string[]
|
|
147
|
+
* type B = EnsureArray<number>; // unknown[]
|
|
148
|
+
* type C = EnsureArray<unknown>; // unknown[]
|
|
149
|
+
* type D = EnsureArray<never>; // unknown[]
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export type EnsureArray<T> = [T] extends [never]
|
|
153
|
+
? unknown[]
|
|
154
|
+
: T extends readonly unknown[]
|
|
155
|
+
? T
|
|
156
|
+
: unknown[];
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Checks whether a type is a literal string or number, rather than a
|
|
160
|
+
* widened `string` or `number` type.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* type A = IsLiteral<"hello">; // true
|
|
165
|
+
* type B = IsLiteral<42>; // true
|
|
166
|
+
* type C = IsLiteral<string>; // false
|
|
167
|
+
* type D = IsLiteral<number>; // false
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export type IsLiteral<T> = string extends T
|
|
171
|
+
? false
|
|
172
|
+
: number extends T
|
|
173
|
+
? false
|
|
174
|
+
: T extends string | number
|
|
175
|
+
? true
|
|
176
|
+
: false;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Resolves a path within an array and adds `| null` only when the path
|
|
180
|
+
* is non-literal (widened `string` or `number`), meaning it cannot be
|
|
181
|
+
* statically verified at compile time.
|
|
182
|
+
*
|
|
183
|
+
* When the path is a literal (e.g., `0`, `"0.name"`), returns just the
|
|
184
|
+
* resolved type without `| null`, consistent with TypeScript's array
|
|
185
|
+
* access convention where `arr[0]` returns the element type without
|
|
186
|
+
* `| undefined`.
|
|
187
|
+
*
|
|
188
|
+
* When the path is non-literal (`string` or `number`), `| null` is added
|
|
189
|
+
* since the path cannot be verified at compile time.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* Literal path → resolved type, no null
|
|
194
|
+
* type A = ArrayResolvePathOrNull<{name: string}[], "0.name">; // string
|
|
195
|
+
*
|
|
196
|
+
* Non-literal path → element type | null
|
|
197
|
+
* type B = ArrayResolvePathOrNull<{name: string}[], string>; // {name: string} | null
|
|
198
|
+
*
|
|
199
|
+
* Literal index on simple array → element type, no null
|
|
200
|
+
* type C = ArrayResolvePathOrNull<string[], 0>; // string
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export type ArrayResolvePathOrNull<TArray extends readonly unknown[], TPath> =
|
|
204
|
+
IsLiteral<TPath> extends true
|
|
205
|
+
? ArrayResolvePath<TArray, TPath, TArray[number]>
|
|
206
|
+
: ArrayResolvePath<TArray, TPath, TArray[number]> | null;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Resolves a path within an array and adds `| TDefault` only when the path
|
|
210
|
+
* is non-literal (widened `string` or `number`), meaning it cannot be
|
|
211
|
+
* statically verified at compile time.
|
|
212
|
+
*
|
|
213
|
+
* When the path is a literal (e.g., `0`, `"0.name"`), returns just the
|
|
214
|
+
* resolved type without `| TDefault`, since the path is trusted at compile
|
|
215
|
+
* time — consistent with {@link ArrayResolvePathOrNull}.
|
|
216
|
+
*
|
|
217
|
+
* When the path is non-literal, `| TDefault` is added since the path
|
|
218
|
+
* cannot be verified and the default value may be returned at runtime.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* // Literal path → resolved type, no default in union
|
|
223
|
+
* type A = ArrayResolvePathOrDefault<string[], 1, number>; // string
|
|
224
|
+
*
|
|
225
|
+
* // Non-literal path → element type | default
|
|
226
|
+
* type B = ArrayResolvePathOrDefault<string[], number, number>; // string | number
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
export type ArrayResolvePathOrDefault<
|
|
230
|
+
TArray extends readonly unknown[],
|
|
231
|
+
TPath,
|
|
232
|
+
TDefault,
|
|
233
|
+
> =
|
|
234
|
+
IsLiteral<TPath> extends true
|
|
235
|
+
? ArrayResolvePath<TArray, TPath, TDefault>
|
|
236
|
+
: ArrayResolvePath<TArray, TPath, TArray[number]> | TDefault;
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Gets the type of a value at a given path in an object or array.
|
|
240
|
+
* Supports dot notation (a.b.c) and array indexing (a.0.b).
|
|
241
|
+
*
|
|
242
|
+
* Delegates to {@link ResolvePath} for the actual path resolution
|
|
243
|
+
* logic and adds TDefault fallback handling.
|
|
244
|
+
*/
|
|
245
|
+
export type GetFieldType<
|
|
246
|
+
T,
|
|
247
|
+
P extends string | number,
|
|
248
|
+
TDefault = never,
|
|
249
|
+
> = P extends keyof T
|
|
250
|
+
? T[P]
|
|
251
|
+
: P extends string
|
|
252
|
+
? ResolvePath<T, P> extends infer R
|
|
253
|
+
? [R] extends [undefined]
|
|
254
|
+
? TDefault
|
|
255
|
+
: R
|
|
256
|
+
: TDefault
|
|
257
|
+
: P extends number
|
|
258
|
+
? ResolveKey<T, P> extends infer R
|
|
259
|
+
? [R] extends [undefined]
|
|
260
|
+
? TDefault
|
|
261
|
+
: R
|
|
262
|
+
: TDefault
|
|
263
|
+
: TDefault;
|
package/src/path-type.d.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A key used to access a path in an object or array.
|
|
3
|
-
*/
|
|
4
|
-
export type PathKey = number | string | null | undefined;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A set of keys used to access a path in an object or array.
|
|
8
|
-
*/
|
|
9
|
-
export type PathKeys = number | string | null | undefined | Array<PathKey>;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Unwraps a value or function that returns a value.
|
|
13
|
-
*/
|
|
14
|
-
export type UnwrapFn<T> = T extends (...args: unknown[]) => infer R ? R : T;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Helper to get array element type
|
|
18
|
-
*/
|
|
19
|
-
type ArrayElement<T> = T extends readonly (infer U)[] ? U : never;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Helper to check if a type is an array
|
|
23
|
-
*/
|
|
24
|
-
type IsArray<T> = T extends readonly unknown[] ? true : false;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Helper to check if a type is a tuple with at least one element
|
|
28
|
-
*/
|
|
29
|
-
type IsTuple<T> = T extends readonly [unknown, ...unknown[]]
|
|
30
|
-
? true
|
|
31
|
-
: T extends readonly []
|
|
32
|
-
? true
|
|
33
|
-
: false;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Helper to get element at specific index in tuple
|
|
37
|
-
* For unions of tuples, returns the union of elements at that index
|
|
38
|
-
*/
|
|
39
|
-
type TupleElement<
|
|
40
|
-
T extends readonly unknown[],
|
|
41
|
-
N extends number,
|
|
42
|
-
> = T extends readonly unknown[]
|
|
43
|
-
? N extends keyof T
|
|
44
|
-
? T[N]
|
|
45
|
-
: ArrayElement<T>
|
|
46
|
-
: never;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Gets the type of a value at a given path in an object or array.
|
|
50
|
-
* Supports dot notation (a.b.c) and array indexing (a.0.b).
|
|
51
|
-
*/
|
|
52
|
-
export type GetFieldType<
|
|
53
|
-
T,
|
|
54
|
-
P extends string | number,
|
|
55
|
-
TDefault = never,
|
|
56
|
-
> = P extends keyof T
|
|
57
|
-
? T[P]
|
|
58
|
-
: P extends `${infer First}.${infer Rest}`
|
|
59
|
-
? First extends keyof T
|
|
60
|
-
? GetFieldType<T[First], Rest, TDefault>
|
|
61
|
-
: First extends `${infer N extends number}`
|
|
62
|
-
? IsArray<T> extends true
|
|
63
|
-
? T extends readonly unknown[]
|
|
64
|
-
? GetFieldType<T[N], Rest, TDefault>
|
|
65
|
-
: TDefault
|
|
66
|
-
: TDefault
|
|
67
|
-
: TDefault
|
|
68
|
-
: P extends `${infer N extends number}`
|
|
69
|
-
? IsArray<T> extends true
|
|
70
|
-
? T extends readonly unknown[]
|
|
71
|
-
? T[N]
|
|
72
|
-
: TDefault
|
|
73
|
-
: TDefault
|
|
74
|
-
: TDefault;
|