@tolki/types 1.9.0 → 1.10.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.9.0",
3
+ "version": "1.10.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",
@@ -42,6 +42,9 @@
42
42
  "./data": {
43
43
  "types": "./src/data.d.ts"
44
44
  },
45
+ "./lang": {
46
+ "types": "./src/lang.d.ts"
47
+ },
45
48
  "./models": {
46
49
  "types": "./src/models.d.ts"
47
50
  },
package/src/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export type * from "./collections";
3
3
  export type * from "./data";
4
4
  export type * from "./enums";
5
5
  export type * from "./json-api";
6
+ export type * from "./lang";
6
7
  export type * from "./models";
7
8
  export type * from "./objects";
8
9
  export type * from "./pagination";
package/src/lang.d.ts ADDED
@@ -0,0 +1,256 @@
1
+ import type { Countable } from "./data";
2
+ import type {
3
+ ObjectDotPath,
4
+ ObjectDotPathValue,
5
+ ObjectResolvePath,
6
+ } from "./path-resolve";
7
+
8
+ /** A tag callback receives the text between `<key>` and `</key>`. */
9
+ type TagCallback = (children: string) => unknown;
10
+
11
+ /** A value a placeholder can take; what PHP cannot cast to a string throws. */
12
+ type ReplacementValue =
13
+ | string
14
+ | number
15
+ | boolean
16
+ | null
17
+ | undefined
18
+ | object
19
+ | TagCallback;
20
+
21
+ /** Placeholder values keyed by name, or a list keyed by position. */
22
+ type Replacements =
23
+ | Readonly<Record<string, ReplacementValue>>
24
+ | readonly ReplacementValue[];
25
+
26
+ /** Any class, so a handler can be registered for it. */
27
+ type StringableType<TValue = unknown> = abstract new (
28
+ ...args: never[]
29
+ ) => TValue;
30
+
31
+ /** Handlers that turn an instance of the exact class into a replacement. */
32
+ type StringableHandlers = ReadonlyMap<
33
+ StringableType,
34
+ (value: never) => unknown
35
+ >;
36
+
37
+ /** A group of lines, as a PHP lang file's array holds them. */
38
+ type LangGroup = { [key: string]: LangValue } | LangValue[];
39
+
40
+ /** A line, or a group of lines. */
41
+ type LangValue = string | LangGroup;
42
+
43
+ /** A line, or a group of lines, as the caller hands them over; the translator never writes to them. */
44
+ type LangInputValue =
45
+ | string
46
+ | { readonly [key: string]: LangInputValue }
47
+ | readonly LangInputValue[];
48
+
49
+ /** One locale's lines: a group under its file's name (`ns::group` for a package), any other value a JSON line. */
50
+ type LocaleLines = Readonly<Record<string, LangInputValue>>;
51
+
52
+ /** Every locale's lines, keyed by locale. */
53
+ type LangLines = Readonly<Record<string, LocaleLines>>;
54
+
55
+ /** What `get()` answers: a line, or a copy of a group. */
56
+ type TranslationValue = string | LangGroup;
57
+
58
+ /** Whether some locale's keys are typed only as `string`, so the lines name no key in particular. */
59
+ type HasUntypedLocale<TLines> = true extends {
60
+ [TLocale in keyof TLines]: string extends keyof TLines[TLocale]
61
+ ? true
62
+ : false;
63
+ }[keyof TLines]
64
+ ? true
65
+ : false;
66
+
67
+ /** The depth left one level further down. */
68
+ type NextDepth = [never, 0, 1, 2, 3, 4, 5, 6, 7];
69
+
70
+ /** Each locale's keys to a `TValue`: JSON lines, groups, and paths at most 4 levels below a group. */
71
+ type LocalePaths<TLines extends LangLines, TValue> = {
72
+ [TLocale in keyof TLines]: ObjectDotPath<TLines[TLocale], TValue, 5>;
73
+ };
74
+
75
+ /** Every key the lines name: each locale's JSON lines and groups, and the paths at most 4 levels below a group. */
76
+ // Indexed here rather than inside LocalePaths: a key union returned through an alias is far slower to check.
77
+ type TranslationKey<TLines extends LangLines> =
78
+ HasUntypedLocale<TLines> extends true
79
+ ? string
80
+ : LocalePaths<TLines, unknown>[keyof TLines];
81
+
82
+ /** A key without a namespace split at its first dot into the group and the item, null for a whole group. */
83
+ type BasicSegments<TKey extends string> =
84
+ TKey extends `${infer TGroup}.${infer TItem}`
85
+ ? [TGroup, TItem]
86
+ : [TKey, null];
87
+
88
+ /** A key split as `parseKey()` splits it: the namespace (`*` for none), the group and the item. */
89
+ type ParsedKey<TKey extends string> =
90
+ TKey extends `${infer TNamespace}::${infer TRest}`
91
+ ? [
92
+ TNamespace,
93
+ ...BasicSegments<
94
+ TRest extends `${infer TFirst}::${string}` ? TFirst : TRest
95
+ >,
96
+ ]
97
+ : ["*", ...BasicSegments<TKey>];
98
+
99
+ /** The lines and groups a key reaches in every locale's groups; a JSON line is a string, which every answer holds. */
100
+ type LocaleValues<
101
+ TLines extends LangLines,
102
+ TName extends string,
103
+ TItem extends string | null,
104
+ > = {
105
+ [TLocale in keyof TLines]: TName extends keyof TLines[TLocale]
106
+ ? ReachedValue<
107
+ GroupItem<Exclude<TLines[TLocale][TName], string>, TItem>
108
+ >
109
+ : never;
110
+ }[keyof TLines];
111
+
112
+ /** A group's item, or the whole group for none; only a pattern's paths, such as a list's `${number}`, are templates. */
113
+ // Distributed so a JSON line, which leaves no group, reaches nothing: ObjectResolvePath reads `never` as unknown.
114
+ type GroupItem<TGroup, TItem extends string | null> = TGroup extends unknown
115
+ ? TItem extends string
116
+ ? Record<never, never> extends Record<TItem, unknown>
117
+ ? ObjectDotPathValue<TGroup, TItem>
118
+ : ObjectResolvePath<TGroup, TItem, never>
119
+ : TGroup
120
+ : never;
121
+
122
+ /** PHP skips an empty group, so `get()` answers the key for it. */
123
+ // An unknown value stands for any line or group, which keyof alone would read as an empty group.
124
+ type ReachedValue<TValue> = unknown extends TValue
125
+ ? TranslationValue
126
+ : TValue extends string
127
+ ? TValue
128
+ : TValue extends readonly never[]
129
+ ? string
130
+ : [keyof TValue] extends [never]
131
+ ? string
132
+ : TValue;
133
+
134
+ /** A value as `get()` copies it, every line a string once replaced; a loosely typed group copies as a `LangGroup`. */
135
+ // A mapped type over a list expands at once, so a recursive type such as LangValue needs the guards and the depth.
136
+ type Translated<TValue, TDepth extends number = 8> = TValue extends string
137
+ ? string
138
+ : TDepth extends 0
139
+ ? TValue
140
+ : { [key: string]: LangValue } extends TValue
141
+ ? string extends keyof TValue
142
+ ? { [key: string]: LangValue }
143
+ : TValue
144
+ : LangValue[] extends TValue
145
+ ? LangValue[]
146
+ : {
147
+ -readonly [TKey in keyof TValue]: Translated<
148
+ TValue[TKey],
149
+ NextDepth[TDepth]
150
+ >;
151
+ };
152
+
153
+ /** The groups a key reaches, with their lines, or the key itself, which a locale without them answers. */
154
+ type FoundResult<TValues> = Translated<TValues> | string;
155
+
156
+ /** What `get()` answers for a key spelled out in full. */
157
+ type LiteralKeyResult<TLines extends LangLines, TKey extends string> =
158
+ ParsedKey<TKey> extends [
159
+ infer TNamespace extends string,
160
+ infer TGroup extends string,
161
+ infer TItem extends string | null,
162
+ ]
163
+ ? TNamespace extends "*"
164
+ ? TGroup extends "*"
165
+ ? TItem extends null
166
+ ? LangGroup | string
167
+ : string
168
+ : FoundResult<LocaleValues<TLines, TGroup, TItem>>
169
+ : FoundResult<
170
+ LocaleValues<TLines, `${TNamespace}::${TGroup}`, TItem>
171
+ >
172
+ : never;
173
+
174
+ /** What `get()` answers for each of several keys spelled out in full. */
175
+ // KeyResult must not call itself: for a generic key the Extract never resolves, and the recursion hits TS2589.
176
+ type LiteralKeysResult<
177
+ TLines extends LangLines,
178
+ TKeys extends string,
179
+ > = TKeys extends string ? LiteralKeyResult<TLines, TKeys> : never;
180
+
181
+ /** What `get()` answers for each key in a union; a pattern answers the key itself, or any known group it matches. */
182
+ // Only a pattern such as `a.${string}` makes Record<TKey, unknown> an index signature, which an empty object satisfies.
183
+ type KeyResult<
184
+ TLines extends LangLines,
185
+ TKey extends string,
186
+ > = TKey extends string
187
+ ? Record<never, never> extends Record<TKey, unknown>
188
+ ?
189
+ | string
190
+ | LiteralKeysResult<
191
+ TLines,
192
+ Extract<LocalePaths<TLines, object>[keyof TLines], TKey>
193
+ >
194
+ : LiteralKeyResult<TLines, TKey>
195
+ : never;
196
+
197
+ /** What `get()` answers: the group a key names in any locale or the key, otherwise a string; either if untyped. */
198
+ type TranslationResult<
199
+ TLines extends LangLines,
200
+ TKey extends string,
201
+ > = string extends TKey
202
+ ? TranslationValue
203
+ : HasUntypedLocale<TLines> extends true
204
+ ? TranslationValue
205
+ : KeyResult<TLines, TKey>;
206
+
207
+ /** What `array()` answers for a key: the group it names, or any group when it names none. */
208
+ type TranslationArray<TLines extends LangLines, TKey extends string> = [
209
+ Extract<TranslationResult<TLines, TKey>, LangGroup>,
210
+ ] extends [never]
211
+ ? LangGroup
212
+ : Extract<TranslationResult<TLines, TKey>, LangGroup>;
213
+
214
+ /** Answers the line for a key no locale has; `null` or `undefined` keeps the key. */
215
+ type MissingKeyHandler = (
216
+ key: string,
217
+ replace: Replacements,
218
+ locale: string,
219
+ fallback: boolean,
220
+ ) => string | null | undefined;
221
+
222
+ /** Turns the locales a lookup would read into the ones it reads. */
223
+ type LocalesResolver = (locales: string[]) => string[];
224
+
225
+ /** What PHP's `count()` accepts: an array, a `Countable`, or here a `Map`, a `Set` or an object. */
226
+ type LangCountable =
227
+ | readonly unknown[]
228
+ | ReadonlyMap<unknown, unknown>
229
+ | ReadonlySet<unknown>
230
+ | Countable
231
+ | Readonly<Record<string, unknown>>;
232
+
233
+ /** A number, or a value whose count is the number. */
234
+ type ChoiceNumber = number | LangCountable;
235
+
236
+ // An export list keeps the helpers above private: a .d.ts module without one exports every declaration.
237
+ export type {
238
+ ChoiceNumber,
239
+ LangCountable,
240
+ LangGroup,
241
+ LangInputValue,
242
+ LangLines,
243
+ LangValue,
244
+ LocaleLines,
245
+ LocalesResolver,
246
+ MissingKeyHandler,
247
+ Replacements,
248
+ ReplacementValue,
249
+ StringableHandlers,
250
+ StringableType,
251
+ TagCallback,
252
+ TranslationArray,
253
+ TranslationKey,
254
+ TranslationResult,
255
+ TranslationValue,
256
+ };
@@ -388,9 +388,10 @@ type ObjectIndexStep<T, K extends string> = K extends keyof T
388
388
  * ends the path; a built-in object's members are mostly on its prototype, which `Object.hasOwn` doesn't see.
389
389
  */
390
390
  type ObjectPathStep<T, K extends string> = T extends readonly unknown[]
391
- ? K extends `${infer N extends number}`
392
- ? N extends KnownObjectKeys<T>
393
- ? [T[N], false]
391
+ ? K extends `${number}`
392
+ ? // A tuple declares its indices as string keys, so the segment matches one and its number never does.
393
+ K extends KnownObjectKeys<T>
394
+ ? [T[K & keyof T], false]
394
395
  : [T[number], true]
395
396
  : [never, true]
396
397
  : T extends ReadonlyMap<unknown, infer V>
@@ -555,3 +556,125 @@ export type ObjectResolvePath<T, P, TDefault = null> = P extends
555
556
  : ObjectPathTop<T, P> extends [infer V, infer M]
556
557
  ? V | (true extends M ? TDefault : never)
557
558
  : TDefault;
559
+
560
+ /** A depth `ObjectDotPath` can count down from; a larger one would index past `ObjectDotPathDepth` and lift the cap. */
561
+ type ObjectDotPathMaxDepth = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
562
+
563
+ /** The depth left one segment further down. */
564
+ type ObjectDotPathDepth = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
565
+
566
+ /** Whether a member of `T` can be a function, as on a DOM node, a class instance with methods or a Collection. */
567
+ // An `any` member is not a method, so a loosely typed group still lists its keys.
568
+ type ObjectHasMethod<T> = true extends {
569
+ [TKey in keyof T]-?: 0 extends 1 & T[TKey]
570
+ ? false
571
+ : [Extract<T[TKey], (...args: never[]) => unknown>] extends [never]
572
+ ? false
573
+ : true;
574
+ }[keyof T]
575
+ ? true
576
+ : false;
577
+
578
+ /** The paths below a member: an object with a method is listed but never walked into. */
579
+ // Distributed, so `HTMLElement | null` ends too; a list's own methods do not count.
580
+ type ObjectDotPathBelow<
581
+ T,
582
+ TValue,
583
+ TDepth extends ObjectDotPathMaxDepth,
584
+ > = T extends readonly unknown[]
585
+ ? ObjectDotPath<T, TValue, TDepth>
586
+ : ObjectHasMethod<T> extends true
587
+ ? never
588
+ : ObjectDotPath<T, TValue, TDepth>;
589
+
590
+ /** A path, when the value under it can be a `TValue`. */
591
+ type ObjectDotPathTo<TPath extends string, TChild, TValue> = [
592
+ Extract<TChild, TValue>,
593
+ ] extends [never]
594
+ ? never
595
+ : TPath;
596
+
597
+ /**
598
+ * Every dot path into `T`, at most `TDepth` segments deep (0 to 10), whose value can be a `TValue`. An object's paths
599
+ * go by its string and number keys and a list's by index. A member holding a scalar, a function, an object with a
600
+ * method such as a DOM node, or a `Map`, `Set`, `WeakMap`, `WeakSet`, `Date`, `RegExp` or `Promise` ends the path.
601
+ *
602
+ * @example
603
+ * ObjectDotPath<{ a: { b: string }; c: string }> // "a" | "a.b" | "c"
604
+ * ObjectDotPath<{ l: { x: string }[] }> // "l" | `l.${number}` | `l.${number}.x`
605
+ * ObjectDotPath<{ a: { b: string }; c: string }, object> // "a"
606
+ */
607
+ export type ObjectDotPath<
608
+ T,
609
+ TValue = unknown,
610
+ TDepth extends ObjectDotPathMaxDepth = 5,
611
+ > = TDepth extends 0
612
+ ? never
613
+ : T extends
614
+ | string
615
+ | number
616
+ | boolean
617
+ | bigint
618
+ | symbol
619
+ | ((...args: never[]) => unknown)
620
+ | ReadonlyMap<unknown, unknown>
621
+ | ReadonlySet<unknown>
622
+ | WeakMap<object, unknown>
623
+ | WeakSet<object>
624
+ | Date
625
+ | RegExp
626
+ | Promise<unknown>
627
+ ? never
628
+ : T extends readonly (infer TItem)[]
629
+ ?
630
+ | ObjectDotPathTo<`${number}`, TItem, TValue>
631
+ | `${number}.${ObjectDotPathBelow<TItem, TValue, ObjectDotPathDepth[TDepth]>}`
632
+ : T extends object
633
+ ? {
634
+ [TKey in keyof T & (string | number)]:
635
+ | ObjectDotPathTo<`${TKey}`, T[TKey], TValue>
636
+ | `${TKey}.${ObjectDotPathBelow<T[TKey], TValue, ObjectDotPathDepth[TDepth]>}`;
637
+ }[keyof T & (string | number)]
638
+ : never;
639
+
640
+ /** One segment of a path `ObjectDotPath` lists: a pattern reads any member of a list, or an index signature's value. */
641
+ type ObjectDotPathStep<T, K extends string> =
642
+ Record<never, never> extends Record<K, unknown>
643
+ ? T extends readonly unknown[]
644
+ ? T[number]
645
+ : T extends object
646
+ ? K extends `${number}`
647
+ ? number extends keyof T
648
+ ? T[number & keyof T]
649
+ : never
650
+ : string extends keyof T
651
+ ? T[string & keyof T]
652
+ : never
653
+ : never
654
+ : ObjectPathStep<T, K>[0];
655
+
656
+ /** Walks a path `ObjectDotPath` lists, one segment at a time. */
657
+ type ObjectDotPathWalk<
658
+ T,
659
+ P extends string,
660
+ > = P extends `${infer Head}.${infer Rest}`
661
+ ? ObjectDotPathStep<T, Head> extends infer V
662
+ ? unknown extends V
663
+ ? unknown
664
+ : ObjectDotPathWalk<NonNullable<V>, Rest>
665
+ : never
666
+ : ObjectDotPathStep<T, P>;
667
+
668
+ /**
669
+ * The value at a path {@link ObjectDotPath} lists, where `${number}` is any one list index and `${string}` any one key.
670
+ * As with `Arr::get()` and `data_get()`, a dotted key is read whole only at the top level, so `"g.a.b"`, which
671
+ * `ObjectDotPath` lists for `{ g: { "a.b": number } }`, reaches nothing.
672
+ *
673
+ * @example
674
+ * ObjectDotPathValue<{ l: { x: string }[] }, `l.${number}`> // { x: string }
675
+ */
676
+ export type ObjectDotPathValue<T, P extends string> = P extends string
677
+ ? Record<never, never> extends Record<P, unknown>
678
+ ? Exclude<ObjectDotPathWalk<T, P>, undefined>
679
+ : ObjectResolvePath<T, P, never>
680
+ : never;