vize 0.607.0 → 0.609.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.
@@ -0,0 +1,311 @@
1
+ // Shared pattern types for vize virtual TypeScript.
2
+ declare global {
3
+ // Adapted from vuejs/language-tools e68d41bfef1770f8428f92097abe959b57de62d2.
4
+ // MIT license; see pattern_matching.LICENSE.
5
+ // Kept as a checked declaration file, shared once per TypeScript program.
6
+ /*
7
+ MIT License
8
+
9
+ Copyright (c) 2021-present Johnson Chu
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ */
29
+ namespace $Patterns {
30
+ type Reachable<T> = [T] extends [never] ? { "Unreachable v-when": never } : true;
31
+ /**
32
+ * RFC 823 pattern types. Match narrows an arm; Subtract keeps the values left
33
+ * for later arms. Objects split by property and arrays by head/tail, so nested
34
+ * unions do not need their entire Cartesian product expanded up front.
35
+ */
36
+ type Pattern =
37
+ | ["any"]
38
+ | ["literal" | "value", unknown]
39
+ | ["or", Pattern[]]
40
+ | ["object", [PropertyKey, Pattern][]]
41
+ | ["array", Pattern[], boolean];
42
+
43
+ type IsAny<T> = 0 extends 1 & T ? true : false;
44
+
45
+ // Vue can unwrap unknown to {}. Keep that space conservative too.
46
+ type IsUnknownLike<T> = [keyof T] extends [never] ? ({} extends T ? true : false) : false;
47
+
48
+ type IsUnion<T, C = T> = T extends C ? ([C] extends [T] ? false : true) : never;
49
+
50
+ // A runtime value with a union type matches one member, so it cannot cover the whole union.
51
+ type IsSingleton<T> =
52
+ IsAny<T> extends true
53
+ ? false
54
+ : true extends IsUnion<T>
55
+ ? false
56
+ : T extends string | number | bigint | boolean | null | undefined
57
+ ? string extends T
58
+ ? false
59
+ : number extends T
60
+ ? false
61
+ : bigint extends T
62
+ ? false
63
+ : boolean extends T
64
+ ? false
65
+ : true
66
+ : false;
67
+
68
+ type Simplify<T> = { [K in keyof T]: T[K] };
69
+
70
+ // Remove optionality before substituting V. A homomorphic -? mapping would
71
+ // strip undefined from V when exactOptionalPropertyTypes is disabled.
72
+ type PresentProperty<T, K extends keyof T, V> = {
73
+ [Q in keyof Required<Pick<T, K>>]: V;
74
+ };
75
+
76
+ // Retain the original type when refinement changes nothing, including its readonly modifiers.
77
+ type ReplaceProperty<T, K extends keyof T, V> = [V] extends [never]
78
+ ? never
79
+ : {} extends Pick<T, K>
80
+ ? Simplify<Omit<T, K> & PresentProperty<T, K, V>>
81
+ : [T[K]] extends [V]
82
+ ? [V] extends [T[K]]
83
+ ? T
84
+ : Simplify<Omit<T, K> & PresentProperty<T, K, V>>
85
+ : Simplify<Omit<T, K> & PresentProperty<T, K, V>>;
86
+
87
+ // Turn the pattern descriptor emitted by vMatch.ts into a positive type constraint.
88
+ type PatternType<P> = P extends ["any"]
89
+ ? unknown
90
+ : P extends ["literal" | "value", infer V]
91
+ ? V
92
+ : P extends ["or", infer A extends unknown[]]
93
+ ? PatternType<A[number]>
94
+ : P extends ["object", infer A extends [PropertyKey, unknown][]]
95
+ ? { [Q in A[number] as Q[0]]: PatternType<Q[1]> }
96
+ : P extends ["array", infer A extends unknown[], infer R]
97
+ ? [...{ [I in keyof A]: PatternType<A[I]> }, ...(R extends true ? unknown[] : [])]
98
+ : never;
99
+
100
+ type Match<T, P extends Pattern> = MatchImpl<T, P>;
101
+ type Subtract<T, P extends Pattern> = SubtractImpl<T, P>;
102
+
103
+ // Narrow one union member at a time. Object patterns constrain only their listed properties.
104
+ type MatchImpl<T, P> = P extends ["any"]
105
+ ? T
106
+ : IsAny<T> extends true
107
+ ? PatternType<P>
108
+ : unknown extends T
109
+ ? PatternType<P>
110
+ : IsUnknownLike<T> extends true
111
+ ? T & PatternType<P>
112
+ : T extends unknown
113
+ ? P extends ["literal" | "value", infer V]
114
+ ? T & V
115
+ : P extends ["or", infer A extends unknown[]]
116
+ ? MatchImpl<T, A[number]>
117
+ : P extends ["object", infer A extends [PropertyKey, unknown][]]
118
+ ? T extends null | undefined
119
+ ? never
120
+ : MatchObject<T, A>
121
+ : P extends ["array", infer A extends unknown[], infer R]
122
+ ? T extends readonly unknown[]
123
+ ? T extends unknown[]
124
+ ? MatchArray<T, A, R>
125
+ : Readonly<MatchArray<T, A, R>>
126
+ : never
127
+ : never
128
+ : never;
129
+
130
+ // Presence does not remove undefined from an optional property value.
131
+ type RequireProperty<T, K extends keyof T> =
132
+ {} extends Pick<T, K> ? Simplify<Omit<T, K> & PresentProperty<T, K, T[K]>> : T;
133
+
134
+ type MatchObject<T, A> = A extends [infer H extends [PropertyKey, unknown], ...infer R]
135
+ ? H[0] extends keyof T
136
+ ? H[1] extends ["any"]
137
+ ? MatchObject<RequireProperty<T, H[0]>, R>
138
+ : MatchObject<ReplaceProperty<T, H[0], MatchImpl<T[H[0]], H[1]>>, R>
139
+ : MatchObject<T & { [K in H[0]]: PatternType<H[1]> }, R>
140
+ : T;
141
+
142
+ type MatchArray<T extends readonly unknown[], A extends unknown[], R> = A extends [
143
+ infer H,
144
+ ...infer Tail,
145
+ ]
146
+ ? T extends readonly []
147
+ ? never
148
+ : T extends readonly [infer V, ...infer Rest]
149
+ ? Prepend<MatchImpl<V, H>, MatchArray<Rest, Tail, R>>
150
+ : T extends readonly [(infer V)?, ...infer Rest]
151
+ ? Prepend<MatchImpl<T[0], H>, MatchArray<Rest, Tail, R>>
152
+ : never
153
+ : R extends true
154
+ ? T
155
+ : [] extends T
156
+ ? []
157
+ : never;
158
+
159
+ type Prepend<H, T> = [H] extends [never]
160
+ ? never
161
+ : T extends readonly unknown[]
162
+ ? [H, ...T]
163
+ : never;
164
+
165
+ // Only unguarded arms call this helper. Unknown and open primitive spaces stay uncovered.
166
+ type SubtractImpl<T, P> = [T] extends [never]
167
+ ? never
168
+ : P extends ["any"]
169
+ ? never
170
+ : IsAny<T> extends true
171
+ ? T
172
+ : unknown extends T
173
+ ? T
174
+ : IsUnknownLike<T> extends true
175
+ ? T
176
+ : string extends T
177
+ ? T
178
+ : number extends T
179
+ ? T
180
+ : bigint extends T
181
+ ? T
182
+ : P extends ["or", infer A extends unknown[]]
183
+ ? SubtractAlternatives<T, A>
184
+ : T extends unknown
185
+ ? P extends ["literal", infer V]
186
+ ? Exclude<T, V>
187
+ : P extends ["value", infer V]
188
+ ? IsSingleton<V> extends true
189
+ ? Exclude<T, V>
190
+ : T
191
+ : P extends ["object", infer A extends [PropertyKey, unknown][]]
192
+ ? T extends null | undefined
193
+ ? T
194
+ : SubtractObject<T, A>
195
+ : P extends ["array", infer A extends unknown[], infer R]
196
+ ? T extends readonly unknown[]
197
+ ? T extends unknown[]
198
+ ? SubtractArray<T, A, R>
199
+ : Readonly<SubtractArray<T, A, R>>
200
+ : T
201
+ : T
202
+ : never;
203
+
204
+ type SubtractAlternatives<T, A> = A extends [infer H, ...infer R]
205
+ ? SubtractAlternatives<SubtractImpl<T, H>, R>
206
+ : T;
207
+
208
+ // The remaining object space has three cases: the key is absent, its pattern fails,
209
+ // or its pattern matches and a later property fails.
210
+ type SubtractObject<T, A> = [T] extends [never]
211
+ ? never
212
+ : A extends [infer H extends [PropertyKey, unknown], ...infer R]
213
+ ? H[0] extends keyof T
214
+ ? H[1] extends ["any"]
215
+ ?
216
+ | ({} extends Pick<T, H[0]> ? Omit<T, H[0]> & { [K in H[0]]?: never } : never)
217
+ | SubtractObject<RequireProperty<T, H[0]>, R>
218
+ :
219
+ | ({} extends Pick<T, H[0]> ? Omit<T, H[0]> & { [K in H[0]]?: never } : never)
220
+ | ReplaceProperty<T, H[0], SubtractImpl<T[H[0]], H[1]>>
221
+ | SubtractObject<ReplaceProperty<T, H[0], MatchImpl<T[H[0]], H[1]>>, R>
222
+ : T
223
+ : never;
224
+
225
+ // Split a tuple or array into its head and tail. Exact-length patterns also leave longer arrays.
226
+ type SubtractArray<T extends readonly unknown[], A extends unknown[], R> = A extends [
227
+ infer H,
228
+ ...infer Tail,
229
+ ]
230
+ ? T extends readonly []
231
+ ? T
232
+ : T extends readonly [infer V, ...infer Rest]
233
+ ?
234
+ | Prepend<SubtractImpl<V, H>, Rest>
235
+ | Prepend<MatchImpl<V, H>, SubtractArray<Rest, Tail, R>>
236
+ : T extends readonly [(infer V)?, ...infer Rest]
237
+ ?
238
+ | []
239
+ | Prepend<SubtractImpl<T[0], H>, Rest>
240
+ | Prepend<MatchImpl<T[0], H>, SubtractArray<Rest, Tail, R>>
241
+ : T
242
+ : R extends true
243
+ ? never
244
+ : T extends readonly []
245
+ ? never
246
+ : T extends readonly [unknown, ...unknown[]]
247
+ ? T
248
+ : T extends readonly [(infer V)?, ...infer Rest]
249
+ ? [T[0], ...Rest]
250
+ : never;
251
+
252
+ // Diagnostic examples must be valid pattern syntax. Fall back to _ when a literal needs escaping.
253
+ type FormatPattern<T> = T extends string
254
+ ? string extends T
255
+ ? "_"
256
+ : T extends `${string}${"'" | '"' | "\\" | "\n" | "\r"}${string}`
257
+ ? "_"
258
+ : `'${T}'`
259
+ : T extends bigint
260
+ ? bigint extends T
261
+ ? "_"
262
+ : `${T}n`
263
+ : T extends number
264
+ ? number extends T
265
+ ? "_"
266
+ : `${T}`
267
+ : T extends boolean | null | undefined
268
+ ? `${T}`
269
+ : T extends readonly unknown[]
270
+ ? `[${FormatTuple<T>}]`
271
+ : "_";
272
+
273
+ type FormatTuple<T extends readonly unknown[]> = T extends readonly []
274
+ ? ""
275
+ : T extends readonly [infer H, ...infer R]
276
+ ? `${FormatPattern<H>}${R extends [] ? "" : `, ${FormatTuple<R>}`}`
277
+ : "...";
278
+
279
+ type MissingPatternMessage<T> =
280
+ IsAny<T> extends true
281
+ ? 'Add an unguarded v-when="_" to prove coverage.'
282
+ : T extends string
283
+ ? string extends T
284
+ ? "An open string requires an unguarded catch-all."
285
+ : T extends `${string}${"'" | '"' | "\\" | "\n" | "\r"}${string}`
286
+ ? 'Add an unguarded v-when="_".'
287
+ : `Missing v-when="'${T}'".`
288
+ : T extends bigint
289
+ ? bigint extends T
290
+ ? "An open bigint requires an unguarded catch-all."
291
+ : `Missing v-when="${T}n".`
292
+ : T extends number
293
+ ? number extends T
294
+ ? "An open number requires an unguarded catch-all."
295
+ : `Missing v-when="${T}".`
296
+ : T extends boolean | null | undefined
297
+ ? `Missing v-when="${T}".`
298
+ : T extends readonly unknown[]
299
+ ? `Missing v-when="[${FormatTuple<T>}]".`
300
+ : 'Coverage cannot be proven. Add missing structural arms or an unguarded v-when="_".';
301
+
302
+ // A nonempty remaining space produces an error mapped to the v-match subject.
303
+ type Exhaustiveness<T> = [T] extends [never]
304
+ ? true
305
+ : {
306
+ "Non-exhaustive v-match": T;
307
+ } & { [K in MissingPatternMessage<T>]: never };
308
+ }
309
+ }
310
+
311
+ export {};