@vicin/phantom 1.1.1 → 2.0.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/CHANGELOG.md +8 -0
- package/CONTRIBUTING.md +24 -0
- package/LICENSE +3 -1
- package/README.md +4 -13
- package/dist/{index.mjs → index.cjs} +25 -20
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.mts → index.d.cts} +107 -236
- package/dist/index.d.ts +107 -236
- package/dist/index.global.js +0 -9
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +10 -32
- package/dist/index.js.map +1 -1
- package/package.json +43 -46
- package/dist/index.mjs.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
declare namespace assertors {
|
|
2
|
-
/**
|
|
3
|
-
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
4
|
-
*
|
|
5
|
-
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
6
|
-
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
7
|
-
* where you know the value is valid.
|
|
8
|
-
*
|
|
9
|
-
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
10
|
-
*
|
|
11
|
-
* @template B - The brand declaration to assign.
|
|
12
|
-
* @returns A function that casts any value to the branded type.
|
|
13
|
-
*/
|
|
14
|
-
const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
15
2
|
/**
|
|
16
3
|
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
17
4
|
*
|
|
@@ -86,6 +73,15 @@ declare namespace assertors {
|
|
|
86
73
|
const revertTransformation: <Tr extends Transformation.Any>() => <T, I>(transformed: T, input: I) => Transformation.Revert<Tr, T, I>;
|
|
87
74
|
}
|
|
88
75
|
|
|
76
|
+
declare const __Phantom: unique symbol;
|
|
77
|
+
declare const __Tag: unique symbol;
|
|
78
|
+
declare const __Label: unique symbol;
|
|
79
|
+
declare const __Base: unique symbol;
|
|
80
|
+
declare const __OriginalType: unique symbol;
|
|
81
|
+
declare const __Input: unique symbol;
|
|
82
|
+
declare const __Traits: unique symbol;
|
|
83
|
+
declare const __Variants: unique symbol;
|
|
84
|
+
|
|
89
85
|
/**
|
|
90
86
|
* Base-type metadata.
|
|
91
87
|
*
|
|
@@ -95,28 +91,28 @@ declare namespace assertors {
|
|
|
95
91
|
declare namespace BaseCore {
|
|
96
92
|
/** Marker type for base constraints */
|
|
97
93
|
type Any = {
|
|
98
|
-
__Phantom: {
|
|
99
|
-
__Base?: unknown;
|
|
94
|
+
[__Phantom]: {
|
|
95
|
+
[__Base]?: unknown;
|
|
100
96
|
};
|
|
101
97
|
};
|
|
102
98
|
/** Declare a base type */
|
|
103
99
|
type Of<Base> = {
|
|
104
|
-
__Phantom: {
|
|
105
|
-
__Base?: Base;
|
|
100
|
+
[__Phantom]: {
|
|
101
|
+
[__Base]?: Base;
|
|
106
102
|
};
|
|
107
103
|
};
|
|
108
104
|
/** Conditionally declare a base type */
|
|
109
105
|
type OfIfExists<Base> = [Base] extends [never] ? {} : Of<Base>;
|
|
110
106
|
/** Extract the base type */
|
|
111
107
|
type BaseOf<T> = T extends {
|
|
112
|
-
__Phantom: {
|
|
113
|
-
__Base?: infer Base;
|
|
108
|
+
[__Phantom]: {
|
|
109
|
+
[__Base]?: infer Base;
|
|
114
110
|
};
|
|
115
111
|
} ? Exclude<Base, undefined> : unknown;
|
|
116
112
|
/** Check whether a base constraint exists */
|
|
117
113
|
type HasBase<T, B = unknown> = T extends {
|
|
118
|
-
__Phantom: {
|
|
119
|
-
__Base?: B;
|
|
114
|
+
[__Phantom]: {
|
|
115
|
+
[__Base]?: B;
|
|
120
116
|
};
|
|
121
117
|
} ? true : false;
|
|
122
118
|
}
|
|
@@ -129,32 +125,32 @@ declare namespace BaseCore {
|
|
|
129
125
|
declare namespace InputCore {
|
|
130
126
|
/** Marker type for input value */
|
|
131
127
|
type Any = {
|
|
132
|
-
__Phantom: {
|
|
133
|
-
__Input: unknown;
|
|
128
|
+
[__Phantom]: {
|
|
129
|
+
[__Input]: unknown;
|
|
134
130
|
};
|
|
135
131
|
};
|
|
136
132
|
/** Set input */
|
|
137
133
|
type Of<Input> = {
|
|
138
|
-
__Phantom: {
|
|
139
|
-
__Input: Input;
|
|
134
|
+
[__Phantom]: {
|
|
135
|
+
[__Input]: Input;
|
|
140
136
|
};
|
|
141
137
|
};
|
|
142
138
|
/** Conditionally set input */
|
|
143
139
|
type OfIfExists<Input> = [Input] extends [never] ? {} : {
|
|
144
|
-
__Phantom: {
|
|
145
|
-
__Input: Input;
|
|
140
|
+
[__Phantom]: {
|
|
141
|
+
[__Input]: Input;
|
|
146
142
|
};
|
|
147
143
|
};
|
|
148
144
|
/** Extract the input */
|
|
149
145
|
type InputOf<T> = T extends {
|
|
150
|
-
__Phantom: {
|
|
151
|
-
__Input: infer Input;
|
|
146
|
+
[__Phantom]: {
|
|
147
|
+
[__Input]: infer Input;
|
|
152
148
|
};
|
|
153
149
|
} ? Input : never;
|
|
154
150
|
/** Check whether an input exists */
|
|
155
151
|
type HasInput<T, I = unknown> = T extends {
|
|
156
|
-
__Phantom: {
|
|
157
|
-
__Input: I;
|
|
152
|
+
[__Phantom]: {
|
|
153
|
+
[__Input]: I;
|
|
158
154
|
};
|
|
159
155
|
} ? true : false;
|
|
160
156
|
}
|
|
@@ -167,28 +163,28 @@ declare namespace InputCore {
|
|
|
167
163
|
declare namespace LabelCore {
|
|
168
164
|
/** Marker type for labeled values */
|
|
169
165
|
type Any = {
|
|
170
|
-
__Phantom: {
|
|
171
|
-
__Label?: string;
|
|
166
|
+
[__Phantom]: {
|
|
167
|
+
[__Label]?: string;
|
|
172
168
|
};
|
|
173
169
|
};
|
|
174
170
|
/** Attach a label */
|
|
175
171
|
type Of<Label extends string> = {
|
|
176
|
-
__Phantom: {
|
|
177
|
-
__Label?: Label;
|
|
172
|
+
[__Phantom]: {
|
|
173
|
+
[__Label]?: Label;
|
|
178
174
|
};
|
|
179
175
|
};
|
|
180
176
|
/** Conditionally attach a label */
|
|
181
177
|
type OfIfExists<Label extends string> = [Label] extends [never] ? {} : Of<Label>;
|
|
182
178
|
/** Extract the label */
|
|
183
179
|
type LabelOf<T> = T extends {
|
|
184
|
-
__Phantom: {
|
|
185
|
-
__Label?: infer Label;
|
|
180
|
+
[__Phantom]: {
|
|
181
|
+
[__Label]?: infer Label;
|
|
186
182
|
};
|
|
187
183
|
} ? Exclude<Label, undefined> : never;
|
|
188
184
|
/** Check whether a label exists */
|
|
189
185
|
type HasLabel<T, L extends string = string> = T extends {
|
|
190
|
-
__Phantom: {
|
|
191
|
-
__Label?: L;
|
|
186
|
+
[__Phantom]: {
|
|
187
|
+
[__Label]?: L;
|
|
192
188
|
};
|
|
193
189
|
} ? true : false;
|
|
194
190
|
}
|
|
@@ -202,28 +198,28 @@ declare namespace LabelCore {
|
|
|
202
198
|
declare namespace TagCore {
|
|
203
199
|
/** Marker type for any tagged value */
|
|
204
200
|
type Any = {
|
|
205
|
-
__Phantom: {
|
|
206
|
-
__Tag: string | symbol;
|
|
201
|
+
[__Phantom]: {
|
|
202
|
+
[__Tag]: string | symbol;
|
|
207
203
|
};
|
|
208
204
|
};
|
|
209
205
|
/** Attach a specific tag */
|
|
210
206
|
type Of<Tag extends string | symbol> = {
|
|
211
|
-
__Phantom: {
|
|
212
|
-
__Tag: Tag;
|
|
207
|
+
[__Phantom]: {
|
|
208
|
+
[__Tag]: Tag;
|
|
213
209
|
};
|
|
214
210
|
};
|
|
215
211
|
/** Conditionally attach a tag if it exists */
|
|
216
212
|
type OfIfExists<Tag extends string | symbol> = [Tag] extends [never] ? {} : Of<Tag>;
|
|
217
213
|
/** Extract the tag from a type */
|
|
218
214
|
type TagOf<T> = T extends {
|
|
219
|
-
__Phantom: {
|
|
220
|
-
__Tag: infer Tag;
|
|
215
|
+
[__Phantom]: {
|
|
216
|
+
[__Tag]: infer Tag;
|
|
221
217
|
};
|
|
222
218
|
} ? Tag : never;
|
|
223
219
|
/** Check whether a type is tagged */
|
|
224
220
|
type HasTag<T, Ta extends string | symbol = string | symbol> = T extends {
|
|
225
|
-
__Phantom: {
|
|
226
|
-
__Tag: Ta;
|
|
221
|
+
[__Phantom]: {
|
|
222
|
+
[__Tag]: Ta;
|
|
227
223
|
};
|
|
228
224
|
} ? true : false;
|
|
229
225
|
}
|
|
@@ -237,44 +233,42 @@ declare namespace TagCore {
|
|
|
237
233
|
declare namespace TraitsCore {
|
|
238
234
|
/** Marker type for trait-bearing values */
|
|
239
235
|
type Any = {
|
|
240
|
-
__Phantom: {
|
|
241
|
-
__Traits: {};
|
|
236
|
+
[__Phantom]: {
|
|
237
|
+
[__Traits]: {};
|
|
242
238
|
};
|
|
243
239
|
};
|
|
244
240
|
/** Declare a single trait */
|
|
245
241
|
type Of<Trait extends string | symbol> = {
|
|
246
|
-
__Phantom: {
|
|
247
|
-
__Traits: {
|
|
242
|
+
[__Phantom]: {
|
|
243
|
+
[__Traits]: {
|
|
248
244
|
[K in Trait]: true;
|
|
249
245
|
};
|
|
250
246
|
};
|
|
251
247
|
};
|
|
252
248
|
/** Construct traits from an explicit trait map */
|
|
253
249
|
type FromMap<Traits extends Record<string | symbol, true>> = {
|
|
254
|
-
__Phantom: {
|
|
255
|
-
__Traits: Traits;
|
|
250
|
+
[__Phantom]: {
|
|
251
|
+
[__Traits]: Traits;
|
|
256
252
|
};
|
|
257
253
|
};
|
|
258
254
|
/** Conditionally declare a trait */
|
|
259
|
-
type OfIfExists<Trait extends string | symbol> = [Trait] extends [
|
|
260
|
-
never
|
|
261
|
-
] ? {} : Of<Trait>;
|
|
255
|
+
type OfIfExists<Trait extends string | symbol> = [Trait] extends [never] ? {} : Of<Trait>;
|
|
262
256
|
/** Extract the trait map */
|
|
263
257
|
type TraitsOf<T> = T extends {
|
|
264
|
-
__Phantom: {
|
|
265
|
-
__Traits: infer Traits;
|
|
258
|
+
[__Phantom]: {
|
|
259
|
+
[__Traits]: infer Traits;
|
|
266
260
|
};
|
|
267
261
|
} ? Traits extends Record<string | symbol, true> ? Traits : never : never;
|
|
268
262
|
/** Extract trait keys */
|
|
269
263
|
type TraitKeysOf<T> = T extends {
|
|
270
|
-
__Phantom: {
|
|
271
|
-
__Traits: infer Traits;
|
|
264
|
+
[__Phantom]: {
|
|
265
|
+
[__Traits]: infer Traits;
|
|
272
266
|
};
|
|
273
267
|
} ? keyof Traits : never;
|
|
274
268
|
/** Check if any traits exist */
|
|
275
269
|
type HasTraits<T, Tr extends string | symbol = string | symbol> = T extends {
|
|
276
|
-
__Phantom: {
|
|
277
|
-
__Traits: Record<Tr, true>;
|
|
270
|
+
[__Phantom]: {
|
|
271
|
+
[__Traits]: Record<Tr, true>;
|
|
278
272
|
};
|
|
279
273
|
} ? true : false;
|
|
280
274
|
}
|
|
@@ -287,43 +281,43 @@ declare namespace TraitsCore {
|
|
|
287
281
|
declare namespace VariantsCore {
|
|
288
282
|
/** Marker type for variant-bearing values */
|
|
289
283
|
type Any = {
|
|
290
|
-
__Phantom: {
|
|
291
|
-
__Variants: string;
|
|
284
|
+
[__Phantom]: {
|
|
285
|
+
[__Variants]: string;
|
|
292
286
|
};
|
|
293
287
|
};
|
|
294
288
|
/** Declare allowed variants */
|
|
295
289
|
type Of<Variants extends string> = {
|
|
296
|
-
__Phantom: {
|
|
297
|
-
__Variants: Variants;
|
|
290
|
+
[__Phantom]: {
|
|
291
|
+
[__Variants]: Variants;
|
|
298
292
|
};
|
|
299
293
|
};
|
|
300
294
|
/** Conditionally declare variants */
|
|
301
295
|
type OfIfExists<Variants extends string> = [Variants] extends [never] ? {} : Of<Variants>;
|
|
302
296
|
/** Extract variant union */
|
|
303
297
|
type VariantsOf<T> = T extends {
|
|
304
|
-
__Phantom: {
|
|
305
|
-
__Variants: infer Variants;
|
|
298
|
+
[__Phantom]: {
|
|
299
|
+
[__Variants]: infer Variants;
|
|
306
300
|
};
|
|
307
301
|
} ? Variants extends string ? Variants : never : never;
|
|
308
302
|
/** Check whether variants exist */
|
|
309
303
|
type HasVariants<T> = T extends {
|
|
310
|
-
__Phantom: {
|
|
311
|
-
__Variants: string;
|
|
304
|
+
[__Phantom]: {
|
|
305
|
+
[__Variants]: string;
|
|
312
306
|
};
|
|
313
307
|
} ? true : false;
|
|
314
308
|
}
|
|
315
309
|
|
|
316
310
|
/** Stip phantom metadata object from a type */
|
|
317
311
|
type StripPhantom<T> = T extends {
|
|
318
|
-
__Phantom: {
|
|
319
|
-
__OriginalType?: infer O;
|
|
312
|
+
[__Phantom]: {
|
|
313
|
+
[__OriginalType]?: infer O;
|
|
320
314
|
};
|
|
321
315
|
} ? Exclude<O, undefined> : T extends {
|
|
322
|
-
__Phantom: {
|
|
323
|
-
__Base?: infer B;
|
|
316
|
+
[__Phantom]: {
|
|
317
|
+
[__Base]?: infer B;
|
|
324
318
|
};
|
|
325
319
|
} ? Exclude<B, undefined> : T extends {
|
|
326
|
-
__Phantom: object;
|
|
320
|
+
[__Phantom]: object;
|
|
327
321
|
} ? unknown : T;
|
|
328
322
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
329
323
|
declare const stripPhantom: <T>(value: T) => StripPhantom<T>;
|
|
@@ -342,15 +336,15 @@ declare namespace PhantomCore {
|
|
|
342
336
|
type StripPhantom<T> = _StripPhantom<T>;
|
|
343
337
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
344
338
|
const stripPhantom: <T>(value: T) => T extends {
|
|
345
|
-
__Phantom: {
|
|
346
|
-
__OriginalType?: infer O;
|
|
339
|
+
[__Phantom]: {
|
|
340
|
+
[__OriginalType]?: infer O;
|
|
347
341
|
};
|
|
348
342
|
} ? Exclude<O, undefined> : T extends {
|
|
349
|
-
__Phantom: {
|
|
350
|
-
__Base?: infer B;
|
|
343
|
+
[__Phantom]: {
|
|
344
|
+
[__Base]?: infer B;
|
|
351
345
|
};
|
|
352
346
|
} ? Exclude<B, undefined> : T extends {
|
|
353
|
-
__Phantom: object;
|
|
347
|
+
[__Phantom]: object;
|
|
354
348
|
} ? unknown : T;
|
|
355
349
|
}
|
|
356
350
|
|
|
@@ -401,27 +395,27 @@ type ErrorType<E> = {
|
|
|
401
395
|
* the original runtime type.
|
|
402
396
|
*/
|
|
403
397
|
type WithMetadata<T, Change extends object> = PhantomCore.StripPhantom<T> & {
|
|
404
|
-
__Phantom: Prettify<Merge<IfNever<PhantomCore.PhantomOf<T>>, Omit<IfNever<PhantomCore.PhantomOf<Change>>,
|
|
398
|
+
[__Phantom]: Prettify<Merge<IfNever<PhantomCore.PhantomOf<T>>, Omit<IfNever<PhantomCore.PhantomOf<Change>>, typeof __Base> & SetType<T>>>;
|
|
405
399
|
};
|
|
406
400
|
/**
|
|
407
401
|
* Patch phantom metadata without touching the original type.
|
|
408
402
|
* Used when mutating metadata declarations themselves.
|
|
409
403
|
*/
|
|
410
404
|
type PatchMetadata<T, Change extends object> = {
|
|
411
|
-
__Phantom: Prettify<Merge<IfNever<PhantomCore.PhantomOf<T>>, IfNever<PhantomCore.PhantomOf<Change>>>>;
|
|
405
|
+
[__Phantom]: Prettify<Merge<IfNever<PhantomCore.PhantomOf<T>>, IfNever<PhantomCore.PhantomOf<Change>>>>;
|
|
412
406
|
};
|
|
413
407
|
/**
|
|
414
408
|
* Remove a metadata dimension while preserving the original type.
|
|
415
409
|
*/
|
|
416
|
-
type WithoutMetadata<T, S extends
|
|
417
|
-
__Phantom: Prettify<Omit<IfNever<PhantomCore.PhantomOf<T>>, S> & SetType<T>>;
|
|
410
|
+
type WithoutMetadata<T, S extends symbol> = PhantomCore.StripPhantom<T> & {
|
|
411
|
+
[__Phantom]: Prettify<Omit<IfNever<PhantomCore.PhantomOf<T>>, S> & SetType<T>>;
|
|
418
412
|
};
|
|
419
413
|
/**
|
|
420
414
|
* Update '__OriginalType' if the passed type is changed.
|
|
421
415
|
*/
|
|
422
|
-
type HandleOriginalType<T> = Equals<Omit<PhantomCore.StripPhantom<T>, never>, Omit<T,
|
|
423
|
-
__Phantom: {
|
|
424
|
-
__OriginalType?: Omit<T,
|
|
416
|
+
type HandleOriginalType<T> = Equals<Omit<PhantomCore.StripPhantom<T>, never>, Omit<T, typeof __Phantom>> extends true ? T : Omit<T, typeof __Phantom> & PatchMetadata<T, {
|
|
417
|
+
[__Phantom]: {
|
|
418
|
+
[__OriginalType]?: Omit<T, typeof __Phantom>;
|
|
425
419
|
};
|
|
426
420
|
}>;
|
|
427
421
|
/**
|
|
@@ -435,61 +429,30 @@ type HandleOriginalType<T> = Equals<Omit<PhantomCore.StripPhantom<T>, never>, Om
|
|
|
435
429
|
* additional phantom metadata.
|
|
436
430
|
*/
|
|
437
431
|
type SetType<T> = T extends {
|
|
438
|
-
__Phantom: {
|
|
439
|
-
__OriginalType?: infer O;
|
|
432
|
+
[__Phantom]: {
|
|
433
|
+
[__OriginalType]?: infer O;
|
|
440
434
|
};
|
|
441
435
|
} ? {
|
|
442
|
-
__OriginalType?: O;
|
|
436
|
+
[__OriginalType]?: O;
|
|
443
437
|
} : {
|
|
444
|
-
__OriginalType?: T;
|
|
438
|
+
[__OriginalType]?: T;
|
|
445
439
|
};
|
|
446
440
|
/** --------------------------------------
|
|
447
441
|
* Generic helpers
|
|
448
442
|
* --------------------------------------- */
|
|
449
443
|
/** Prettify type */
|
|
450
444
|
type Prettify<T> = {
|
|
451
|
-
[K in keyof T]: K extends
|
|
445
|
+
[K in keyof T]: K extends typeof __Phantom | typeof __Traits ? Prettify<T[K]> : T[K];
|
|
452
446
|
} & {};
|
|
453
447
|
/** Check equality and returns true or false */
|
|
454
|
-
type Equals<A1
|
|
448
|
+
type Equals<A1, A2> = (<A>() => A extends A2 ? true : false) extends <A>() => A extends A1 ? true : false ? true : false;
|
|
455
449
|
/** Type to replace 'never' with another type */
|
|
456
450
|
type IfNever<T, R = {}> = [T] extends [never] ? R : T;
|
|
457
451
|
/** Get intersection from union */
|
|
458
|
-
type IntersectOf<U
|
|
452
|
+
type IntersectOf<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
459
453
|
/** Merge two objects. if matching keys between the two exists, the second object key value is used. */
|
|
460
454
|
type Merge<O1 extends object, O2 extends object> = Prettify<Omit<O1, keyof O2> & O2>;
|
|
461
455
|
|
|
462
|
-
/**
|
|
463
|
-
* Branding API.
|
|
464
|
-
*
|
|
465
|
-
* Brands provide nominal typing for otherwise identical values.
|
|
466
|
-
* A value may only be branded once.
|
|
467
|
-
*
|
|
468
|
-
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
469
|
-
*/
|
|
470
|
-
declare namespace BrandCore {
|
|
471
|
-
/** Type guard for any brand. */
|
|
472
|
-
export type Any = TagCore.Of<string | symbol>;
|
|
473
|
-
/** Declare a brand */
|
|
474
|
-
export type Declare<T extends string | symbol, L extends string = never> = Prettify<TagCore.Of<T> & LabelCore.OfIfExists<L>>;
|
|
475
|
-
/**
|
|
476
|
-
* Assign a brand to a value.
|
|
477
|
-
* Fails if the value is already branded.
|
|
478
|
-
*/
|
|
479
|
-
export type Assign<B extends Any, T> = T extends ErrorType<any> ? T : _Assign<B, HandleOriginalType<T>>;
|
|
480
|
-
/** Internal implementation of 'Brand.Assign' */
|
|
481
|
-
type _Assign<B extends Any, T> = TagCore.HasTag<T> extends true ? ErrorType<Errors<B, T>['alreadyBranded']> : WithMetadata<T, B>;
|
|
482
|
-
/**
|
|
483
|
-
* Assign a brand if possible, otherwise return the original type.
|
|
484
|
-
*/
|
|
485
|
-
export type AssignSafe<B extends Any, T> = T extends ErrorType<any> ? T : _AssignSafe<B, HandleOriginalType<T>>;
|
|
486
|
-
/** Internal implementation of 'Brand.AssignSafe' */
|
|
487
|
-
type _AssignSafe<B extends Any, T> = TagCore.HasTag<T> extends true ? T : WithMetadata<T, B>;
|
|
488
|
-
/** Check whether value is branded with */
|
|
489
|
-
export type isBrand<T, B extends Any> = T extends B ? true : false;
|
|
490
|
-
export { };
|
|
491
|
-
}
|
|
492
|
-
|
|
493
456
|
/**
|
|
494
457
|
* Identity API.
|
|
495
458
|
*
|
|
@@ -501,7 +464,7 @@ declare namespace IdentityCore {
|
|
|
501
464
|
/** Type guard for any identity. */
|
|
502
465
|
export type Any = TagCore.Of<string | symbol>;
|
|
503
466
|
/** Declare an identity */
|
|
504
|
-
export type Declare<T extends string | symbol, L extends string = never, B
|
|
467
|
+
export type Declare<T extends string | symbol, L extends string = never, B = never, V extends string = never> = IfNever<B, unknown> & Prettify<TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
505
468
|
/**
|
|
506
469
|
* Assign an identity to a value.
|
|
507
470
|
* Enforces base-type compatibility.
|
|
@@ -546,11 +509,11 @@ declare namespace TraitCore {
|
|
|
546
509
|
/** Remove a trait */
|
|
547
510
|
export type Drop<Tr extends Any, T> = T extends ErrorType<any> ? T : _Drop<Tr, HandleOriginalType<T>>;
|
|
548
511
|
/** Internal implementation of 'Trait.Drop' */
|
|
549
|
-
type _Drop<Tr extends Any, T> = Equals<TraitsCore.TraitKeysOf<Tr>, TraitsCore.TraitKeysOf<T>> extends true ? Equals<keyof PhantomCore.PhantomOf<T>,
|
|
512
|
+
type _Drop<Tr extends Any, T> = Equals<TraitsCore.TraitKeysOf<Tr>, TraitsCore.TraitKeysOf<T>> extends true ? Equals<keyof PhantomCore.PhantomOf<T>, typeof __OriginalType | typeof __Traits> extends true ? PhantomCore.StripPhantom<T> : WithoutMetadata<T, typeof __Traits> : WithMetadata<T, TraitsCore.FromMap<Omit<TraitsCore.TraitsOf<T>, TraitsCore.TraitKeysOf<Tr>>>>;
|
|
550
513
|
/** Remove multiple traits */
|
|
551
514
|
export type DropMulti<Tr extends readonly Any[], T> = T extends ErrorType<any> ? T : _DropMulti<Tr, HandleOriginalType<T>>;
|
|
552
515
|
/** Internal implementation of 'Trait.DropMulti' */
|
|
553
|
-
type _DropMulti<Tr extends readonly Any[], T> = Equals<TraitsCore.TraitKeysOf<IntersectOf<Tr[number]>>, TraitsCore.TraitKeysOf<T>> extends true ? Equals<keyof PhantomCore.PhantomOf<T>,
|
|
516
|
+
type _DropMulti<Tr extends readonly Any[], T> = Equals<TraitsCore.TraitKeysOf<IntersectOf<Tr[number]>>, TraitsCore.TraitKeysOf<T>> extends true ? Equals<keyof PhantomCore.PhantomOf<T>, typeof __OriginalType | typeof __Traits> extends true ? PhantomCore.StripPhantom<T> : WithoutMetadata<T, typeof __Traits> : WithMetadata<T, TraitsCore.FromMap<Omit<TraitsCore.TraitsOf<T>, TraitsCore.TraitKeysOf<IntersectOf<Tr[number]>>>>>;
|
|
554
517
|
/** Check whether value has trait */
|
|
555
518
|
export type HasTrait<T, Tr extends Any> = T extends Tr ? true : false;
|
|
556
519
|
export { };
|
|
@@ -566,7 +529,7 @@ declare namespace TransformationCore {
|
|
|
566
529
|
/** Type guard for any transformation. */
|
|
567
530
|
export type Any = InputCore.Of<any> & TagCore.Of<string | symbol>;
|
|
568
531
|
/** Declare a transformation */
|
|
569
|
-
export type Declare<I, T extends string | symbol, L extends string = never, B
|
|
532
|
+
export type Declare<I, T extends string | symbol, L extends string = never, B = never, V extends string = never> = IfNever<B, unknown> & Prettify<InputCore.Of<I> & TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
570
533
|
/**
|
|
571
534
|
* Apply a transformation to a value.
|
|
572
535
|
* Enforces base-type compatibility.
|
|
@@ -676,26 +639,6 @@ declare namespace Traits {
|
|
|
676
639
|
/** Check if any traits exist */
|
|
677
640
|
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
678
641
|
}
|
|
679
|
-
/**
|
|
680
|
-
* Branding API.
|
|
681
|
-
*
|
|
682
|
-
* Brands provide nominal typing for otherwise identical values.
|
|
683
|
-
* A value may only be branded once.
|
|
684
|
-
*
|
|
685
|
-
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
686
|
-
*/
|
|
687
|
-
declare namespace Brand {
|
|
688
|
-
/** Type guard for any brand. */
|
|
689
|
-
type Any = BrandCore.Any;
|
|
690
|
-
/** Declare a brand */
|
|
691
|
-
type Declare<T extends string | symbol, L extends string = never> = BrandCore.Declare<T, L>;
|
|
692
|
-
/** Assign a brand to a value. Fails if the value is already branded */
|
|
693
|
-
type Assign<B extends Any, T> = BrandCore.Assign<B, T>;
|
|
694
|
-
/** Assign a brand if possible, otherwise return the original type */
|
|
695
|
-
type AssignSafe<B extends Any, T> = BrandCore.AssignSafe<B, T>;
|
|
696
|
-
/** Check whether value is branded with */
|
|
697
|
-
type isBrand<T, B extends Any> = BrandCore.isBrand<T, B>;
|
|
698
|
-
}
|
|
699
642
|
/**
|
|
700
643
|
* Identity API.
|
|
701
644
|
*
|
|
@@ -707,7 +650,7 @@ declare namespace Identity {
|
|
|
707
650
|
/** Type guard for any identity. */
|
|
708
651
|
type Any = IdentityCore.Any;
|
|
709
652
|
/** Declare an identity */
|
|
710
|
-
type Declare<T extends string | symbol, L extends string = never, B
|
|
653
|
+
type Declare<T extends string | symbol, L extends string = never, B = never, V extends string = never> = IdentityCore.Declare<T, L, B, V>;
|
|
711
654
|
/** Assign an identity to a value. Enforces base-type compatibility */
|
|
712
655
|
type Assign<I extends Any, T> = IdentityCore.Assign<I, T>;
|
|
713
656
|
/** Safe identity assignment */
|
|
@@ -751,7 +694,7 @@ declare namespace Transformation {
|
|
|
751
694
|
/** Type guard for any transformation. */
|
|
752
695
|
type Any = TransformationCore.Any;
|
|
753
696
|
/** Declare a transformation */
|
|
754
|
-
type Declare<I, T extends string | symbol, L extends string = never, B
|
|
697
|
+
type Declare<I, T extends string | symbol, L extends string = never, B = never, V extends string = never> = TransformationCore.Declare<I, T, L, B, V>;
|
|
755
698
|
/** Apply a transformation to a value. Enforces base-type compatibility */
|
|
756
699
|
type Apply<Tr extends Any, I, T> = TransformationCore.Apply<Tr, I, T>;
|
|
757
700
|
/** Revert a transformation */
|
|
@@ -794,12 +737,6 @@ declare namespace Inspect {
|
|
|
794
737
|
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
795
738
|
/** Check if any traits exist */
|
|
796
739
|
type HasTraits<T> = TraitsCore.HasTraits<T>;
|
|
797
|
-
/**
|
|
798
|
-
* Check whether value is branded with
|
|
799
|
-
*
|
|
800
|
-
* @deprecated To unify Api surface 'isIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
801
|
-
*/
|
|
802
|
-
type isBrand<T, B extends BrandCore.Any> = BrandCore.isBrand<T, B>;
|
|
803
740
|
/** Check whether value is branded with */
|
|
804
741
|
type isIdentity<T, I extends IdentityCore.Any> = IdentityCore.isIdentity<T, I>;
|
|
805
742
|
/** Check whether value has trait */
|
|
@@ -808,20 +745,6 @@ declare namespace Inspect {
|
|
|
808
745
|
type isTransformed<T, Tr extends TransformationCore.Any> = TransformationCore.isTransformed<T, Tr>;
|
|
809
746
|
}
|
|
810
747
|
|
|
811
|
-
/**
|
|
812
|
-
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
813
|
-
*
|
|
814
|
-
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
815
|
-
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
816
|
-
* where you know the value is valid.
|
|
817
|
-
*
|
|
818
|
-
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
819
|
-
*
|
|
820
|
-
* @template B - The brand declaration to assign.
|
|
821
|
-
* @returns A function that casts any value to the branded type.
|
|
822
|
-
*/
|
|
823
|
-
declare const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
824
|
-
|
|
825
748
|
/**
|
|
826
749
|
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
827
750
|
*
|
|
@@ -1028,26 +951,6 @@ declare namespace Phantom {
|
|
|
1028
951
|
/** Check if any traits exist */
|
|
1029
952
|
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
1030
953
|
}
|
|
1031
|
-
/**
|
|
1032
|
-
* Branding API.
|
|
1033
|
-
*
|
|
1034
|
-
* Brands provide nominal typing for otherwise identical values.
|
|
1035
|
-
* A value may only be branded once.
|
|
1036
|
-
*
|
|
1037
|
-
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1038
|
-
*/
|
|
1039
|
-
namespace Brand {
|
|
1040
|
-
/** Type guard for any brand. */
|
|
1041
|
-
type Any = BrandCore.Any;
|
|
1042
|
-
/** Declare a brand */
|
|
1043
|
-
type Declare<T extends string | symbol, L extends string = never> = BrandCore.Declare<T, L>;
|
|
1044
|
-
/** Assign a brand to a value. Fails if the value is already branded */
|
|
1045
|
-
type Assign<B extends Any, T> = BrandCore.Assign<B, T>;
|
|
1046
|
-
/** Assign a brand if possible, otherwise return the original type */
|
|
1047
|
-
type AssignSafe<B extends Any, T> = BrandCore.AssignSafe<B, T>;
|
|
1048
|
-
/** Check whether value is branded with */
|
|
1049
|
-
type isBrand<T, B extends Brand.Any> = BrandCore.isBrand<T, B>;
|
|
1050
|
-
}
|
|
1051
954
|
/**
|
|
1052
955
|
* Identity API.
|
|
1053
956
|
*
|
|
@@ -1059,7 +962,7 @@ declare namespace Phantom {
|
|
|
1059
962
|
/** Type guard for any identity. */
|
|
1060
963
|
type Any = IdentityCore.Any;
|
|
1061
964
|
/** Declare an identity */
|
|
1062
|
-
type Declare<T extends string | symbol, L extends string = never, B
|
|
965
|
+
type Declare<T extends string | symbol, L extends string = never, B = never, V extends string = never> = IdentityCore.Declare<T, L, B, V>;
|
|
1063
966
|
/** Assign an identity to a value. Enforces base-type compatibility */
|
|
1064
967
|
type Assign<I extends Any, T> = IdentityCore.Assign<I, T>;
|
|
1065
968
|
/** Safe identity assignment */
|
|
@@ -1103,7 +1006,7 @@ declare namespace Phantom {
|
|
|
1103
1006
|
/** Type guard for any transformation. */
|
|
1104
1007
|
type Any = TransformationCore.Any;
|
|
1105
1008
|
/** Declare a transformation */
|
|
1106
|
-
type Declare<I, T extends string | symbol, L extends string = never, B
|
|
1009
|
+
type Declare<I, T extends string | symbol, L extends string = never, B = never, V extends string = never> = TransformationCore.Declare<I, T, L, B, V>;
|
|
1107
1010
|
/** Apply a transformation to a value. Enforces base-type compatibility */
|
|
1108
1011
|
type Apply<Tr extends Any, I, T> = TransformationCore.Apply<Tr, I, T>;
|
|
1109
1012
|
/** Revert a transformation */
|
|
@@ -1125,15 +1028,15 @@ declare namespace Phantom {
|
|
|
1125
1028
|
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1126
1029
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1127
1030
|
const stripPhantom: <T>(value: T) => T extends {
|
|
1128
|
-
__Phantom: {
|
|
1129
|
-
__OriginalType?: infer O;
|
|
1031
|
+
[__Phantom]: {
|
|
1032
|
+
[__OriginalType]?: infer O;
|
|
1130
1033
|
};
|
|
1131
1034
|
} ? Exclude<O, undefined> : T extends {
|
|
1132
|
-
__Phantom: {
|
|
1133
|
-
__Base?: infer B;
|
|
1035
|
+
[__Phantom]: {
|
|
1036
|
+
[__Base]?: infer B;
|
|
1134
1037
|
};
|
|
1135
1038
|
} ? Exclude<B, undefined> : T extends {
|
|
1136
|
-
__Phantom: object;
|
|
1039
|
+
[__Phantom]: object;
|
|
1137
1040
|
} ? unknown : T;
|
|
1138
1041
|
/** Extract the label */
|
|
1139
1042
|
type LabelOf<T> = LabelCore.LabelOf<T>;
|
|
@@ -1161,12 +1064,6 @@ declare namespace Phantom {
|
|
|
1161
1064
|
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
1162
1065
|
/** Check if any traits exist */
|
|
1163
1066
|
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
1164
|
-
/**
|
|
1165
|
-
* Check whether value is branded with
|
|
1166
|
-
*
|
|
1167
|
-
* @deprecated To unify Api surface 'isIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1168
|
-
*/
|
|
1169
|
-
type isBrand<T, B extends Brand.Any> = BrandCore.isBrand<T, B>;
|
|
1170
1067
|
/** Check whether value is branded with */
|
|
1171
1068
|
type isIdentity<T, I extends Identity.Any> = IdentityCore.isIdentity<T, I>;
|
|
1172
1069
|
/** Check whether value has trait */
|
|
@@ -1178,19 +1075,6 @@ declare namespace Phantom {
|
|
|
1178
1075
|
* assertors
|
|
1179
1076
|
* --------------------------------------- */
|
|
1180
1077
|
namespace assertors {
|
|
1181
|
-
/**
|
|
1182
|
-
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
1183
|
-
*
|
|
1184
|
-
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
1185
|
-
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1186
|
-
* where you know the value is valid.
|
|
1187
|
-
*
|
|
1188
|
-
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1189
|
-
*
|
|
1190
|
-
* @template B - The brand declaration to assign.
|
|
1191
|
-
* @returns A function that casts any value to the branded type.
|
|
1192
|
-
*/
|
|
1193
|
-
const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
1194
1078
|
/**
|
|
1195
1079
|
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
1196
1080
|
*
|
|
@@ -1264,19 +1148,6 @@ declare namespace Phantom {
|
|
|
1264
1148
|
*/
|
|
1265
1149
|
const revertTransformation: <Tr extends Transformation.Any>() => <T, I>(transformed: T, input: I) => Transformation.Revert<Tr, T, I>;
|
|
1266
1150
|
}
|
|
1267
|
-
/**
|
|
1268
|
-
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
1269
|
-
*
|
|
1270
|
-
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
1271
|
-
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1272
|
-
* where you know the value is valid.
|
|
1273
|
-
*
|
|
1274
|
-
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1275
|
-
*
|
|
1276
|
-
* @template B - The brand declaration to assign.
|
|
1277
|
-
* @returns A function that casts any value to the branded type.
|
|
1278
|
-
*/
|
|
1279
|
-
const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
1280
1151
|
/**
|
|
1281
1152
|
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
1282
1153
|
*
|
|
@@ -1358,15 +1229,15 @@ declare namespace Phantom {
|
|
|
1358
1229
|
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1359
1230
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1360
1231
|
const stripPhantom: <T>(value: T) => T extends {
|
|
1361
|
-
__Phantom: {
|
|
1362
|
-
__OriginalType?: infer O;
|
|
1232
|
+
[__Phantom]: {
|
|
1233
|
+
[__OriginalType]?: infer O;
|
|
1363
1234
|
};
|
|
1364
1235
|
} ? Exclude<O, undefined> : T extends {
|
|
1365
|
-
__Phantom: {
|
|
1366
|
-
__Base?: infer B;
|
|
1236
|
+
[__Phantom]: {
|
|
1237
|
+
[__Base]?: infer B;
|
|
1367
1238
|
};
|
|
1368
1239
|
} ? Exclude<B, undefined> : T extends {
|
|
1369
|
-
__Phantom: object;
|
|
1240
|
+
[__Phantom]: object;
|
|
1370
1241
|
} ? unknown : T;
|
|
1371
1242
|
/** --------------------------------------
|
|
1372
1243
|
* Error type
|
|
@@ -1404,4 +1275,4 @@ declare namespace Phantom {
|
|
|
1404
1275
|
}
|
|
1405
1276
|
}
|
|
1406
1277
|
|
|
1407
|
-
export { Base,
|
|
1278
|
+
export { Base, type ErrorType, Identity, Input, Inspect, Label, Phantom, PhantomChain, PhantomCore, Tag, Trait, Traits, Transformation, Variants, __Base, __Input, __Label, __OriginalType, __Phantom, __Tag, __Traits, __Variants, addTrait, addTraits, applyTransformation, asIdentity, assertors, Phantom as default, dropTrait, dropTraits, revertTransformation, stripPhantom };
|