@tolki/types 1.0.1 → 1.1.0
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/README.md +549 -0
- package/package.json +13 -4
- package/src/enums.d.ts +131 -0
- package/src/index.d.ts +2 -1
- package/src/path-resolve.d.ts +263 -0
- package/src/path-type.d.ts +0 -74
package/README.md
CHANGED
|
@@ -5,3 +5,552 @@ Utility TypeScript types for @tolki packages and Laravel HTTP responses.
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
7
7
|
The full documentation for the TypeScript type utilities can be found at [https://tolki.abe.dev](https://tolki.abe.dev/typescript/).
|
|
8
|
+
|
|
9
|
+
<!-- AUTO-GENERATED-DOCS:START -->
|
|
10
|
+
|
|
11
|
+
# TypeScript Type Utilities Installation
|
|
12
|
+
|
|
13
|
+
The [`@tolki/types`](https://www.npmjs.com/package/@tolki/types) package provides a variety of TypeScript types inspired by Laravel's data structures such as pagination results and models. You can install it via npm, yarn, or pnpm:
|
|
14
|
+
|
|
15
|
+
```bash [npm]
|
|
16
|
+
npm install @tolki/types
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash [yarn]
|
|
20
|
+
yarn add @tolki/types
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash [pnpm]
|
|
24
|
+
pnpm add @tolki/types
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Importing the Package
|
|
28
|
+
|
|
29
|
+
Example usage of types in a Vue SFC setup script section:
|
|
30
|
+
|
|
31
|
+
```vue
|
|
32
|
+
<script lang="ts" setup>
|
|
33
|
+
import type { LengthAwarePaginator, TimestampModel } from "@tolki/types";
|
|
34
|
+
|
|
35
|
+
interface User extends TimestampModel {
|
|
36
|
+
name: string;
|
|
37
|
+
email: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const { users } = defineProps<{
|
|
41
|
+
users: LengthAwarePaginator<User>;
|
|
42
|
+
}>();
|
|
43
|
+
</script>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
See the [TypeScript Types Documentation](/typescript/typescript-utilities-list) for a full list of available types and their descriptions.
|
|
47
|
+
|
|
48
|
+
# Tolki TypeScript Type Utilities List
|
|
49
|
+
|
|
50
|
+
## Paginator Utilities Types
|
|
51
|
+
|
|
52
|
+
Laravel provides 3 built in [pagination modes](https://laravel.com/docs/pagination). Each returns a similar but slightly different structured response. When you're working on the front end and using TypeScript, you'd have to write out what each response structure looks like for each pagination mode. These utility types provide you the type-safe representations of these responses, making it easier to define pagination data structures in a type safe manner.
|
|
53
|
+
|
|
54
|
+
[LengthAwarePaginator](#lengthawarepaginator) [SimplePaginator](#simplepaginator) [CursorPaginator](#cursorpaginator) [Pagination Components](#pagination-components)
|
|
55
|
+
|
|
56
|
+
### LengthAwarePaginator
|
|
57
|
+
|
|
58
|
+
Imagine you have a Laravel controller with an Inertia response of your users table paginated.
|
|
59
|
+
|
|
60
|
+
```PHP
|
|
61
|
+
<?php
|
|
62
|
+
|
|
63
|
+
namespace App\Http\Controllers;
|
|
64
|
+
|
|
65
|
+
use App\Models\User;
|
|
66
|
+
use Inertia\Inertia;
|
|
67
|
+
use Inertia\Response;
|
|
68
|
+
|
|
69
|
+
class UserController extends Controller
|
|
70
|
+
{
|
|
71
|
+
public function index(): Response
|
|
72
|
+
{
|
|
73
|
+
return Inertia::render('Users', [
|
|
74
|
+
'users' => User::query()->paginate(15),
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
On your TypeScript based front end, you can quickly define what the pagination response structure looks like by importing the `LengthAwarePaginator` type.
|
|
81
|
+
|
|
82
|
+
The `LengthAwarePaginator` type receives a single generic type parameter which represents the type of the individual items in the paginated data.
|
|
83
|
+
|
|
84
|
+
```vue
|
|
85
|
+
<script setup lang="ts">
|
|
86
|
+
import type { LengthAwarePaginator } from "@tolki/types";
|
|
87
|
+
|
|
88
|
+
interface User {
|
|
89
|
+
id: number;
|
|
90
|
+
name: string;
|
|
91
|
+
email: string;
|
|
92
|
+
created_at: string;
|
|
93
|
+
updated_at: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
defineProps<{
|
|
97
|
+
users: LengthAwarePaginator<User>;
|
|
98
|
+
}>();
|
|
99
|
+
</script>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The example above is a script section of a Vue single file component, but it can be applied to React or Svelte just as easily.
|
|
103
|
+
|
|
104
|
+
With that definition above, you can now use the structure with type safety of the pagination result in your front-end.
|
|
105
|
+
|
|
106
|
+
Example in Vue template:
|
|
107
|
+
|
|
108
|
+
```vue
|
|
109
|
+
<template>
|
|
110
|
+
<div>
|
|
111
|
+
<table v-if="users.data.length > 0">
|
|
112
|
+
<thead>
|
|
113
|
+
<tr>
|
|
114
|
+
<th>ID</th>
|
|
115
|
+
<th>Name</th>
|
|
116
|
+
<th>Email</th>
|
|
117
|
+
<th>Created At</th>
|
|
118
|
+
<th>Updated At</th>
|
|
119
|
+
</tr>
|
|
120
|
+
</thead>
|
|
121
|
+
<tbody>
|
|
122
|
+
<tr v-for="user in users.data" :key="user.id">
|
|
123
|
+
<td>{{ user.id }}</td>
|
|
124
|
+
<td>{{ user.name }}</td>
|
|
125
|
+
<td>{{ user.email }}</td>
|
|
126
|
+
<td>{{ user.created_at }}</td>
|
|
127
|
+
<td>{{ user.updated_at }}</td>
|
|
128
|
+
</tr>
|
|
129
|
+
</tbody>
|
|
130
|
+
</table>
|
|
131
|
+
|
|
132
|
+
<div v-else>No users found.</div>
|
|
133
|
+
|
|
134
|
+
<div>
|
|
135
|
+
Showing results from {{ users.from }} to {{ users.to }} of
|
|
136
|
+
{{ users.total }}
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
</template>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### SimplePaginator
|
|
143
|
+
|
|
144
|
+
If you use the `simplePaginate` response function as show below, you can use the `SimplePaginator` type to define the response structure.
|
|
145
|
+
|
|
146
|
+
Example Laravel controller:
|
|
147
|
+
|
|
148
|
+
```PHP
|
|
149
|
+
<?php
|
|
150
|
+
|
|
151
|
+
namespace App\Http\Controllers;
|
|
152
|
+
|
|
153
|
+
use App\Models\User;
|
|
154
|
+
use Inertia\Inertia;
|
|
155
|
+
use Inertia\Response;
|
|
156
|
+
|
|
157
|
+
class UserController extends Controller
|
|
158
|
+
{
|
|
159
|
+
public function index(): Response
|
|
160
|
+
{
|
|
161
|
+
return Inertia::render('Users', [
|
|
162
|
+
'users' => User::query()->simplePaginate(15),
|
|
163
|
+
]);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
In your TypeScript based front end, you can use the `SimplePaginator` type, which also accepts a single generic parameter to define the data structure.
|
|
169
|
+
|
|
170
|
+
```vue
|
|
171
|
+
<script setup lang="ts">
|
|
172
|
+
import type { SimplePaginator } from "@tolki/types";
|
|
173
|
+
|
|
174
|
+
interface User {
|
|
175
|
+
id: number;
|
|
176
|
+
name: string;
|
|
177
|
+
email: string;
|
|
178
|
+
created_at: string;
|
|
179
|
+
updated_at: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
defineProps<{
|
|
183
|
+
users: SimplePaginator<User>;
|
|
184
|
+
}>();
|
|
185
|
+
</script>
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### CursorPaginator
|
|
189
|
+
|
|
190
|
+
The last pagination result that Laravel provides is the `cursor` pagination. For that response structure you can use the `CursorPaginator` type.
|
|
191
|
+
|
|
192
|
+
Example Laravel controller:
|
|
193
|
+
|
|
194
|
+
```PHP
|
|
195
|
+
<?php
|
|
196
|
+
|
|
197
|
+
namespace App\Http\Controllers;
|
|
198
|
+
|
|
199
|
+
use App\Models\User;
|
|
200
|
+
use Inertia\Inertia;
|
|
201
|
+
use Inertia\Response;
|
|
202
|
+
|
|
203
|
+
class UserController extends Controller
|
|
204
|
+
{
|
|
205
|
+
public function index(): Response
|
|
206
|
+
{
|
|
207
|
+
return Inertia::render('Users', [
|
|
208
|
+
'users' => User::query()->cursorPaginate(15),
|
|
209
|
+
]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
In your TypeScript based front end, you can now use the `CursorPaginator` type to define the response structure, which also accepts a single generic parameter to define the data structure.
|
|
215
|
+
|
|
216
|
+
```vue
|
|
217
|
+
<script setup lang="ts">
|
|
218
|
+
import type { CursorPaginator } from "@tolki/types";
|
|
219
|
+
|
|
220
|
+
interface User {
|
|
221
|
+
id: number;
|
|
222
|
+
name: string;
|
|
223
|
+
email: string;
|
|
224
|
+
created_at: string;
|
|
225
|
+
updated_at: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
defineProps<{
|
|
229
|
+
users: CursorPaginator<User>;
|
|
230
|
+
}>();
|
|
231
|
+
</script>
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Pagination components
|
|
235
|
+
|
|
236
|
+
You can also use the pagination types to write type safe pagination components for your front end UI. A rough incomplete pagination example that supports all three pagination response types would look something like this.
|
|
237
|
+
|
|
238
|
+
```vue
|
|
239
|
+
<script setup lang="ts" generic="T">
|
|
240
|
+
import type {
|
|
241
|
+
CursorPaginator,
|
|
242
|
+
LengthAwarePaginator,
|
|
243
|
+
SimplePaginator,
|
|
244
|
+
} from "@tolki/types";
|
|
245
|
+
|
|
246
|
+
defineProps<{
|
|
247
|
+
data: LengthAwarePaginator<T> | SimplePaginator<T> | CursorPaginator<T>;
|
|
248
|
+
}>;
|
|
249
|
+
</script>
|
|
250
|
+
|
|
251
|
+
<template>
|
|
252
|
+
<div>
|
|
253
|
+
<!-- pagination UI implementation -->
|
|
254
|
+
</div>
|
|
255
|
+
</template>
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Then you'd use your pagination component like this:
|
|
259
|
+
|
|
260
|
+
```vue
|
|
261
|
+
<script setup lang="ts">
|
|
262
|
+
import type { LengthAwarePaginator } from "@tolki/types";
|
|
263
|
+
|
|
264
|
+
interface User {
|
|
265
|
+
/* .. */
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
defineProps<{
|
|
269
|
+
users: LengthAwarePaginator<User>;
|
|
270
|
+
}>();
|
|
271
|
+
</script>
|
|
272
|
+
|
|
273
|
+
<template>
|
|
274
|
+
<div>
|
|
275
|
+
<!-- T is inferred as User -->
|
|
276
|
+
<Pagination :data="users" />
|
|
277
|
+
</div>
|
|
278
|
+
</template>
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Model Utilities Types
|
|
282
|
+
|
|
283
|
+
Unless you're using a package like [Model Typer](https://github.com/fumeapp/modeltyper) or [Laravel Wayfinder](https://github.com/laravel/wayfinder), you probably find yourself defining models manually on a TypeScript based Laravel project.
|
|
284
|
+
|
|
285
|
+
The purpose of the following model utility types is to simplify and standardize the way you define these models.
|
|
286
|
+
|
|
287
|
+
### Model Types
|
|
288
|
+
|
|
289
|
+
You can use the simplest `Model` type which only defines a possibly undefined id key and any number of string keyed unknown values.
|
|
290
|
+
|
|
291
|
+
The `Model` type receives a generic parameter to define the type of ID it should be. With that defined, you can now use any number of columns on a `User` type, however, they'll simply register as `unknown`.
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
import type { Model } from "@tolki/types";
|
|
295
|
+
|
|
296
|
+
interface User extends Model<number> {
|
|
297
|
+
title: string;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const user: User = getUser();
|
|
301
|
+
|
|
302
|
+
user.id; // inferred as number
|
|
303
|
+
user.title; // inferred as string
|
|
304
|
+
user.created_at; // inferred as unknown
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
If you'd like to auto define time stamp columns, you can use the `TimestampModel` type, the `SoftDeleteModel`, or the `AllTimestampsModel` type to define `created_at`, `updated_at`, and `deleted_at` in one go.
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import type {
|
|
311
|
+
TimestampModel,
|
|
312
|
+
SoftDeleteModel,
|
|
313
|
+
AllTimestampsModel,
|
|
314
|
+
} from "@tolki/types";
|
|
315
|
+
|
|
316
|
+
interface User extends TimestampModel<number> {
|
|
317
|
+
title: string;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const user: User = getUser();
|
|
321
|
+
|
|
322
|
+
user.id; // inferred as number
|
|
323
|
+
user.title; // inferred as string
|
|
324
|
+
user.created_at; // inferred as string
|
|
325
|
+
user.updated_at; // inferred as string
|
|
326
|
+
|
|
327
|
+
interface Post extends SoftDeleteModel<string> {}
|
|
328
|
+
|
|
329
|
+
const post: Post = getPost();
|
|
330
|
+
|
|
331
|
+
post.id; // inferred as string
|
|
332
|
+
post.deleted_at; // inferred as string
|
|
333
|
+
|
|
334
|
+
interface Notification extends AllTimestampsModel<number> {
|
|
335
|
+
message: string;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const notification: Notification = getNotification();
|
|
339
|
+
|
|
340
|
+
notification.id; // inferred as number
|
|
341
|
+
notification.message; // inferred as string
|
|
342
|
+
notification.created_at; // inferred as string
|
|
343
|
+
notification.updated_at; // inferred as string
|
|
344
|
+
notification.deleted_at; // inferred as string
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Model Relationship Types
|
|
348
|
+
|
|
349
|
+
Many times when you load eloquent results, it is common to use [relationship aggregation eloquent methods](https://laravel.com/docs/12.x/eloquent-relationships#aggregating-related-models) like count, max, min, sum, average or if a relationship exists using eloquent methods like `withCount`. Those methods create a dynamic column on an eloquent result.
|
|
350
|
+
|
|
351
|
+
For example, let's say you queried this:
|
|
352
|
+
|
|
353
|
+
```PHP
|
|
354
|
+
Post::select(['title', 'body'])
|
|
355
|
+
->withCount('comments')
|
|
356
|
+
->get();
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
That creates the following result structure
|
|
360
|
+
|
|
361
|
+
```TypeScript
|
|
362
|
+
interface Post {
|
|
363
|
+
title: string;
|
|
364
|
+
body: string;
|
|
365
|
+
comments_count: number;
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
For these type of situations, you can use the following helpers to quickly define one or more relationships.
|
|
370
|
+
|
|
371
|
+
[WithCount](#withcount) [WithMax](#withmax) [WithMin](#withmin) [WithSum](#withsum) [WithAvg](#withavg) [WithExists](#withexists)
|
|
372
|
+
|
|
373
|
+
### WithCount
|
|
374
|
+
|
|
375
|
+
```TypeScript
|
|
376
|
+
import type { WithCount } from "@tolki/types";
|
|
377
|
+
|
|
378
|
+
interface Post extends WithCount<"comments" | "authors">{
|
|
379
|
+
title: string;
|
|
380
|
+
body: string;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const post: Post = getPost();
|
|
384
|
+
|
|
385
|
+
post.title // inferred as string
|
|
386
|
+
post.comments_count // inferred as number | null
|
|
387
|
+
post.authors_count // inferred as number | null
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### WithMax
|
|
391
|
+
|
|
392
|
+
```TypeScript
|
|
393
|
+
import type { WithMax } from "@tolki/types";
|
|
394
|
+
|
|
395
|
+
interface Post extends WithMax<"comments" | "authors">{
|
|
396
|
+
title: string;
|
|
397
|
+
body: string;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const post: Post = getPost();
|
|
401
|
+
|
|
402
|
+
post.title // inferred as string
|
|
403
|
+
post.comments_max // inferred as number | null
|
|
404
|
+
post.authors_max // inferred as number | null
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### WithMin
|
|
408
|
+
|
|
409
|
+
```TypeScript
|
|
410
|
+
import type { WithMin } from "@tolki/types";
|
|
411
|
+
|
|
412
|
+
interface Post extends WithMin<"comments" | "authors">{
|
|
413
|
+
title: string;
|
|
414
|
+
body: string;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const post: Post = getPost();
|
|
418
|
+
|
|
419
|
+
post.title // inferred as string
|
|
420
|
+
post.comments_min // inferred as number | null
|
|
421
|
+
post.authors_min // inferred as number | null
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
### WithSum
|
|
425
|
+
|
|
426
|
+
```TypeScript
|
|
427
|
+
import type { WithSum } from "@tolki/types";
|
|
428
|
+
|
|
429
|
+
interface Post extends WithSum<"comments" | "authors">{
|
|
430
|
+
title: string;
|
|
431
|
+
body: string;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const post: Post = getPost();
|
|
435
|
+
|
|
436
|
+
post.title // inferred as string
|
|
437
|
+
post.comments_sum // inferred as number | null
|
|
438
|
+
post.authors_sum // inferred as number | null
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
### WithAvg
|
|
442
|
+
|
|
443
|
+
```TypeScript
|
|
444
|
+
import type { WithAvg } from "@tolki/types";
|
|
445
|
+
|
|
446
|
+
interface Post extends WithAvg<"comments" | "authors">{
|
|
447
|
+
title: string;
|
|
448
|
+
body: string;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const post: Post = getPost();
|
|
452
|
+
|
|
453
|
+
post.title // inferred as string
|
|
454
|
+
post.comments_avg // inferred as number | null
|
|
455
|
+
post.authors_avg // inferred as number | null
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
### WithExists
|
|
459
|
+
|
|
460
|
+
```TypeScript
|
|
461
|
+
import type { WithExists } from "@tolki/types";
|
|
462
|
+
|
|
463
|
+
interface Post extends WithExists<"comments" | "authors">{
|
|
464
|
+
title: string;
|
|
465
|
+
body: string;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const post: Post = getPost();
|
|
469
|
+
|
|
470
|
+
post.title // inferred as string
|
|
471
|
+
post.comments_exists // inferred as boolean
|
|
472
|
+
post.authors_exists // inferred as boolean
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
## JsonResource Utilities Types
|
|
476
|
+
|
|
477
|
+
When working with Laravel's [JsonResource](https://laravel.com/docs/eloquent-resources), you often need to define the structure of the JSON response in your TypeScript front end. By default, JSON resources either return a single resource object or a collection of resource objects, which do not have a different data structure from your models' properties.
|
|
478
|
+
|
|
479
|
+
However, when you paginate results in JSON sources, the structure changes. The following utility types help you define these structures in a type-safe manner.
|
|
480
|
+
|
|
481
|
+
For example, given this Laravel controller snippet:
|
|
482
|
+
|
|
483
|
+
```PHP
|
|
484
|
+
use App\Http\Resources\UserCollection;
|
|
485
|
+
use App\Models\User;
|
|
486
|
+
|
|
487
|
+
Route::get('/users', function () {
|
|
488
|
+
return new UserCollection(User::paginate());
|
|
489
|
+
});
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
The JSON structure returned will look like this:
|
|
493
|
+
|
|
494
|
+
```json
|
|
495
|
+
{
|
|
496
|
+
"data": [
|
|
497
|
+
{
|
|
498
|
+
"id": 1,
|
|
499
|
+
"name": "Eladio Schroeder Sr.",
|
|
500
|
+
"email": "therese28@example.com"
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
"id": 2,
|
|
504
|
+
"name": "Liliana Mayert",
|
|
505
|
+
"email": "evandervort@example.com"
|
|
506
|
+
}
|
|
507
|
+
],
|
|
508
|
+
"links": {
|
|
509
|
+
"first": "http://example.com/users?page=1",
|
|
510
|
+
"last": "http://example.com/users?page=1",
|
|
511
|
+
"prev": null,
|
|
512
|
+
"next": null
|
|
513
|
+
},
|
|
514
|
+
"meta": {
|
|
515
|
+
"current_page": 1,
|
|
516
|
+
"from": 1,
|
|
517
|
+
"last_page": 1,
|
|
518
|
+
"path": "http://example.com/users",
|
|
519
|
+
"per_page": 15,
|
|
520
|
+
"to": 10,
|
|
521
|
+
"total": 10
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
To define this structure in TypeScript, you can use the `JsonResourcePaginator` utility type as shown below:
|
|
527
|
+
|
|
528
|
+
```typescript
|
|
529
|
+
import type { JsonResourcePaginator } from "@tolki/types";
|
|
530
|
+
|
|
531
|
+
interface User {
|
|
532
|
+
id: number;
|
|
533
|
+
name: string;
|
|
534
|
+
email: string;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
defineProps<{
|
|
538
|
+
users: JsonResourcePaginator<User>;
|
|
539
|
+
}>();
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
That utility type accepts a single generic parameter which represents the type of the individual items in the paginated data.
|
|
543
|
+
|
|
544
|
+
The `JsonResourcePaginator` type itself uses two additional utility types: `JsonResourceLinks` and `JsonResourceMeta`, which represent the `links` and `meta` sections of the JSON response respectively.
|
|
545
|
+
|
|
546
|
+
```typescript
|
|
547
|
+
export interface JsonResourcePaginator<T> {
|
|
548
|
+
data: T[];
|
|
549
|
+
meta: JsonResourceMeta;
|
|
550
|
+
links: JsonResourceLinks;
|
|
551
|
+
}
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
If needed, you can also use these types individually to define the structure of the `links` and `meta` sections separately. You can also compose your own JSON resource structures if your PHP resources return custom structures.
|
|
555
|
+
|
|
556
|
+
<!-- AUTO-GENERATED-DOCS:END -->
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolki/types",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
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",
|
|
@@ -36,6 +36,9 @@
|
|
|
36
36
|
"./collections": {
|
|
37
37
|
"types": "./src/collections.d.ts"
|
|
38
38
|
},
|
|
39
|
+
"./enums": {
|
|
40
|
+
"types": "./src/enums.d.ts"
|
|
41
|
+
},
|
|
39
42
|
"./data": {
|
|
40
43
|
"types": "./src/data.d.ts"
|
|
41
44
|
},
|
|
@@ -48,8 +51,8 @@
|
|
|
48
51
|
"./pagination": {
|
|
49
52
|
"types": "./src/pagination.d.ts"
|
|
50
53
|
},
|
|
51
|
-
"./path-
|
|
52
|
-
"types": "./src/path-
|
|
54
|
+
"./path-resolve": {
|
|
55
|
+
"types": "./src/path-resolve.d.ts"
|
|
53
56
|
},
|
|
54
57
|
"./responses": {
|
|
55
58
|
"types": "./src/responses.d.ts"
|
|
@@ -61,7 +64,13 @@
|
|
|
61
64
|
"types": "./src/utils.d.ts"
|
|
62
65
|
}
|
|
63
66
|
},
|
|
67
|
+
"tolki": {
|
|
68
|
+
"docs": [
|
|
69
|
+
"typescript/index.md",
|
|
70
|
+
"typescript/typescript-utilities-list.md"
|
|
71
|
+
]
|
|
72
|
+
},
|
|
64
73
|
"scripts": {
|
|
65
|
-
"
|
|
74
|
+
"readme:build": "ts-node ../../scripts/readme-build.ts --filter @tolki/types"
|
|
66
75
|
}
|
|
67
76
|
}
|
package/src/enums.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract the case key names from the _cases tuple.
|
|
3
|
+
*/
|
|
4
|
+
export type CaseKeys<TEnum extends EnumConst> = TEnum["_cases"][number];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the instance method key names from the `_methods` tuple.
|
|
8
|
+
*/
|
|
9
|
+
export type MethodKeys<TEnum extends EnumConst> = TEnum["_methods"][number];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the static helper key names from the `_static` tuple.
|
|
13
|
+
*/
|
|
14
|
+
export type StaticKeys<TEnum extends EnumConst> = TEnum["_static"][number];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Extracts the helper key names from the `_helpers` tuple.
|
|
18
|
+
*/
|
|
19
|
+
export type HelperKeys<TEnum extends EnumConst> = TEnum["_helpers"][number];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Constraint for all generated enum const objects.
|
|
23
|
+
*
|
|
24
|
+
* - `_cases` lists the case key names (matches PHP enum cases).
|
|
25
|
+
* - `_methods` lists instance method names whose values are per-case maps.
|
|
26
|
+
* Only these properties are resolved per-case by `toEnum`; everything else
|
|
27
|
+
* passes through as-is. Use an empty array when there are no instance methods.
|
|
28
|
+
* - `_helpers` lists helper method names added to the enum.
|
|
29
|
+
* - `_static` lists static helper method names added to the enum.
|
|
30
|
+
*/
|
|
31
|
+
export type EnumConst = {
|
|
32
|
+
readonly _cases: readonly string[];
|
|
33
|
+
readonly _methods: readonly string[];
|
|
34
|
+
readonly _static: readonly string[];
|
|
35
|
+
readonly [key: string]: unknown;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the case key name whose value matches the provided value.
|
|
40
|
+
* Uses a mapped type to find case keys where the value type matches bidirectionally.
|
|
41
|
+
*/
|
|
42
|
+
export type MatchedCaseKey<TEnum extends EnumConst, TValue> = {
|
|
43
|
+
[K in CaseKeys<TEnum>]: TEnum[K] extends TValue
|
|
44
|
+
? TValue extends TEnum[K]
|
|
45
|
+
? K
|
|
46
|
+
: never
|
|
47
|
+
: never;
|
|
48
|
+
}[CaseKeys<TEnum>];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Resolves the value of an instance method for a given case value.
|
|
52
|
+
* Iterates case keys to find the one whose value matches TValue bidirectionally,
|
|
53
|
+
* then looks up the method's value at that case key.
|
|
54
|
+
*/
|
|
55
|
+
export type ResolveMethodForValue<
|
|
56
|
+
TEnum extends EnumConst,
|
|
57
|
+
K extends keyof TEnum,
|
|
58
|
+
TValue,
|
|
59
|
+
> = {
|
|
60
|
+
[C in CaseKeys<TEnum>]: TEnum[C] extends TValue
|
|
61
|
+
? TValue extends TEnum[C]
|
|
62
|
+
? C extends keyof TEnum[K]
|
|
63
|
+
? TEnum[K][C]
|
|
64
|
+
: never
|
|
65
|
+
: never
|
|
66
|
+
: never;
|
|
67
|
+
}[CaseKeys<TEnum>];
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Builds the resolved instance type by iterating over all non-case, non-meta keys.
|
|
71
|
+
*
|
|
72
|
+
* - value: set directly to TValue.
|
|
73
|
+
* - Instance methods (listed in _methods): resolved per-case via ResolveMethodForValue.
|
|
74
|
+
* - Static methods / other values: passed through unchanged.
|
|
75
|
+
*/
|
|
76
|
+
export type ResolvedEnumInstance<TEnum extends EnumConst, TValue> = {
|
|
77
|
+
readonly value: TValue;
|
|
78
|
+
} & {
|
|
79
|
+
readonly [K in Exclude<
|
|
80
|
+
keyof TEnum,
|
|
81
|
+
CaseKeys<TEnum> | "_cases" | "_methods" | "_helpers" | "_static"
|
|
82
|
+
>]: K extends MethodKeys<TEnum>
|
|
83
|
+
? ResolveMethodForValue<TEnum, K, TValue>
|
|
84
|
+
: TEnum[K];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Extracts the union of all case values from an enum const.
|
|
89
|
+
*/
|
|
90
|
+
export type CaseValue<TEnum extends EnumConst> = {
|
|
91
|
+
[K in CaseKeys<TEnum>]: TEnum[K];
|
|
92
|
+
}[CaseKeys<TEnum>];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The fully inferred return type of toEnum.
|
|
96
|
+
*/
|
|
97
|
+
export type ToEnumResult<TEnum extends EnumConst, TValue> =
|
|
98
|
+
MatchedCaseKey<TEnum, TValue> extends never
|
|
99
|
+
? never
|
|
100
|
+
: ResolvedEnumInstance<TEnum, TValue>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Overrides properties in T with properties in U.
|
|
104
|
+
* Equivalent to a spread: `{ ...T, ...U }`.
|
|
105
|
+
* Keys to omit are derived automatically from U — no manual list needed.
|
|
106
|
+
*/
|
|
107
|
+
type Override<T, U> = Omit<T, keyof U> & U;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The return type of `defineEnum`.
|
|
111
|
+
*
|
|
112
|
+
* Enriches a raw enum const object with bound `from`, `tryFrom`, and `cases`
|
|
113
|
+
* methods so callers don't need to pass the enum object every time.
|
|
114
|
+
*
|
|
115
|
+
* Uses `Omit` to strip any existing `from`, `tryFrom`, or `cases` properties
|
|
116
|
+
* from the original enum before intersecting, avoiding `never` from conflicting
|
|
117
|
+
* property types (e.g. when the enum has `from: null` as a helper stub).
|
|
118
|
+
*/
|
|
119
|
+
export type DefineEnumResult<TEnum extends EnumConst> = Override<
|
|
120
|
+
TEnum,
|
|
121
|
+
{
|
|
122
|
+
readonly _helpers: string[];
|
|
123
|
+
readonly from: <const TValue extends CaseValue<TEnum>>(
|
|
124
|
+
value: TValue,
|
|
125
|
+
) => ToEnumResult<TEnum, TValue>;
|
|
126
|
+
readonly tryFrom: <const TValue extends CaseValue<TEnum>>(
|
|
127
|
+
value: TValue,
|
|
128
|
+
) => ToEnumResult<TEnum, TValue> | null;
|
|
129
|
+
readonly cases: () => Array<ToEnumResult<TEnum, CaseValue<TEnum>>>;
|
|
130
|
+
}
|
|
131
|
+
>;
|
package/src/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export type * from "./arrays";
|
|
2
2
|
export type * from "./collections";
|
|
3
3
|
export type * from "./data";
|
|
4
|
+
export type * from "./enums";
|
|
4
5
|
export type * from "./models";
|
|
5
6
|
export type * from "./objects";
|
|
6
7
|
export type * from "./pagination";
|
|
7
|
-
export type * from "./path-
|
|
8
|
+
export type * from "./path-resolve";
|
|
8
9
|
export type * from "./resource-responses";
|
|
9
10
|
export type * from "./strings";
|
|
10
11
|
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;
|