@tolki/types 1.5.0 → 1.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/types",
3
- "version": "1.5.0",
3
+ "version": "1.6.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",
package/src/arrays.d.ts CHANGED
@@ -64,6 +64,18 @@ export type UndotResult<
64
64
  ? UndotValue<TValue>[]
65
65
  : TValue[];
66
66
 
67
+ /**
68
+ * Key shape accepted by `Arr.undot`: either a bare numeric index (`0`, `"0"`)
69
+ * or a dot-path whose first segment is numeric (`"0.1"`, `"12.foo"`). A
70
+ * string-first-segment key like `"user.languages.0"` belongs to `Obj.undot`
71
+ * instead — this type rejects it at compile time rather than at runtime.
72
+ *
73
+ * @example
74
+ * UndotArrayKey // matches: 0, "0", "1.2", "3.foo.bar"
75
+ * UndotArrayKey // rejects: "foo", "user.languages.0"
76
+ */
77
+ export type UndotArrayKey = number | `${number}` | `${number}.${string}`;
78
+
67
79
  /**
68
80
  * Helper type to check if an array is mutable (not readonly)
69
81
  */
@@ -83,3 +95,41 @@ export type AddToArray<T extends unknown[], V> =
83
95
  ? Array<U | V>
84
96
  : never
85
97
  : never;
98
+
99
+ /**
100
+ * Removes `null` from an array's element type.
101
+ * Used by helpers that filter null values out of an array.
102
+ *
103
+ * @example
104
+ * NonNullableArray<(string | null)[]> // string[]
105
+ * NonNullableArray<string[]> // string[]
106
+ */
107
+ export type NonNullableArray<T extends readonly unknown[]> =
108
+ T extends readonly (infer U)[] ? Exclude<U, null>[] : never;
109
+
110
+ /**
111
+ * Removes the values PHP treats as falsy from an array's element type.
112
+ *
113
+ * @example
114
+ * TruthyArray<(string | null | undefined)[]> // string[]
115
+ * TruthyArray<(number | false)[]> // number[]
116
+ */
117
+ export type TruthyArray<T extends readonly unknown[]> =
118
+ T extends readonly (infer U)[]
119
+ ? Exclude<U, null | undefined | false | 0 | "">[]
120
+ : never;
121
+
122
+ /**
123
+ * A single sort descriptor accepted by array sort helpers.
124
+ *
125
+ * - a dot-notated key path, sorted ascending
126
+ * - a `[key]` single-element tuple, identical to a bare key path, ascending
127
+ * - a `[key, direction]` tuple: `true`/`'asc'`/`"Ascending"` sorts ascending,
128
+ * everything else descending, mirroring `Collection::sortByMany`
129
+ * - a comparator returning a negative, zero, or positive number
130
+ */
131
+ export type SortSpec<TValue> =
132
+ | string
133
+ | readonly [string]
134
+ | readonly [string, boolean | "Ascending" | "Descending" | "asc" | "desc"]
135
+ | ((a: TValue, b: TValue) => number);
@@ -1,2 +1,2 @@
1
- export type ProxyTarget = this;
1
+ export type ProxyTarget = object;
2
2
  export type PropertyName = string | symbol;
@@ -261,3 +261,52 @@ export type GetFieldType<
261
261
  : R
262
262
  : TDefault
263
263
  : TDefault;
264
+
265
+ /**
266
+ * Resolves the value type produced by plucking `TPath` out of each element of
267
+ * an array. A `*` segment produces an array of the values found at that level.
268
+ * Non-literal paths resolve to `unknown`.
269
+ *
270
+ * The terminal (non-wildcard) arm maps `undefined` to `null`: at runtime,
271
+ * `resolvePluckPath` (arr.ts) substitutes `null` for any segment that
272
+ * resolves to `undefined` — including an absent optional property — so an
273
+ * optional property's type must be `T | null`, never `T | undefined`, to
274
+ * match what `pluck` actually returns.
275
+ *
276
+ * The same mapping applies to intermediate segments: an optional or
277
+ * nullable intermediate makes the whole resolved type nullable, because
278
+ * `resolvePluckPath` short-circuits to `null` the moment any segment is
279
+ * null or undefined.
280
+ *
281
+ * @example
282
+ * PluckValue<{ name: string }, "name"> // string
283
+ * PluckValue<{ user: { name: string } }, "user.name"> // string
284
+ * PluckValue<{ users: { first: string }[] }, "users.*.first"> // string[]
285
+ * PluckValue<{ name: string }, string> // unknown
286
+ * PluckValue<{ name?: string }, "name"> // string | null
287
+ * PluckValue<{ user?: { name: string } }, "user.name"> // string | null
288
+ */
289
+ export type PluckValue<TItem, TPath> =
290
+ TPath extends `${infer Head}.${infer Rest}`
291
+ ? Head extends "*"
292
+ ? TItem extends readonly (infer TElement)[]
293
+ ? PluckValue<TElement, Rest>[]
294
+ : unknown
295
+ : Head extends keyof TItem
296
+ ? null extends TItem[Head]
297
+ ? PluckValue<NonNullable<TItem[Head]>, Rest> | null
298
+ : undefined extends TItem[Head]
299
+ ? PluckValue<NonNullable<TItem[Head]>, Rest> | null
300
+ : PluckValue<TItem[Head], Rest>
301
+ : unknown
302
+ : TPath extends "*"
303
+ ? TItem extends readonly (infer TElement)[]
304
+ ? TElement[]
305
+ : unknown
306
+ : TPath extends keyof TItem
307
+ ? TItem[TPath] extends infer TResolved
308
+ ? TResolved extends undefined
309
+ ? null
310
+ : TResolved
311
+ : never
312
+ : unknown;