@vicin/phantom 1.0.2 → 1.0.5
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 +18 -3
- package/dist/index.d.mts +1353 -0
- package/dist/index.d.ts +1353 -7
- package/dist/index.global.js +135 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +129 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +117 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +6 -2
- package/dist/assertors/assertors.d.ts +0 -86
- package/dist/assertors/assertors.d.ts.map +0 -1
- package/dist/assertors/assertors.js +0 -91
- package/dist/assertors/assertors.js.map +0 -1
- package/dist/assertors/brand.d.ts +0 -13
- package/dist/assertors/brand.d.ts.map +0 -1
- package/dist/assertors/brand.js +0 -12
- package/dist/assertors/brand.js.map +0 -1
- package/dist/assertors/identity.d.ts +0 -13
- package/dist/assertors/identity.d.ts.map +0 -1
- package/dist/assertors/identity.js +0 -12
- package/dist/assertors/identity.js.map +0 -1
- package/dist/assertors/index.d.ts +0 -2
- package/dist/assertors/index.d.ts.map +0 -1
- package/dist/assertors/index.js +0 -2
- package/dist/assertors/index.js.map +0 -1
- package/dist/assertors/trait.d.ts +0 -38
- package/dist/assertors/trait.d.ts.map +0 -1
- package/dist/assertors/trait.js +0 -37
- package/dist/assertors/trait.js.map +0 -1
- package/dist/assertors/transformation.d.ts +0 -27
- package/dist/assertors/transformation.d.ts.map +0 -1
- package/dist/assertors/transformation.js +0 -26
- package/dist/assertors/transformation.js.map +0 -1
- package/dist/chain/chain.d.ts +0 -42
- package/dist/chain/chain.d.ts.map +0 -1
- package/dist/chain/chain.js +0 -48
- package/dist/chain/chain.js.map +0 -1
- package/dist/chain/index.d.ts +0 -2
- package/dist/chain/index.d.ts.map +0 -1
- package/dist/chain/index.js +0 -2
- package/dist/chain/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/phantom.d.ts +0 -352
- package/dist/phantom.d.ts.map +0 -1
- package/dist/phantom.js +0 -139
- package/dist/phantom.js.map +0 -1
- package/dist/types/composed/brand.d.ts +0 -32
- package/dist/types/composed/brand.d.ts.map +0 -1
- package/dist/types/composed/brand.js +0 -2
- package/dist/types/composed/brand.js.map +0 -1
- package/dist/types/composed/helpers.d.ts +0 -75
- package/dist/types/composed/helpers.d.ts.map +0 -1
- package/dist/types/composed/helpers.js +0 -2
- package/dist/types/composed/helpers.js.map +0 -1
- package/dist/types/composed/identity.d.ts +0 -37
- package/dist/types/composed/identity.d.ts.map +0 -1
- package/dist/types/composed/identity.js +0 -2
- package/dist/types/composed/identity.js.map +0 -1
- package/dist/types/composed/index.d.ts +0 -6
- package/dist/types/composed/index.d.ts.map +0 -1
- package/dist/types/composed/index.js +0 -2
- package/dist/types/composed/index.js.map +0 -1
- package/dist/types/composed/trait.d.ts +0 -35
- package/dist/types/composed/trait.d.ts.map +0 -1
- package/dist/types/composed/trait.js +0 -2
- package/dist/types/composed/trait.js.map +0 -1
- package/dist/types/composed/transformation.d.ts +0 -34
- package/dist/types/composed/transformation.d.ts.map +0 -1
- package/dist/types/composed/transformation.js +0 -2
- package/dist/types/composed/transformation.js.map +0 -1
- package/dist/types/core/base.d.ts +0 -35
- package/dist/types/core/base.d.ts.map +0 -1
- package/dist/types/core/base.js +0 -2
- package/dist/types/core/base.js.map +0 -1
- package/dist/types/core/index.d.ts +0 -8
- package/dist/types/core/index.d.ts.map +0 -1
- package/dist/types/core/index.js +0 -2
- package/dist/types/core/index.js.map +0 -1
- package/dist/types/core/input.d.ts +0 -38
- package/dist/types/core/input.d.ts.map +0 -1
- package/dist/types/core/input.js +0 -2
- package/dist/types/core/input.js.map +0 -1
- package/dist/types/core/label.d.ts +0 -34
- package/dist/types/core/label.d.ts.map +0 -1
- package/dist/types/core/label.js +0 -2
- package/dist/types/core/label.js.map +0 -1
- package/dist/types/core/phantom.d.ts +0 -20
- package/dist/types/core/phantom.d.ts.map +0 -1
- package/dist/types/core/phantom.js +0 -11
- package/dist/types/core/phantom.js.map +0 -1
- package/dist/types/core/tag.d.ts +0 -35
- package/dist/types/core/tag.d.ts.map +0 -1
- package/dist/types/core/tag.js +0 -2
- package/dist/types/core/tag.js.map +0 -1
- package/dist/types/core/traits.d.ts +0 -51
- package/dist/types/core/traits.d.ts.map +0 -1
- package/dist/types/core/traits.js +0 -2
- package/dist/types/core/traits.js.map +0 -1
- package/dist/types/core/variants.d.ts +0 -34
- package/dist/types/core/variants.d.ts.map +0 -1
- package/dist/types/core/variants.js +0 -2
- package/dist/types/core/variants.js.map +0 -1
- package/dist/types/errors.d.ts +0 -40
- package/dist/types/errors.d.ts.map +0 -1
- package/dist/types/errors.js +0 -2
- package/dist/types/errors.js.map +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -2
- package/dist/types/index.js.map +0 -1
- package/dist/types/public.d.ts +0 -224
- package/dist/types/public.d.ts.map +0 -1
- package/dist/types/public.js +0 -12
- package/dist/types/public.js.map +0 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,1353 @@
|
|
|
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
|
+
* @template B - The brand declaration to assign.
|
|
10
|
+
* @returns A function that casts any value to the branded type.
|
|
11
|
+
*/
|
|
12
|
+
const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
15
|
+
*
|
|
16
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
17
|
+
* with the identity's nominal type applied. Use it when you know a value
|
|
18
|
+
* conforms to an identity but need to assert it for the type system.
|
|
19
|
+
*
|
|
20
|
+
* @template I - The identity declaration to assign.
|
|
21
|
+
* @returns A function that casts any value to the assigned identity type.
|
|
22
|
+
*/
|
|
23
|
+
const asIdentity: <I extends Identity.Any>() => </**
|
|
24
|
+
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
25
|
+
*
|
|
26
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
27
|
+
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
28
|
+
* where you know the value is valid.
|
|
29
|
+
*
|
|
30
|
+
* @template B - The brand declaration to assign.
|
|
31
|
+
* @returns A function that casts any value to the branded type.
|
|
32
|
+
*/ V>(value: V) => Identity.Assign<I, V>;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a typed caster that adds a single {@link Trait} to a value.
|
|
35
|
+
*
|
|
36
|
+
* Zero-runtime-cost assertion helper.
|
|
37
|
+
*
|
|
38
|
+
* @template Tr - The trait declaration to add.
|
|
39
|
+
* @returns A function that adds the trait to any value.
|
|
40
|
+
*/
|
|
41
|
+
const addTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Add<Tr, V>;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a typed caster that adds multiple {@link Trait}s to a value.
|
|
44
|
+
*
|
|
45
|
+
* Zero-runtime-cost assertion helper.
|
|
46
|
+
*
|
|
47
|
+
* @template Tr - Tuple of trait declarations to add.
|
|
48
|
+
* @returns A function that adds all traits to any value.
|
|
49
|
+
*/
|
|
50
|
+
const addTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.AddMulti<Tr, V>;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a typed caster that removes a single {@link Trait} from a value.
|
|
53
|
+
*
|
|
54
|
+
* Zero-runtime-cost assertion helper.
|
|
55
|
+
*
|
|
56
|
+
* @template Tr - The trait declaration to remove.
|
|
57
|
+
* @returns A function that drops the trait from any value.
|
|
58
|
+
*/
|
|
59
|
+
const dropTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Drop<Tr, V>;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a typed caster that removes multiple {@link Trait}s from a value.
|
|
62
|
+
*
|
|
63
|
+
* Zero-runtime-cost assertion helper.
|
|
64
|
+
*
|
|
65
|
+
* @template Tr - Tuple of trait declarations to remove.
|
|
66
|
+
* @returns A function that drops all specified traits from any value.
|
|
67
|
+
*/
|
|
68
|
+
const dropTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.DropMulti<Tr, V>;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a typed applicator for a {@link Transformation}.
|
|
71
|
+
*
|
|
72
|
+
* Use this for "forward" operations (e.g., encrypt, encode, wrap).
|
|
73
|
+
* The `input` parameter is only used for type inference — it is not used at runtime.
|
|
74
|
+
*
|
|
75
|
+
* Zero-runtime-cost assertion helper.
|
|
76
|
+
*
|
|
77
|
+
* @template Tr - The transformation declaration.
|
|
78
|
+
* @returns A function that applies the transformation while preserving the input type for later revert.
|
|
79
|
+
*/
|
|
80
|
+
const applyTransformation: <Tr extends Transformation.Any>() => <I, T>(input: I, transformed: T) => Transformation.Apply<Tr, I, T>;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a typed reverter for a {@link Transformation}.
|
|
83
|
+
*
|
|
84
|
+
* Use this for "reverse" operations (e.g., decrypt, decode, unwrap).
|
|
85
|
+
* The `transformed` parameter is used for type inference of the expected input,
|
|
86
|
+
* and `input` is the computed result that must match the stored input type.
|
|
87
|
+
*
|
|
88
|
+
* Zero-runtime-cost assertion helper.
|
|
89
|
+
*
|
|
90
|
+
* @template Tr - The transformation declaration.
|
|
91
|
+
* @returns A function that reverts the transformation, stripping phantom metadata.
|
|
92
|
+
*/
|
|
93
|
+
const revertTransformation: <Tr extends Transformation.Any>() => <T, I>(transformed: T, input: I) => Transformation.Revert<Tr, T, I>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Base-type metadata.
|
|
98
|
+
*
|
|
99
|
+
* Used to constrain which runtime types a brand, identity,
|
|
100
|
+
* or transformation may be applied to.
|
|
101
|
+
*/
|
|
102
|
+
declare namespace BaseCore {
|
|
103
|
+
/** Marker type for base constraints */
|
|
104
|
+
type Any = {
|
|
105
|
+
__Phantom: {
|
|
106
|
+
__Base?: unknown;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
/** Declare a base type */
|
|
110
|
+
type Of<Base> = {
|
|
111
|
+
__Phantom: {
|
|
112
|
+
__Base?: Base;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
/** Conditionally declare a base type */
|
|
116
|
+
type OfIfExists<Base> = [Base] extends [never] ? {} : Of<Base>;
|
|
117
|
+
/** Extract the base type */
|
|
118
|
+
type BaseOf<T> = T extends {
|
|
119
|
+
__Phantom: {
|
|
120
|
+
__Base?: infer Base;
|
|
121
|
+
};
|
|
122
|
+
} ? Exclude<Base, undefined> : unknown;
|
|
123
|
+
/** Check whether a base constraint exists */
|
|
124
|
+
type HasBase<T, B = unknown> = T extends {
|
|
125
|
+
__Phantom: {
|
|
126
|
+
__Base?: B;
|
|
127
|
+
};
|
|
128
|
+
} ? true : false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Input metadata.
|
|
133
|
+
*
|
|
134
|
+
* Utilities for attaching and querying input metadata in transformations.
|
|
135
|
+
*/
|
|
136
|
+
declare namespace InputCore {
|
|
137
|
+
/** Marker type for input value */
|
|
138
|
+
type Any = {
|
|
139
|
+
__Phantom: {
|
|
140
|
+
__Input: unknown;
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
/** Set input */
|
|
144
|
+
type Of<Input> = {
|
|
145
|
+
__Phantom: {
|
|
146
|
+
__Input: Input;
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
/** Conditionally set input */
|
|
150
|
+
type OfIfExists<Input> = [Input] extends [never] ? {} : {
|
|
151
|
+
__Phantom: {
|
|
152
|
+
__Input: Input;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
/** Extract the input */
|
|
156
|
+
type InputOf<T> = T extends {
|
|
157
|
+
__Phantom: {
|
|
158
|
+
__Input: infer Input;
|
|
159
|
+
};
|
|
160
|
+
} ? Input : never;
|
|
161
|
+
/** Check whether an input exists */
|
|
162
|
+
type HasInput<T, I = unknown> = T extends {
|
|
163
|
+
__Phantom: {
|
|
164
|
+
__Input: I;
|
|
165
|
+
};
|
|
166
|
+
} ? true : false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Optional human-readable label metadata.
|
|
171
|
+
*
|
|
172
|
+
* Labels are descriptive only and do not affect identity.
|
|
173
|
+
*/
|
|
174
|
+
declare namespace LabelCore {
|
|
175
|
+
/** Marker type for labeled values */
|
|
176
|
+
type Any = {
|
|
177
|
+
__Phantom: {
|
|
178
|
+
__Label?: string;
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
/** Attach a label */
|
|
182
|
+
type Of<Label extends string> = {
|
|
183
|
+
__Phantom: {
|
|
184
|
+
__Label?: Label;
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
/** Conditionally attach a label */
|
|
188
|
+
type OfIfExists<Label extends string> = [Label] extends [never] ? {} : Of<Label>;
|
|
189
|
+
/** Extract the label */
|
|
190
|
+
type LabelOf<T> = T extends {
|
|
191
|
+
__Phantom: {
|
|
192
|
+
__Label?: infer Label;
|
|
193
|
+
};
|
|
194
|
+
} ? Exclude<Label, undefined> : never;
|
|
195
|
+
/** Check whether a label exists */
|
|
196
|
+
type HasLabel<T, L extends string = string> = T extends {
|
|
197
|
+
__Phantom: {
|
|
198
|
+
__Label?: L;
|
|
199
|
+
};
|
|
200
|
+
} ? true : false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Nominal tag metadata.
|
|
205
|
+
*
|
|
206
|
+
* Tags uniquely identify a branded or identified type.
|
|
207
|
+
* A value may only have a single tag.
|
|
208
|
+
*/
|
|
209
|
+
declare namespace TagCore {
|
|
210
|
+
/** Marker type for any tagged value */
|
|
211
|
+
type Any = {
|
|
212
|
+
__Phantom: {
|
|
213
|
+
__Tag: string | symbol;
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
/** Attach a specific tag */
|
|
217
|
+
type Of<Tag extends string | symbol> = {
|
|
218
|
+
__Phantom: {
|
|
219
|
+
__Tag: Tag;
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
/** Conditionally attach a tag if it exists */
|
|
223
|
+
type OfIfExists<Tag extends string | symbol> = [Tag] extends [never] ? {} : Of<Tag>;
|
|
224
|
+
/** Extract the tag from a type */
|
|
225
|
+
type TagOf<T> = T extends {
|
|
226
|
+
__Phantom: {
|
|
227
|
+
__Tag: infer Tag;
|
|
228
|
+
};
|
|
229
|
+
} ? Tag : never;
|
|
230
|
+
/** Check whether a type is tagged */
|
|
231
|
+
type HasTag<T, Ta extends string | symbol = string | symbol> = T extends {
|
|
232
|
+
__Phantom: {
|
|
233
|
+
__Tag: Ta;
|
|
234
|
+
};
|
|
235
|
+
} ? true : false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Trait metadata.
|
|
240
|
+
*
|
|
241
|
+
* Traits behave like a set of capabilities that can be
|
|
242
|
+
* added or removed independently.
|
|
243
|
+
*/
|
|
244
|
+
declare namespace TraitsCore {
|
|
245
|
+
/** Marker type for trait-bearing values */
|
|
246
|
+
type Any = {
|
|
247
|
+
__Phantom: {
|
|
248
|
+
__Traits: {};
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
/** Declare a single trait */
|
|
252
|
+
type Of<Trait extends string | symbol> = {
|
|
253
|
+
__Phantom: {
|
|
254
|
+
__Traits: {
|
|
255
|
+
[K in Trait]: true;
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
/** Construct traits from an explicit trait map */
|
|
260
|
+
type FromMap<Traits extends Record<string | symbol, true>> = {
|
|
261
|
+
__Phantom: {
|
|
262
|
+
__Traits: Traits;
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
/** Conditionally declare a trait */
|
|
266
|
+
type OfIfExists<Trait extends string | symbol> = [Trait] extends [
|
|
267
|
+
never
|
|
268
|
+
] ? {} : Of<Trait>;
|
|
269
|
+
/** Extract the trait map */
|
|
270
|
+
type TraitsOf<T> = T extends {
|
|
271
|
+
__Phantom: {
|
|
272
|
+
__Traits: infer Traits;
|
|
273
|
+
};
|
|
274
|
+
} ? Traits extends Record<string | symbol, true> ? Traits : never : never;
|
|
275
|
+
/** Extract trait keys */
|
|
276
|
+
type TraitKeysOf<T> = T extends {
|
|
277
|
+
__Phantom: {
|
|
278
|
+
__Traits: infer Traits;
|
|
279
|
+
};
|
|
280
|
+
} ? keyof Traits : never;
|
|
281
|
+
/** Check if any traits exist */
|
|
282
|
+
type HasTraits<T, Tr extends string | symbol = string | symbol> = T extends {
|
|
283
|
+
__Phantom: {
|
|
284
|
+
__Traits: Record<Tr, true>;
|
|
285
|
+
};
|
|
286
|
+
} ? true : false;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Variant metadata.
|
|
291
|
+
*
|
|
292
|
+
* Variants represent mutually exclusive states of a type.
|
|
293
|
+
*/
|
|
294
|
+
declare namespace VariantsCore {
|
|
295
|
+
/** Marker type for variant-bearing values */
|
|
296
|
+
type Any = {
|
|
297
|
+
__Phantom: {
|
|
298
|
+
__Variants: string;
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
/** Declare allowed variants */
|
|
302
|
+
type Of<Variants extends string> = {
|
|
303
|
+
__Phantom: {
|
|
304
|
+
__Variants: Variants;
|
|
305
|
+
};
|
|
306
|
+
};
|
|
307
|
+
/** Conditionally declare variants */
|
|
308
|
+
type OfIfExists<Variants extends string> = [Variants] extends [never] ? {} : Of<Variants>;
|
|
309
|
+
/** Extract variant union */
|
|
310
|
+
type VariantsOf<T> = T extends {
|
|
311
|
+
__Phantom: {
|
|
312
|
+
__Variants: infer Variants;
|
|
313
|
+
};
|
|
314
|
+
} ? Variants extends string ? Variants : never : never;
|
|
315
|
+
/** Check whether variants exist */
|
|
316
|
+
type HasVariants<T> = T extends {
|
|
317
|
+
__Phantom: {
|
|
318
|
+
__Variants: string;
|
|
319
|
+
};
|
|
320
|
+
} ? true : false;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Phantom meatadata object manipulators.
|
|
325
|
+
*
|
|
326
|
+
* Phantom matadata object holds all metadata used by 'phantom'.
|
|
327
|
+
*/
|
|
328
|
+
declare namespace PhantomCore {
|
|
329
|
+
/** Get phantom metadata object from a type */
|
|
330
|
+
type PhantomOf<T> = T extends {
|
|
331
|
+
__Phantom: infer Phantom extends object;
|
|
332
|
+
} ? Phantom : never;
|
|
333
|
+
/** Stip phantom metadata object from a type */
|
|
334
|
+
type StripPhantom<T> = T extends {
|
|
335
|
+
__Phantom: {
|
|
336
|
+
__OriginalType?: infer O;
|
|
337
|
+
};
|
|
338
|
+
} ? Exclude<O, undefined> : T;
|
|
339
|
+
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
340
|
+
const stripPhantom: <T>(value: T) => StripPhantom<T>;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Interface of 'Brand' and 'Identity' errors. */
|
|
344
|
+
interface Errors<I, T> {
|
|
345
|
+
alreadyBranded: {
|
|
346
|
+
code: 'ALREADY_BRANDED';
|
|
347
|
+
message: 'Type already branded';
|
|
348
|
+
context: {
|
|
349
|
+
type: T;
|
|
350
|
+
};
|
|
351
|
+
};
|
|
352
|
+
typeNotExtendBase: {
|
|
353
|
+
code: 'TYPE_NOT_EXTEND_BASE';
|
|
354
|
+
message: 'Type not extend base';
|
|
355
|
+
context: {
|
|
356
|
+
type: PhantomCore.StripPhantom<T>;
|
|
357
|
+
base: BaseCore.BaseOf<I>;
|
|
358
|
+
};
|
|
359
|
+
};
|
|
360
|
+
transformationMismatch: {
|
|
361
|
+
code: 'TRANSFORMATION_MISMATCH';
|
|
362
|
+
message: 'Transformation mismatch';
|
|
363
|
+
context: {
|
|
364
|
+
type: T;
|
|
365
|
+
transformation: I;
|
|
366
|
+
};
|
|
367
|
+
};
|
|
368
|
+
notTransformed: {
|
|
369
|
+
code: 'NOT_TRANSFORMED';
|
|
370
|
+
message: 'Type is not transformed';
|
|
371
|
+
context: {
|
|
372
|
+
type: T;
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
declare const __ErrorType: unique symbol;
|
|
377
|
+
/** Unique Error type for rules validation in phantom. */
|
|
378
|
+
type ErrorType<E> = {
|
|
379
|
+
[__ErrorType]: E;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/** --------------------------------------
|
|
383
|
+
* PhantomCore type helpers
|
|
384
|
+
* --------------------------------------- */
|
|
385
|
+
/**
|
|
386
|
+
* Attach or override phantom metadata while preserving
|
|
387
|
+
* the original runtime type.
|
|
388
|
+
*/
|
|
389
|
+
type WithMetadata<T, Change extends object> = PhantomCore.StripPhantom<T> & {
|
|
390
|
+
__Phantom: Prettify<Merge<IfNever<PhantomCore.PhantomOf<T>>, Omit<IfNever<PhantomCore.PhantomOf<Change>>, '__Base'> & SetType<T>>>;
|
|
391
|
+
};
|
|
392
|
+
/**
|
|
393
|
+
* Patch phantom metadata without touching the original type.
|
|
394
|
+
* Used when mutating metadata declarations themselves.
|
|
395
|
+
*/
|
|
396
|
+
type PatchMetadata<T, Change extends object> = {
|
|
397
|
+
__Phantom: Prettify<Merge<IfNever<PhantomCore.PhantomOf<T>>, IfNever<PhantomCore.PhantomOf<Change>>>>;
|
|
398
|
+
};
|
|
399
|
+
/**
|
|
400
|
+
* Remove a metadata dimension while preserving the original type.
|
|
401
|
+
*/
|
|
402
|
+
type WithoutMetadata<T, S extends string> = PhantomCore.StripPhantom<T> & {
|
|
403
|
+
__Phantom: Prettify<Omit<IfNever<PhantomCore.PhantomOf<T>>, S> & SetType<T>>;
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* Update '__OriginalType' if the passed type is changed.
|
|
407
|
+
*/
|
|
408
|
+
type HandleOriginalType<T> = Equals<Omit<PhantomCore.StripPhantom<T>, never>, Omit<T, '__Phantom'>> extends true ? T : Omit<T, '__Phantom'> & PatchMetadata<T, {
|
|
409
|
+
__Phantom: {
|
|
410
|
+
__OriginalType?: Omit<T, '__Phantom'>;
|
|
411
|
+
};
|
|
412
|
+
}>;
|
|
413
|
+
/**
|
|
414
|
+
* Attaches or updates the original runtime type metadata
|
|
415
|
+
* for a nominal value.
|
|
416
|
+
*
|
|
417
|
+
* If the type already carries an `__OriginalType`, it is preserved.
|
|
418
|
+
* Otherwise, the provided type is stored as the original type.
|
|
419
|
+
*
|
|
420
|
+
* This is used to maintain a stable runtime type while layering
|
|
421
|
+
* additional phantom metadata.
|
|
422
|
+
*/
|
|
423
|
+
type SetType<T> = T extends {
|
|
424
|
+
__Phantom: {
|
|
425
|
+
__OriginalType?: infer O;
|
|
426
|
+
};
|
|
427
|
+
} ? {
|
|
428
|
+
__OriginalType?: O;
|
|
429
|
+
} : {
|
|
430
|
+
__OriginalType?: T;
|
|
431
|
+
};
|
|
432
|
+
/** --------------------------------------
|
|
433
|
+
* Generic helpers
|
|
434
|
+
* --------------------------------------- */
|
|
435
|
+
/** Prettify type */
|
|
436
|
+
type Prettify<T> = {
|
|
437
|
+
[K in keyof T]: K extends '__Phantom' | '__Traits' ? Prettify<T[K]> : T[K];
|
|
438
|
+
} & {};
|
|
439
|
+
/** Check equality and returns true or false */
|
|
440
|
+
type Equals<A1 extends any, A2 extends any> = (<A>() => A extends A2 ? true : false) extends <A>() => A extends A1 ? true : false ? true : false;
|
|
441
|
+
/** Type to replace 'never' with another type */
|
|
442
|
+
type IfNever<T, R = {}> = [T] extends [never] ? R : T;
|
|
443
|
+
/** Get intersection from union */
|
|
444
|
+
type IntersectOf<U extends any> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
445
|
+
/** Merge two objects. if matching keys between the two exists, the second object key value is used. */
|
|
446
|
+
type Merge<O1 extends object, O2 extends object> = Prettify<Omit<O1, keyof O2> & O2>;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Branding API.
|
|
450
|
+
*
|
|
451
|
+
* Brands provide nominal typing for otherwise identical values.
|
|
452
|
+
* A value may only be branded once.
|
|
453
|
+
*/
|
|
454
|
+
declare namespace BrandCore {
|
|
455
|
+
/** Type guard for any brand. */
|
|
456
|
+
export type Any = TagCore.Of<string | symbol>;
|
|
457
|
+
/** Declare a brand */
|
|
458
|
+
export type Declare<T extends string | symbol, L extends string = never> = Prettify<TagCore.Of<T> & LabelCore.OfIfExists<L>>;
|
|
459
|
+
/**
|
|
460
|
+
* Assign a brand to a value.
|
|
461
|
+
* Fails if the value is already branded.
|
|
462
|
+
*/
|
|
463
|
+
export type Assign<B extends Any, T> = T extends ErrorType<any> ? T : _Assign<B, HandleOriginalType<T>>;
|
|
464
|
+
/** Internal implementation of 'Brand.Assign' */
|
|
465
|
+
type _Assign<B extends Any, T> = TagCore.HasTag<T> extends true ? ErrorType<Errors<B, T>['alreadyBranded']> : WithMetadata<T, B>;
|
|
466
|
+
/**
|
|
467
|
+
* Assign a brand if possible, otherwise return the original type.
|
|
468
|
+
*/
|
|
469
|
+
export type AssignSafe<B extends Any, T> = T extends ErrorType<any> ? T : _AssignSafe<B, HandleOriginalType<T>>;
|
|
470
|
+
/** Internal implementation of 'Brand.AssignSafe' */
|
|
471
|
+
type _AssignSafe<B extends Any, T> = TagCore.HasTag<T> extends true ? T : WithMetadata<T, B>;
|
|
472
|
+
/** Check whether value is branded with */
|
|
473
|
+
export type isBrand<T, B extends Any> = T extends B ? true : false;
|
|
474
|
+
export { };
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Identity API.
|
|
479
|
+
*
|
|
480
|
+
* Identities are brands with additional constraints:
|
|
481
|
+
* - Base type
|
|
482
|
+
* - Variants
|
|
483
|
+
*/
|
|
484
|
+
declare namespace IdentityCore {
|
|
485
|
+
/** Type guard for any identity. */
|
|
486
|
+
export type Any = TagCore.Of<string | symbol>;
|
|
487
|
+
/** Declare an identity */
|
|
488
|
+
export type Declare<T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = Prettify<TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
489
|
+
/**
|
|
490
|
+
* Assign an identity to a value.
|
|
491
|
+
* Enforces base-type compatibility.
|
|
492
|
+
*/
|
|
493
|
+
export type Assign<I extends Any, T> = T extends ErrorType<any> ? T : _Assign<I, HandleOriginalType<T>>;
|
|
494
|
+
/** Internal implementation of 'Identity.Assign' */
|
|
495
|
+
type _Assign<I extends Any, T> = TagCore.HasTag<T> extends true ? ErrorType<Errors<I, T>['alreadyBranded']> : T extends BaseCore.BaseOf<I> ? WithMetadata<T, I> : ErrorType<Errors<I, T>['typeNotExtendBase']>;
|
|
496
|
+
/** Safe identity assignment */
|
|
497
|
+
export type AssignSafe<I extends Any, T> = T extends ErrorType<any> ? T : _AssignSafe<I, HandleOriginalType<T>>;
|
|
498
|
+
/** Internal implementation of 'Identity.AssignSafe' */
|
|
499
|
+
type _AssignSafe<I extends Any, T> = TagCore.HasTag<T> extends true ? T : T extends BaseCore.BaseOf<I> ? WithMetadata<T, I> : ErrorType<Errors<I, T>['typeNotExtendBase']>;
|
|
500
|
+
/** Set the active variant on an identity */
|
|
501
|
+
export type WithVariant<I extends Any, V extends VariantsCore.VariantsOf<I>> = PatchMetadata<I, VariantsCore.Of<V>>;
|
|
502
|
+
/** Set the active variant on a value */
|
|
503
|
+
export type WithTypeVariant<T, V extends VariantsCore.VariantsOf<T>> = T extends ErrorType<any> ? T : _WithTypeVariant<HandleOriginalType<T>, V>;
|
|
504
|
+
/** Internal implementation of 'Identity.WithTypeVariant' */
|
|
505
|
+
type _WithTypeVariant<T, V extends VariantsCore.VariantsOf<T>> = WithMetadata<T, VariantsCore.Of<V>>;
|
|
506
|
+
/** Check whether value is branded with */
|
|
507
|
+
export type isIdentity<T, I extends Any> = T extends I ? true : false;
|
|
508
|
+
export { };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Trait API.
|
|
513
|
+
*
|
|
514
|
+
* Traits are additive capabilities that can be attached
|
|
515
|
+
* or removed independently.
|
|
516
|
+
*/
|
|
517
|
+
declare namespace TraitCore {
|
|
518
|
+
/** Type guard for any trait. */
|
|
519
|
+
export type Any = TraitsCore.Of<string | symbol>;
|
|
520
|
+
/** Declare a trait */
|
|
521
|
+
export type Declare<Tr extends string | symbol> = TraitsCore.Of<Tr>;
|
|
522
|
+
/** Add a trait */
|
|
523
|
+
export type Add<Tr extends Any, T> = T extends ErrorType<any> ? T : _Add<Tr, HandleOriginalType<T>>;
|
|
524
|
+
/** Internal implementation of 'Trait.Add' */
|
|
525
|
+
type _Add<Tr extends Any, T> = WithMetadata<T, TraitsCore.FromMap<IfNever<TraitsCore.TraitsOf<T>> & IfNever<TraitsCore.TraitsOf<Tr>>>>;
|
|
526
|
+
/** Add multiple traits */
|
|
527
|
+
export type AddMulti<Tr extends readonly Any[], T> = T extends ErrorType<any> ? T : _AddMulti<Tr, HandleOriginalType<T>>;
|
|
528
|
+
/** Internal implementation of 'Trait.AddMulti' */
|
|
529
|
+
type _AddMulti<Tr extends readonly Any[], T> = WithMetadata<T, TraitsCore.FromMap<IfNever<TraitsCore.TraitsOf<T>> & IfNever<TraitsCore.TraitsOf<IntersectOf<Tr[number]>>>>>;
|
|
530
|
+
/** Remove a trait */
|
|
531
|
+
export type Drop<Tr extends Any, T> = T extends ErrorType<any> ? T : _Drop<Tr, HandleOriginalType<T>>;
|
|
532
|
+
/** Internal implementation of 'Trait.Drop' */
|
|
533
|
+
type _Drop<Tr extends Any, T> = Equals<TraitsCore.TraitKeysOf<Tr>, TraitsCore.TraitKeysOf<T>> extends true ? Equals<keyof PhantomCore.PhantomOf<T>, '__OriginalType' | '__Traits'> extends true ? PhantomCore.StripPhantom<T> : WithoutMetadata<T, '__Traits'> : WithMetadata<T, TraitsCore.FromMap<Omit<TraitsCore.TraitsOf<T>, TraitsCore.TraitKeysOf<Tr>>>>;
|
|
534
|
+
/** Remove multiple traits */
|
|
535
|
+
export type DropMulti<Tr extends readonly Any[], T> = T extends ErrorType<any> ? T : _DropMulti<Tr, HandleOriginalType<T>>;
|
|
536
|
+
/** Internal implementation of 'Trait.DropMulti' */
|
|
537
|
+
type _DropMulti<Tr extends readonly Any[], T> = Equals<TraitsCore.TraitKeysOf<IntersectOf<Tr[number]>>, TraitsCore.TraitKeysOf<T>> extends true ? Equals<keyof PhantomCore.PhantomOf<T>, '__OriginalType' | '__Traits'> extends true ? PhantomCore.StripPhantom<T> : WithoutMetadata<T, '__Traits'> : WithMetadata<T, TraitsCore.FromMap<Omit<TraitsCore.TraitsOf<T>, TraitsCore.TraitKeysOf<IntersectOf<Tr[number]>>>>>;
|
|
538
|
+
/** Check whether value has trait */
|
|
539
|
+
export type HasTrait<T, Tr extends Any> = T extends Tr ? true : false;
|
|
540
|
+
export { };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Transformation API.
|
|
545
|
+
*
|
|
546
|
+
* Transformations represent reversible operations that
|
|
547
|
+
* change the shape of a value while preserving its origin.
|
|
548
|
+
*/
|
|
549
|
+
declare namespace TransformationCore {
|
|
550
|
+
/** Type guard for any transformation. */
|
|
551
|
+
export type Any = InputCore.Of<any> & TagCore.Of<string | symbol>;
|
|
552
|
+
/** Declare a transformation */
|
|
553
|
+
export type Declare<I, T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = Prettify<InputCore.Of<I> & TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
554
|
+
/**
|
|
555
|
+
* Apply a transformation to a value.
|
|
556
|
+
* Enforces base-type compatibility.
|
|
557
|
+
*/
|
|
558
|
+
export type Apply<Tr extends Any, I, T> = T extends ErrorType<any> ? T : _Apply<Tr, I, HandleOriginalType<T>>;
|
|
559
|
+
/** Internal implementation of 'Transformation.Apply' */
|
|
560
|
+
type _Apply<Tr extends Any, I, T> = TagCore.HasTag<T> extends true ? ErrorType<Errors<I, T>['alreadyBranded']> : T extends BaseCore.BaseOf<I> ? WithMetadata<T, PatchMetadata<Tr, InputCore.Of<I>>> : ErrorType<Errors<I, T>['typeNotExtendBase']>;
|
|
561
|
+
/** Revert a transformation */
|
|
562
|
+
export type Revert<Tr extends Any, T, I> = T extends ErrorType<any> ? T : _Revert<Tr, HandleOriginalType<T>, I>;
|
|
563
|
+
/** Internal implementation of 'Transformation.Revert' */
|
|
564
|
+
type _Revert<Tr extends Any, T, I> = InputCore.HasInput<T> extends true ? T extends Tr ? InputCore.InputOf<T> : ErrorType<Errors<Tr, T>['transformationMismatch']> : ErrorType<Errors<never, T>['notTransformed']>;
|
|
565
|
+
/** Revert a transformation whatever transformation was */
|
|
566
|
+
export type RevertAny<T, I> = T extends ErrorType<any> ? T : _RevertAny<HandleOriginalType<T>>;
|
|
567
|
+
/** Internal implementation of 'Transformation.RevertAny' */
|
|
568
|
+
type _RevertAny<T> = InputCore.HasInput<T> extends true ? InputCore.InputOf<T> : ErrorType<Errors<never, T>['notTransformed']>;
|
|
569
|
+
/** Check whether value is transformed with */
|
|
570
|
+
export type isTransformed<T, Tr extends Any> = T extends Tr ? true : false;
|
|
571
|
+
export { };
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
*
|
|
576
|
+
* This file define public aspect of fields and compesed types
|
|
577
|
+
*
|
|
578
|
+
*/
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Optional human-readable label metadata.
|
|
582
|
+
*
|
|
583
|
+
* Labels are descriptive only and do not affect identity.
|
|
584
|
+
*/
|
|
585
|
+
declare namespace Label {
|
|
586
|
+
/** Marker type for labeled values */
|
|
587
|
+
type Any = LabelCore.Any;
|
|
588
|
+
/** Extract the label */
|
|
589
|
+
type LabelOf<T> = LabelCore.LabelOf<T>;
|
|
590
|
+
/** Check whether a label exists */
|
|
591
|
+
type HasLabel<T, L extends string = string> = LabelCore.HasLabel<T, L>;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Nominal tag metadata.
|
|
595
|
+
*
|
|
596
|
+
* Tags uniquely identify a branded or identified type.
|
|
597
|
+
* A value may only have a single tag.
|
|
598
|
+
*/
|
|
599
|
+
declare namespace Tag {
|
|
600
|
+
/** Marker type for any tagged value */
|
|
601
|
+
type Any = TagCore.Any;
|
|
602
|
+
/** Extract the tag from a type */
|
|
603
|
+
type TagOf<T> = TagCore.TagOf<T>;
|
|
604
|
+
/** Check whether a type is tagged */
|
|
605
|
+
type HasTag<T, Ta extends string | symbol = string | symbol> = TagCore.HasTag<T, Ta>;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Variant metadata.
|
|
609
|
+
*
|
|
610
|
+
* Variants represent mutually exclusive states of a type.
|
|
611
|
+
*/
|
|
612
|
+
declare namespace Variants {
|
|
613
|
+
/** Marker type for variant-bearing values */
|
|
614
|
+
type Any = VariantsCore.Any;
|
|
615
|
+
/** Extract variant union */
|
|
616
|
+
type VariantsOf<T> = VariantsCore.VariantsOf<T>;
|
|
617
|
+
/** Check whether variants exist */
|
|
618
|
+
type HasVariants<T> = VariantsCore.HasVariants<T>;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Base-type metadata.
|
|
622
|
+
*
|
|
623
|
+
* Used to constrain which runtime types a brand, identity,
|
|
624
|
+
* or transformation may be applied to.
|
|
625
|
+
*/
|
|
626
|
+
declare namespace Base {
|
|
627
|
+
/** Marker type for base constraints */
|
|
628
|
+
type Any = BaseCore.Any;
|
|
629
|
+
/** Extract the base type */
|
|
630
|
+
type BaseOf<T> = BaseCore.BaseOf<T>;
|
|
631
|
+
/** Check whether a base constraint exists */
|
|
632
|
+
type HasBase<T, B = unknown> = BaseCore.HasBase<T, B>;
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Input metadata.
|
|
636
|
+
*
|
|
637
|
+
* Utilities for attaching and querying input metadata in transformations.
|
|
638
|
+
*/
|
|
639
|
+
declare namespace Input {
|
|
640
|
+
/** Marker type for input value */
|
|
641
|
+
type Any = InputCore.Any;
|
|
642
|
+
/** Extract the input */
|
|
643
|
+
type InputOf<T> = InputCore.InputOf<T>;
|
|
644
|
+
/** Check whether an input exists */
|
|
645
|
+
type HasInput<T, I = unknown> = InputCore.HasInput<T, I>;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Trait metadata.
|
|
649
|
+
*
|
|
650
|
+
* Traits behave like a set of capabilities that can be
|
|
651
|
+
* added or removed independently.
|
|
652
|
+
*/
|
|
653
|
+
declare namespace Traits {
|
|
654
|
+
/** Marker type for trait-bearing values */
|
|
655
|
+
type Any = TraitsCore.Any;
|
|
656
|
+
/** Extract the trait map */
|
|
657
|
+
type TraitsOf<T> = TraitsCore.TraitsOf<T>;
|
|
658
|
+
/** Extract trait keys */
|
|
659
|
+
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
660
|
+
/** Check if any traits exist */
|
|
661
|
+
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Branding API.
|
|
665
|
+
*
|
|
666
|
+
* Brands provide nominal typing for otherwise identical values.
|
|
667
|
+
* A value may only be branded once.
|
|
668
|
+
*/
|
|
669
|
+
declare namespace Brand {
|
|
670
|
+
/** Type guard for any brand. */
|
|
671
|
+
type Any = BrandCore.Any;
|
|
672
|
+
/** Declare a brand */
|
|
673
|
+
type Declare<T extends string | symbol, L extends string = never> = BrandCore.Declare<T, L>;
|
|
674
|
+
/** Assign a brand to a value. Fails if the value is already branded */
|
|
675
|
+
type Assign<B extends Any, T> = BrandCore.Assign<B, T>;
|
|
676
|
+
/** Assign a brand if possible, otherwise return the original type */
|
|
677
|
+
type AssignSafe<B extends Any, T> = BrandCore.AssignSafe<B, T>;
|
|
678
|
+
/** Check whether value is branded with */
|
|
679
|
+
type isBrand<T, B extends Any> = BrandCore.isBrand<T, B>;
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Identity API.
|
|
683
|
+
*
|
|
684
|
+
* Identities are brands with additional constraints:
|
|
685
|
+
* - Base type
|
|
686
|
+
* - Variants
|
|
687
|
+
*/
|
|
688
|
+
declare namespace Identity {
|
|
689
|
+
/** Type guard for any identity. */
|
|
690
|
+
type Any = IdentityCore.Any;
|
|
691
|
+
/** Declare an identity */
|
|
692
|
+
type Declare<T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = IdentityCore.Declare<T, L, B, V>;
|
|
693
|
+
/** Assign an identity to a value. Enforces base-type compatibility */
|
|
694
|
+
type Assign<I extends Any, T> = IdentityCore.Assign<I, T>;
|
|
695
|
+
/** Safe identity assignment */
|
|
696
|
+
type AssignSafe<I extends Any, T> = IdentityCore.AssignSafe<I, T>;
|
|
697
|
+
/** Set the active variant on an identity */
|
|
698
|
+
type WithVariant<I extends Any, V extends Variants.VariantsOf<I>> = IdentityCore.WithVariant<I, V>;
|
|
699
|
+
/** Set the active variant on a value */
|
|
700
|
+
type WithTypeVariant<T, V extends Variants.VariantsOf<T>> = IdentityCore.WithTypeVariant<T, V>;
|
|
701
|
+
/** Check whether value is branded with */
|
|
702
|
+
type isIdentity<T, I extends Any> = IdentityCore.isIdentity<T, I>;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Trait API.
|
|
706
|
+
*
|
|
707
|
+
* Traits are additive capabilities that can be attached
|
|
708
|
+
* or removed independently.
|
|
709
|
+
*/
|
|
710
|
+
declare namespace Trait {
|
|
711
|
+
/** Type guard for any trait. */
|
|
712
|
+
type Any = TraitCore.Any;
|
|
713
|
+
/** Declare a trait */
|
|
714
|
+
type Declare<Tr extends string | symbol> = TraitCore.Declare<Tr>;
|
|
715
|
+
/** Add a trait */
|
|
716
|
+
type Add<Tr extends Any, T> = TraitCore.Add<Tr, T>;
|
|
717
|
+
/** Add multiple traits */
|
|
718
|
+
type AddMulti<Tr extends readonly Any[], T> = TraitCore.AddMulti<Tr, T>;
|
|
719
|
+
/** Remove a trait */
|
|
720
|
+
type Drop<Tr extends Any, T> = TraitCore.Drop<Tr, T>;
|
|
721
|
+
/** Remove multiple traits */
|
|
722
|
+
type DropMulti<Tr extends readonly Any[], T> = TraitCore.DropMulti<Tr, T>;
|
|
723
|
+
/** Check whether value has trait */
|
|
724
|
+
type HasTrait<T, Tr extends Any> = TraitCore.HasTrait<T, Tr>;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Transformation API.
|
|
728
|
+
*
|
|
729
|
+
* Transformations represent reversible operations that
|
|
730
|
+
* change the shape of a value while preserving its origin.
|
|
731
|
+
*/
|
|
732
|
+
declare namespace Transformation {
|
|
733
|
+
/** Type guard for any transformation. */
|
|
734
|
+
type Any = TransformationCore.Any;
|
|
735
|
+
/** Declare a transformation */
|
|
736
|
+
type Declare<I, T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = TransformationCore.Declare<I, T, L, B, V>;
|
|
737
|
+
/** Apply a transformation to a value. Enforces base-type compatibility */
|
|
738
|
+
type Apply<Tr extends Any, I, T> = TransformationCore.Apply<Tr, I, T>;
|
|
739
|
+
/** Revert a transformation */
|
|
740
|
+
type Revert<Tr extends Any, T, I> = TransformationCore.Revert<Tr, T, I>;
|
|
741
|
+
/** Revert a transformation whatever transformation was */
|
|
742
|
+
type RevertAny<T, I> = TransformationCore.RevertAny<T, I>;
|
|
743
|
+
/** Check whether value is transformed with */
|
|
744
|
+
type isTransformed<T, Tr extends Any> = TransformationCore.isTransformed<T, Tr>;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Inspect API.
|
|
749
|
+
*
|
|
750
|
+
* Inspection helpers of phantom types.
|
|
751
|
+
*/
|
|
752
|
+
declare namespace Inspect {
|
|
753
|
+
/** Extract the label */
|
|
754
|
+
type LabelOf<T> = LabelCore.LabelOf<T>;
|
|
755
|
+
/** Check whether a base constraint exists */
|
|
756
|
+
type HasLabel<T, L extends string = string> = LabelCore.HasLabel<T, L>;
|
|
757
|
+
/** Extract the tag from a type */
|
|
758
|
+
type TagOf<T> = TagCore.TagOf<T>;
|
|
759
|
+
/** Check whether a type is tagged */
|
|
760
|
+
type HasTag<T, Ta extends string | symbol = string | symbol> = TagCore.HasTag<T, Ta>;
|
|
761
|
+
/** Extract variant union */
|
|
762
|
+
type VariantsOf<T> = VariantsCore.VariantsOf<T>;
|
|
763
|
+
/** Check whether variants exist */
|
|
764
|
+
type HasVariants<T> = VariantsCore.HasVariants<T>;
|
|
765
|
+
/** Extract the base type */
|
|
766
|
+
type BaseOf<T> = BaseCore.BaseOf<T>;
|
|
767
|
+
/** Check whether a base constraint exists */
|
|
768
|
+
type HasBase<T, B = unknown> = BaseCore.HasBase<T, B>;
|
|
769
|
+
/** Extract the input */
|
|
770
|
+
type InputOf<T> = InputCore.InputOf<T>;
|
|
771
|
+
/** Check whether an input exists */
|
|
772
|
+
type HasInput<T, I = unknown> = InputCore.HasInput<T, I>;
|
|
773
|
+
/** Extract the trait map */
|
|
774
|
+
type TraitsOf<T> = TraitsCore.TraitsOf<T>;
|
|
775
|
+
/** Extract trait keys */
|
|
776
|
+
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
777
|
+
/** Check if any traits exist */
|
|
778
|
+
type HasTraits<T> = TraitsCore.HasTraits<T>;
|
|
779
|
+
/** Check whether value is branded with */
|
|
780
|
+
type isBrand<T, B extends BrandCore.Any> = BrandCore.isBrand<T, B>;
|
|
781
|
+
/** Check whether value is branded with */
|
|
782
|
+
type isIdentity<T, I extends IdentityCore.Any> = IdentityCore.isIdentity<T, I>;
|
|
783
|
+
/** Check whether value has trait */
|
|
784
|
+
type HasTrait<T, Tr extends TraitCore.Any> = TraitCore.HasTrait<T, Tr>;
|
|
785
|
+
/** Check whether value is transformed with */
|
|
786
|
+
type isTransformed<T, Tr extends TransformationCore.Any> = TransformationCore.isTransformed<T, Tr>;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
791
|
+
*
|
|
792
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
793
|
+
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
794
|
+
* where you know the value is valid.
|
|
795
|
+
*
|
|
796
|
+
* @template B - The brand declaration to assign.
|
|
797
|
+
* @returns A function that casts any value to the branded type.
|
|
798
|
+
*/
|
|
799
|
+
declare const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
803
|
+
*
|
|
804
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
805
|
+
* with the identity's nominal type applied. Use it when you know a value
|
|
806
|
+
* conforms to an identity but need to assert it for the type system.
|
|
807
|
+
*
|
|
808
|
+
* @template I - The identity declaration to assign.
|
|
809
|
+
* @returns A function that casts any value to the assigned identity type.
|
|
810
|
+
*/
|
|
811
|
+
declare const asIdentity: <I extends Identity.Any>() => <V>(value: V) => Identity.Assign<I, V>;
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Creates a typed caster that adds a single {@link Trait} to a value.
|
|
815
|
+
*
|
|
816
|
+
* Zero-runtime-cost assertion helper.
|
|
817
|
+
*
|
|
818
|
+
* @template Tr - The trait declaration to add.
|
|
819
|
+
* @returns A function that adds the trait to any value.
|
|
820
|
+
*/
|
|
821
|
+
declare const addTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Add<Tr, V>;
|
|
822
|
+
/**
|
|
823
|
+
* Creates a typed caster that adds multiple {@link Trait}s to a value.
|
|
824
|
+
*
|
|
825
|
+
* Zero-runtime-cost assertion helper.
|
|
826
|
+
*
|
|
827
|
+
* @template Tr - Tuple of trait declarations to add.
|
|
828
|
+
* @returns A function that adds all traits to any value.
|
|
829
|
+
*/
|
|
830
|
+
declare const addTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.AddMulti<Tr, V>;
|
|
831
|
+
/**
|
|
832
|
+
* Creates a typed caster that removes a single {@link Trait} from a value.
|
|
833
|
+
*
|
|
834
|
+
* Zero-runtime-cost assertion helper.
|
|
835
|
+
*
|
|
836
|
+
* @template Tr - The trait declaration to remove.
|
|
837
|
+
* @returns A function that drops the trait from any value.
|
|
838
|
+
*/
|
|
839
|
+
declare const dropTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Drop<Tr, V>;
|
|
840
|
+
/**
|
|
841
|
+
* Creates a typed caster that removes multiple {@link Trait}s from a value.
|
|
842
|
+
*
|
|
843
|
+
* Zero-runtime-cost assertion helper.
|
|
844
|
+
*
|
|
845
|
+
* @template Tr - Tuple of trait declarations to remove.
|
|
846
|
+
* @returns A function that drops all specified traits from any value.
|
|
847
|
+
*/
|
|
848
|
+
declare const dropTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.DropMulti<Tr, V>;
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Creates a typed applicator for a {@link Transformation}.
|
|
852
|
+
*
|
|
853
|
+
* Use this for "forward" operations (e.g., encrypt, encode, wrap).
|
|
854
|
+
* The `input` parameter is only used for type inference — it is not used at runtime.
|
|
855
|
+
*
|
|
856
|
+
* Zero-runtime-cost assertion helper.
|
|
857
|
+
*
|
|
858
|
+
* @template Tr - The transformation declaration.
|
|
859
|
+
* @returns A function that applies the transformation while preserving the input type for later revert.
|
|
860
|
+
*/
|
|
861
|
+
declare const applyTransformation: <Tr extends Transformation.Any>() => <I, T>(input: I, transformed: T) => Transformation.Apply<Tr, I, T>;
|
|
862
|
+
/**
|
|
863
|
+
* Creates a typed reverter for a {@link Transformation}.
|
|
864
|
+
*
|
|
865
|
+
* Use this for "reverse" operations (e.g., decrypt, decode, unwrap).
|
|
866
|
+
* The `transformed` parameter is used for type inference of the expected input,
|
|
867
|
+
* and `input` is the computed result that must match the stored input type.
|
|
868
|
+
*
|
|
869
|
+
* Zero-runtime-cost assertion helper.
|
|
870
|
+
*
|
|
871
|
+
* @template Tr - The transformation declaration.
|
|
872
|
+
* @returns A function that reverts the transformation, stripping phantom metadata.
|
|
873
|
+
*/
|
|
874
|
+
declare const revertTransformation: <Tr extends Transformation.Any>() => <T, I>(transformed: T, input: I) => Transformation.Revert<Tr, T, I>;
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* A fluent PhantomChain class for chaining Phantom assertors.
|
|
878
|
+
*
|
|
879
|
+
* This provides a better developer experience (DX) by allowing method chaining
|
|
880
|
+
* with `.with(assertor)` instead of nesting function calls or using a variadic chain.
|
|
881
|
+
* Each `.with()` applies the assertor to the current value, updating the type incrementally.
|
|
882
|
+
* Call `.end()` to retrieve the final value.
|
|
883
|
+
*
|
|
884
|
+
* At runtime, assertors are zero-cost casts, so the PhantomChain adds minimal overhead
|
|
885
|
+
* (just object creation and method calls).
|
|
886
|
+
*
|
|
887
|
+
* Example:
|
|
888
|
+
* ```ts
|
|
889
|
+
* const asMyBrand = Phantom.assertors.asBrand<MyBrand>();
|
|
890
|
+
* const asMyTrait = Phantom.assertors.asTrait<MyTrait>();
|
|
891
|
+
* const applyMyTransform = Phantom.assertors.applyTransformation<MyTransform>();
|
|
892
|
+
*
|
|
893
|
+
* const result = new PhantomChain("value")
|
|
894
|
+
* .with(asMyBrand)
|
|
895
|
+
* .with(asMyTrait)
|
|
896
|
+
* .with(applyMyTransform)
|
|
897
|
+
* .end();
|
|
898
|
+
* ```
|
|
899
|
+
*/
|
|
900
|
+
declare class PhantomChain<T> {
|
|
901
|
+
private value;
|
|
902
|
+
constructor(value: T);
|
|
903
|
+
/**
|
|
904
|
+
* Apply the next assertor in the chain.
|
|
905
|
+
*
|
|
906
|
+
* @param assertor A function that takes the current value and returns the updated value (with new type).
|
|
907
|
+
* @returns A new PhantomChain instance with the updated value and type.
|
|
908
|
+
*/
|
|
909
|
+
with<U>(assertor: (value: T) => U): PhantomChain<U>;
|
|
910
|
+
/**
|
|
911
|
+
* End the chain and return the final value.
|
|
912
|
+
*
|
|
913
|
+
* @returns The value after all transformations.
|
|
914
|
+
*/
|
|
915
|
+
end(): T;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
declare const _PhantomChain: typeof PhantomChain;
|
|
919
|
+
type _ErrorType<E> = ErrorType<E>;
|
|
920
|
+
declare namespace Phantom {
|
|
921
|
+
/** --------------------------------------
|
|
922
|
+
* Types
|
|
923
|
+
* --------------------------------------- */
|
|
924
|
+
/**
|
|
925
|
+
* Optional human-readable label metadata.
|
|
926
|
+
*
|
|
927
|
+
* Labels are descriptive only and do not affect identity.
|
|
928
|
+
*/
|
|
929
|
+
namespace Label {
|
|
930
|
+
/** Marker type for labeled values */
|
|
931
|
+
type Any = LabelCore.Any;
|
|
932
|
+
/** Extract the label */
|
|
933
|
+
type LabelOf<T> = LabelCore.LabelOf<T>;
|
|
934
|
+
/** Check whether a label exists */
|
|
935
|
+
type HasLabel<T, L extends string = string> = LabelCore.HasLabel<T, L>;
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Nominal tag metadata.
|
|
939
|
+
*
|
|
940
|
+
* Tags uniquely identify a branded or identified type.
|
|
941
|
+
* A value may only have a single tag.
|
|
942
|
+
*/
|
|
943
|
+
namespace Tag {
|
|
944
|
+
/** Marker type for any tagged value */
|
|
945
|
+
type Any = TagCore.Any;
|
|
946
|
+
/** Extract the tag from a type */
|
|
947
|
+
type TagOf<T> = TagCore.TagOf<T>;
|
|
948
|
+
/** Check whether a type is tagged */
|
|
949
|
+
type HasTag<T, Ta extends string | symbol = string | symbol> = TagCore.HasTag<T, Ta>;
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Variant metadata.
|
|
953
|
+
*
|
|
954
|
+
* Variants represent mutually exclusive states of a type.
|
|
955
|
+
*/
|
|
956
|
+
namespace Variants {
|
|
957
|
+
/** Marker type for variant-bearing values */
|
|
958
|
+
type Any = VariantsCore.Any;
|
|
959
|
+
/** Extract variant union */
|
|
960
|
+
type VariantsOf<T> = VariantsCore.VariantsOf<T>;
|
|
961
|
+
/** Check whether variants exist */
|
|
962
|
+
type HasVariants<T> = VariantsCore.HasVariants<T>;
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Base-type metadata.
|
|
966
|
+
*
|
|
967
|
+
* Used to constrain which runtime types a brand, identity,
|
|
968
|
+
* or transformation may be applied to.
|
|
969
|
+
*/
|
|
970
|
+
namespace Base {
|
|
971
|
+
/** Marker type for base constraints */
|
|
972
|
+
type Any = BaseCore.Any;
|
|
973
|
+
/** Extract the base type */
|
|
974
|
+
type BaseOf<T> = BaseCore.BaseOf<T>;
|
|
975
|
+
/** Check whether a base constraint exists */
|
|
976
|
+
type HasBase<T, B = unknown> = BaseCore.HasBase<T, B>;
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Input metadata.
|
|
980
|
+
*
|
|
981
|
+
* Utilities for attaching and querying input metadata in transformations.
|
|
982
|
+
*/
|
|
983
|
+
namespace Input {
|
|
984
|
+
/** Marker type for input value */
|
|
985
|
+
type Any = InputCore.Any;
|
|
986
|
+
/** Extract the input */
|
|
987
|
+
type InputOf<T> = InputCore.InputOf<T>;
|
|
988
|
+
/** Check whether an input exists */
|
|
989
|
+
type HasInput<T, I = unknown> = InputCore.HasInput<T, I>;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Trait metadata.
|
|
993
|
+
*
|
|
994
|
+
* Traits behave like a set of capabilities that can be
|
|
995
|
+
* added or removed independently.
|
|
996
|
+
*/
|
|
997
|
+
namespace Traits {
|
|
998
|
+
/** Marker type for trait-bearing values */
|
|
999
|
+
type Any = TraitsCore.Any;
|
|
1000
|
+
/** Extract the trait map */
|
|
1001
|
+
type TraitsOf<T> = TraitsCore.TraitsOf<T>;
|
|
1002
|
+
/** Extract trait keys */
|
|
1003
|
+
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
1004
|
+
/** Check if any traits exist */
|
|
1005
|
+
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
1006
|
+
}
|
|
1007
|
+
/**
|
|
1008
|
+
* Branding API.
|
|
1009
|
+
*
|
|
1010
|
+
* Brands provide nominal typing for otherwise identical values.
|
|
1011
|
+
* A value may only be branded once.
|
|
1012
|
+
*/
|
|
1013
|
+
namespace Brand {
|
|
1014
|
+
/** Type guard for any brand. */
|
|
1015
|
+
type Any = BrandCore.Any;
|
|
1016
|
+
/** Declare a brand */
|
|
1017
|
+
type Declare<T extends string | symbol, L extends string = never> = BrandCore.Declare<T, L>;
|
|
1018
|
+
/** Assign a brand to a value. Fails if the value is already branded */
|
|
1019
|
+
type Assign<B extends Any, T> = BrandCore.Assign<B, T>;
|
|
1020
|
+
/** Assign a brand if possible, otherwise return the original type */
|
|
1021
|
+
type AssignSafe<B extends Any, T> = BrandCore.AssignSafe<B, T>;
|
|
1022
|
+
/** Check whether value is branded with */
|
|
1023
|
+
type isBrand<T, B extends Brand.Any> = BrandCore.isBrand<T, B>;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Identity API.
|
|
1027
|
+
*
|
|
1028
|
+
* Identities are brands with additional constraints:
|
|
1029
|
+
* - Base type
|
|
1030
|
+
* - Variants
|
|
1031
|
+
*/
|
|
1032
|
+
namespace Identity {
|
|
1033
|
+
/** Type guard for any identity. */
|
|
1034
|
+
type Any = IdentityCore.Any;
|
|
1035
|
+
/** Declare an identity */
|
|
1036
|
+
type Declare<T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = IdentityCore.Declare<T, L, B, V>;
|
|
1037
|
+
/** Assign an identity to a value. Enforces base-type compatibility */
|
|
1038
|
+
type Assign<I extends Any, T> = IdentityCore.Assign<I, T>;
|
|
1039
|
+
/** Safe identity assignment */
|
|
1040
|
+
type AssignSafe<I extends Any, T> = IdentityCore.AssignSafe<I, T>;
|
|
1041
|
+
/** Set the active variant on an identity */
|
|
1042
|
+
type WithVariant<I extends Any, V extends Variants.VariantsOf<I>> = IdentityCore.WithVariant<I, V>;
|
|
1043
|
+
/** Set the active variant on a value */
|
|
1044
|
+
type WithTypeVariant<T, V extends Variants.VariantsOf<T>> = IdentityCore.WithTypeVariant<T, V>;
|
|
1045
|
+
/** Check whether value is branded with */
|
|
1046
|
+
type isIdentity<T, I extends Any> = IdentityCore.isIdentity<T, I>;
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Trait API.
|
|
1050
|
+
*
|
|
1051
|
+
* Traits are additive capabilities that can be attached
|
|
1052
|
+
* or removed independently.
|
|
1053
|
+
*/
|
|
1054
|
+
namespace Trait {
|
|
1055
|
+
/** Type guard for any trait. */
|
|
1056
|
+
type Any = TraitCore.Any;
|
|
1057
|
+
/** Declare a trait */
|
|
1058
|
+
type Declare<Tr extends string | symbol> = TraitCore.Declare<Tr>;
|
|
1059
|
+
/** Add a trait */
|
|
1060
|
+
type Add<Tr extends Any, T> = TraitCore.Add<Tr, T>;
|
|
1061
|
+
/** Add multiple traits */
|
|
1062
|
+
type AddMulti<Tr extends readonly Any[], T> = TraitCore.AddMulti<Tr, T>;
|
|
1063
|
+
/** Remove a trait */
|
|
1064
|
+
type Drop<Tr extends Any, T> = TraitCore.Drop<Tr, T>;
|
|
1065
|
+
/** Remove multiple traits */
|
|
1066
|
+
type DropMulti<Tr extends readonly Any[], T> = TraitCore.DropMulti<Tr, T>;
|
|
1067
|
+
/** Check whether value has trait */
|
|
1068
|
+
type HasTrait<T, Tr extends Any> = TraitCore.HasTrait<T, Tr>;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Transformation API.
|
|
1072
|
+
*
|
|
1073
|
+
* Transformations represent reversible operations that
|
|
1074
|
+
* change the shape of a value while preserving its origin.
|
|
1075
|
+
*/
|
|
1076
|
+
namespace Transformation {
|
|
1077
|
+
/** Type guard for any transformation. */
|
|
1078
|
+
type Any = TransformationCore.Any;
|
|
1079
|
+
/** Declare a transformation */
|
|
1080
|
+
type Declare<I, T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = TransformationCore.Declare<I, T, L, B, V>;
|
|
1081
|
+
/** Apply a transformation to a value. Enforces base-type compatibility */
|
|
1082
|
+
type Apply<Tr extends Any, I, T> = TransformationCore.Apply<Tr, I, T>;
|
|
1083
|
+
/** Revert a transformation */
|
|
1084
|
+
type Revert<Tr extends Any, T, I> = TransformationCore.Revert<Tr, T, I>;
|
|
1085
|
+
/** Revert a transformation whatever transformation was */
|
|
1086
|
+
type RevertAny<T, I> = TransformationCore.RevertAny<T, I>;
|
|
1087
|
+
/** Check whether value is transformed with */
|
|
1088
|
+
type isTransformed<T, Tr extends Any> = TransformationCore.isTransformed<T, Tr>;
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Inspect API.
|
|
1092
|
+
*
|
|
1093
|
+
* Inspection helpers of phantom types.
|
|
1094
|
+
*/
|
|
1095
|
+
namespace Inspect {
|
|
1096
|
+
/** Get phantom metadata object from a type */
|
|
1097
|
+
type PhantomOf<T> = PhantomCore.PhantomOf<T>;
|
|
1098
|
+
/** Stip phantom metadata object from a type */
|
|
1099
|
+
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1100
|
+
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1101
|
+
const stripPhantom: <T>(value: T) => PhantomCore.StripPhantom<T>;
|
|
1102
|
+
/** Extract the label */
|
|
1103
|
+
type LabelOf<T> = LabelCore.LabelOf<T>;
|
|
1104
|
+
/** Check whether a base constraint exists */
|
|
1105
|
+
type HasLabel<T, L extends string = string> = LabelCore.HasLabel<T, L>;
|
|
1106
|
+
/** Extract the tag from a type */
|
|
1107
|
+
type TagOf<T> = TagCore.TagOf<T>;
|
|
1108
|
+
/** Check whether a type is tagged */
|
|
1109
|
+
type HasTag<T, Ta extends string | symbol = string | symbol> = TagCore.HasTag<T, Ta>;
|
|
1110
|
+
/** Extract variant union */
|
|
1111
|
+
type VariantsOf<T> = VariantsCore.VariantsOf<T>;
|
|
1112
|
+
/** Check whether variants exist */
|
|
1113
|
+
type HasVariants<T> = VariantsCore.HasVariants<T>;
|
|
1114
|
+
/** Extract the base type */
|
|
1115
|
+
type BaseOf<T> = BaseCore.BaseOf<T>;
|
|
1116
|
+
/** Check whether a base constraint exists */
|
|
1117
|
+
type HasBase<T, B = unknown> = BaseCore.HasBase<T, B>;
|
|
1118
|
+
/** Extract the input */
|
|
1119
|
+
type InputOf<T> = InputCore.InputOf<T>;
|
|
1120
|
+
/** Check whether an input exists */
|
|
1121
|
+
type HasInput<T, I = unknown> = InputCore.HasInput<T, I>;
|
|
1122
|
+
/** Extract the trait map */
|
|
1123
|
+
type TraitsOf<T> = TraitsCore.TraitsOf<T>;
|
|
1124
|
+
/** Extract trait keys */
|
|
1125
|
+
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
1126
|
+
/** Check if any traits exist */
|
|
1127
|
+
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
1128
|
+
/** Check whether value is branded with */
|
|
1129
|
+
type isBrand<T, B extends Brand.Any> = BrandCore.isBrand<T, B>;
|
|
1130
|
+
/** Check whether value is branded with */
|
|
1131
|
+
type isIdentity<T, I extends Identity.Any> = IdentityCore.isIdentity<T, I>;
|
|
1132
|
+
/** Check whether value has trait */
|
|
1133
|
+
type HasTrait<T, Tr extends Trait.Any> = TraitCore.HasTrait<T, Tr>;
|
|
1134
|
+
/** Check whether value is transformed with */
|
|
1135
|
+
type isTransformed<T, Tr extends Transformation.Any> = TransformationCore.isTransformed<T, Tr>;
|
|
1136
|
+
}
|
|
1137
|
+
/** --------------------------------------
|
|
1138
|
+
* assertors
|
|
1139
|
+
* --------------------------------------- */
|
|
1140
|
+
namespace assertors {
|
|
1141
|
+
/**
|
|
1142
|
+
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
1143
|
+
*
|
|
1144
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
1145
|
+
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1146
|
+
* where you know the value is valid.
|
|
1147
|
+
*
|
|
1148
|
+
* @template B - The brand declaration to assign.
|
|
1149
|
+
* @returns A function that casts any value to the branded type.
|
|
1150
|
+
*/
|
|
1151
|
+
const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
1152
|
+
/**
|
|
1153
|
+
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
1154
|
+
*
|
|
1155
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
1156
|
+
* with the identity's nominal type applied. Use it when you know a value
|
|
1157
|
+
* conforms to an identity but need to assert it for the type system.
|
|
1158
|
+
*
|
|
1159
|
+
* @template I - The identity declaration to assign.
|
|
1160
|
+
* @returns A function that casts any value to the assigned identity type.
|
|
1161
|
+
*/
|
|
1162
|
+
const asIdentity: <I extends Identity.Any>() => <V>(value: V) => Identity.Assign<I, V>;
|
|
1163
|
+
/**
|
|
1164
|
+
* Creates a typed caster that adds a single {@link Trait} to a value.
|
|
1165
|
+
*
|
|
1166
|
+
* Zero-runtime-cost assertion helper.
|
|
1167
|
+
*
|
|
1168
|
+
* @template Tr - The trait declaration to add.
|
|
1169
|
+
* @returns A function that adds the trait to any value.
|
|
1170
|
+
*/
|
|
1171
|
+
const addTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Add<Tr, V>;
|
|
1172
|
+
/**
|
|
1173
|
+
* Creates a typed caster that adds multiple {@link Trait}s to a value.
|
|
1174
|
+
*
|
|
1175
|
+
* Zero-runtime-cost assertion helper.
|
|
1176
|
+
*
|
|
1177
|
+
* @template Tr - Tuple of trait declarations to add.
|
|
1178
|
+
* @returns A function that adds all traits to any value.
|
|
1179
|
+
*/
|
|
1180
|
+
const addTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.AddMulti<Tr, V>;
|
|
1181
|
+
/**
|
|
1182
|
+
* Creates a typed caster that removes a single {@link Trait} from a value.
|
|
1183
|
+
*
|
|
1184
|
+
* Zero-runtime-cost assertion helper.
|
|
1185
|
+
*
|
|
1186
|
+
* @template Tr - The trait declaration to remove.
|
|
1187
|
+
* @returns A function that drops the trait from any value.
|
|
1188
|
+
*/
|
|
1189
|
+
const dropTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Drop<Tr, V>;
|
|
1190
|
+
/**
|
|
1191
|
+
* Creates a typed caster that removes multiple {@link Trait}s from a value.
|
|
1192
|
+
*
|
|
1193
|
+
* Zero-runtime-cost assertion helper.
|
|
1194
|
+
*
|
|
1195
|
+
* @template Tr - Tuple of trait declarations to remove.
|
|
1196
|
+
* @returns A function that drops all specified traits from any value.
|
|
1197
|
+
*/
|
|
1198
|
+
const dropTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.DropMulti<Tr, V>;
|
|
1199
|
+
/**
|
|
1200
|
+
* Creates a typed applicator for a {@link Transformation}.
|
|
1201
|
+
*
|
|
1202
|
+
* Use this for "forward" operations (e.g., encrypt, encode, wrap).
|
|
1203
|
+
* The `input` parameter is only used for type inference — it is not used at runtime.
|
|
1204
|
+
*
|
|
1205
|
+
* Zero-runtime-cost assertion helper.
|
|
1206
|
+
*
|
|
1207
|
+
* @template Tr - The transformation declaration.
|
|
1208
|
+
* @returns A function that applies the transformation while preserving the input type for later revert.
|
|
1209
|
+
*/
|
|
1210
|
+
const applyTransformation: <Tr extends Transformation.Any>() => <I, T>(input: I, transformed: T) => Transformation.Apply<Tr, I, T>;
|
|
1211
|
+
/**
|
|
1212
|
+
* Creates a typed reverter for a {@link Transformation}.
|
|
1213
|
+
*
|
|
1214
|
+
* Use this for "reverse" operations (e.g., decrypt, decode, unwrap).
|
|
1215
|
+
* The `transformed` parameter is used for type inference of the expected input,
|
|
1216
|
+
* and `input` is the computed result that must match the stored input type.
|
|
1217
|
+
*
|
|
1218
|
+
* Zero-runtime-cost assertion helper.
|
|
1219
|
+
*
|
|
1220
|
+
* @template Tr - The transformation declaration.
|
|
1221
|
+
* @returns A function that reverts the transformation, stripping phantom metadata.
|
|
1222
|
+
*/
|
|
1223
|
+
const revertTransformation: <Tr extends Transformation.Any>() => <T, I>(transformed: T, input: I) => Transformation.Revert<Tr, T, I>;
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Creates a typed caster that assigns a {@link Brand} to a value.
|
|
1227
|
+
*
|
|
1228
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
1229
|
+
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1230
|
+
* where you know the value is valid.
|
|
1231
|
+
*
|
|
1232
|
+
* @template B - The brand declaration to assign.
|
|
1233
|
+
* @returns A function that casts any value to the branded type.
|
|
1234
|
+
*/
|
|
1235
|
+
const asBrand: <B extends Brand.Any>() => <T>(value: T) => Brand.Assign<B, T>;
|
|
1236
|
+
/**
|
|
1237
|
+
* Creates a typed caster that assigns an {@link Identity} to a value.
|
|
1238
|
+
*
|
|
1239
|
+
* This is a zero-cost runtime assertion helper — it simply returns the value
|
|
1240
|
+
* with the identity's nominal type applied. Use it when you know a value
|
|
1241
|
+
* conforms to an identity but need to assert it for the type system.
|
|
1242
|
+
*
|
|
1243
|
+
* @template I - The identity declaration to assign.
|
|
1244
|
+
* @returns A function that casts any value to the assigned identity type.
|
|
1245
|
+
*/
|
|
1246
|
+
const asIdentity: <I extends Identity.Any>() => <V>(value: V) => Identity.Assign<I, V>;
|
|
1247
|
+
/**
|
|
1248
|
+
* Creates a typed caster that adds a single {@link Trait} to a value.
|
|
1249
|
+
*
|
|
1250
|
+
* Zero-runtime-cost assertion helper.
|
|
1251
|
+
*
|
|
1252
|
+
* @template Tr - The trait declaration to add.
|
|
1253
|
+
* @returns A function that adds the trait to any value.
|
|
1254
|
+
*/
|
|
1255
|
+
const addTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Add<Tr, V>;
|
|
1256
|
+
/**
|
|
1257
|
+
* Creates a typed caster that adds multiple {@link Trait}s to a value.
|
|
1258
|
+
*
|
|
1259
|
+
* Zero-runtime-cost assertion helper.
|
|
1260
|
+
*
|
|
1261
|
+
* @template Tr - Tuple of trait declarations to add.
|
|
1262
|
+
* @returns A function that adds all traits to any value.
|
|
1263
|
+
*/
|
|
1264
|
+
const addTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.AddMulti<Tr, V>;
|
|
1265
|
+
/**
|
|
1266
|
+
* Creates a typed caster that removes a single {@link Trait} from a value.
|
|
1267
|
+
*
|
|
1268
|
+
* Zero-runtime-cost assertion helper.
|
|
1269
|
+
*
|
|
1270
|
+
* @template Tr - The trait declaration to remove.
|
|
1271
|
+
* @returns A function that drops the trait from any value.
|
|
1272
|
+
*/
|
|
1273
|
+
const dropTrait: <Tr extends Trait.Any>() => <V>(value: V) => Trait.Drop<Tr, V>;
|
|
1274
|
+
/**
|
|
1275
|
+
* Creates a typed caster that removes multiple {@link Trait}s from a value.
|
|
1276
|
+
*
|
|
1277
|
+
* Zero-runtime-cost assertion helper.
|
|
1278
|
+
*
|
|
1279
|
+
* @template Tr - Tuple of trait declarations to remove.
|
|
1280
|
+
* @returns A function that drops all specified traits from any value.
|
|
1281
|
+
*/
|
|
1282
|
+
const dropTraits: <Tr extends Trait.Any[]>() => <V>(value: V) => Trait.DropMulti<Tr, V>;
|
|
1283
|
+
/**
|
|
1284
|
+
* Creates a typed applicator for a {@link Transformation}.
|
|
1285
|
+
*
|
|
1286
|
+
* Use this for "forward" operations (e.g., encrypt, encode, wrap).
|
|
1287
|
+
* The `input` parameter is only used for type inference — it is not used at runtime.
|
|
1288
|
+
*
|
|
1289
|
+
* Zero-runtime-cost assertion helper.
|
|
1290
|
+
*
|
|
1291
|
+
* @template Tr - The transformation declaration.
|
|
1292
|
+
* @returns A function that applies the transformation while preserving the input type for later revert.
|
|
1293
|
+
*/
|
|
1294
|
+
const applyTransformation: <Tr extends Transformation.Any>() => <I, T>(input: I, transformed: T) => Transformation.Apply<Tr, I, T>;
|
|
1295
|
+
/**
|
|
1296
|
+
* Creates a typed reverter for a {@link Transformation}.
|
|
1297
|
+
*
|
|
1298
|
+
* Use this for "reverse" operations (e.g., decrypt, decode, unwrap).
|
|
1299
|
+
* The `transformed` parameter is used for type inference of the expected input,
|
|
1300
|
+
* and `input` is the computed result that must match the stored input type.
|
|
1301
|
+
*
|
|
1302
|
+
* Zero-runtime-cost assertion helper.
|
|
1303
|
+
*
|
|
1304
|
+
* @template Tr - The transformation declaration.
|
|
1305
|
+
* @returns A function that reverts the transformation, stripping phantom metadata.
|
|
1306
|
+
*/
|
|
1307
|
+
const revertTransformation: <Tr extends Transformation.Any>() => <T, I>(transformed: T, input: I) => Transformation.Revert<Tr, T, I>;
|
|
1308
|
+
/** --------------------------------------
|
|
1309
|
+
* Phantom object manipulators
|
|
1310
|
+
* --------------------------------------- */
|
|
1311
|
+
/** Get phantom metadata object from a type */
|
|
1312
|
+
type PhantomOf<T> = PhantomCore.PhantomOf<T>;
|
|
1313
|
+
/** Stip phantom metadata object from a type */
|
|
1314
|
+
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1315
|
+
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1316
|
+
const stripPhantom: <T>(value: T) => PhantomCore.StripPhantom<T>;
|
|
1317
|
+
/** --------------------------------------
|
|
1318
|
+
* Error type
|
|
1319
|
+
* --------------------------------------- */
|
|
1320
|
+
/** Unique Error type for rules validation in phantom. */
|
|
1321
|
+
type ErrorType<E> = _ErrorType<E>;
|
|
1322
|
+
/** --------------------------------------
|
|
1323
|
+
* Chain class
|
|
1324
|
+
* --------------------------------------- */
|
|
1325
|
+
/**
|
|
1326
|
+
* A fluent PhantomChain class for chaining Phantom assertors.
|
|
1327
|
+
*
|
|
1328
|
+
* This provides a better developer experience (DX) by allowing method chaining
|
|
1329
|
+
* with `.with(assertor)` instead of nesting function calls or using a variadic chain.
|
|
1330
|
+
* Each `.with()` applies the assertor to the current value, updating the type incrementally.
|
|
1331
|
+
* Call `.end()` to retrieve the final value.
|
|
1332
|
+
*
|
|
1333
|
+
* At runtime, assertors are zero-cost casts, so the PhantomChain adds minimal overhead
|
|
1334
|
+
* (just object creation and method calls).
|
|
1335
|
+
*
|
|
1336
|
+
* Example:
|
|
1337
|
+
* ```ts
|
|
1338
|
+
* const asMyBrand = Phantom.assertors.asBrand<MyBrand>();
|
|
1339
|
+
* const asMyTrait = Phantom.assertors.asTrait<MyTrait>();
|
|
1340
|
+
* const applyMyTransform = Phantom.assertors.applyTransformation<MyTransform>();
|
|
1341
|
+
*
|
|
1342
|
+
* const result = new PhantomChain("value")
|
|
1343
|
+
* .with(asMyBrand)
|
|
1344
|
+
* .with(asMyTrait)
|
|
1345
|
+
* .with(applyMyTransform)
|
|
1346
|
+
* .end();
|
|
1347
|
+
* ```
|
|
1348
|
+
*/
|
|
1349
|
+
class PhantomChain<T> extends _PhantomChain<T> {
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
export { Base, Brand, type ErrorType, Identity, Input, Inspect, Label, Phantom, PhantomChain, PhantomCore, Tag, Trait, Traits, Transformation, Variants, addTrait, addTraits, applyTransformation, asBrand, asIdentity, assertors, Phantom as default, dropTrait, dropTraits, revertTransformation };
|