@vicin/phantom 1.1.0 → 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} +123 -228
- package/dist/index.d.ts +123 -228
- 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/CHANGELOG.md
ADDED
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Contributing to Phantom
|
|
2
|
+
|
|
3
|
+
Thanks for your interest! We welcome contributions.
|
|
4
|
+
|
|
5
|
+
## How to Contribute
|
|
6
|
+
|
|
7
|
+
- **Issues**: Report bugs or suggest features via GitHub Issues. Use templates if possible.
|
|
8
|
+
- **Pull Requests (PRs)**:
|
|
9
|
+
1. Fork the repo and create a branch: `git checkout -b feature/my-new-feature`.
|
|
10
|
+
2. Commit changes: Follow conventional commits (e.g., "feat: add new ValueObject").
|
|
11
|
+
3. Push and open a PR against `develop` branch.
|
|
12
|
+
4. Describe changes, link to issues, and add tests.
|
|
13
|
+
- **Code Style**: Use Prettier/ESLint.
|
|
14
|
+
- **Tests**: All PRs must pass unit tests.
|
|
15
|
+
- **License**: By contributing, you agree to MIT license.
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
- Clone: `git clone https://github.com/ZiadTaha62/vicin-packages.git`
|
|
20
|
+
- Install: `pnpm install`
|
|
21
|
+
- Build: `pnpm run build --filter @vicin/phantom`
|
|
22
|
+
- Test: `pnpm run test --filter @vicin/phantom` coverage should be `>90%` of lines.
|
|
23
|
+
|
|
24
|
+
Questions? Open an issue!
|
package/LICENSE
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ziad Taha and vicin contributors
|
|
2
4
|
|
|
3
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
6
|
|
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
- [Install](#install)
|
|
16
16
|
- [Core concepts](#core-concepts)
|
|
17
17
|
- [Terminology](#terminology)
|
|
18
|
-
- [
|
|
18
|
+
- [Phantom object](#phantom-object)
|
|
19
19
|
- [Type constructs](#type-constructs)
|
|
20
20
|
- [Identities with constraints](#identities-with-constraints)
|
|
21
21
|
- [Traits (additive capabilities)](#traits-additive-capabilities)
|
|
@@ -90,7 +90,7 @@ pnpm add @vicin/phantom
|
|
|
90
90
|
|
|
91
91
|
---
|
|
92
92
|
|
|
93
|
-
###
|
|
93
|
+
### Phantom object
|
|
94
94
|
|
|
95
95
|
Under the hood `Phantom` is just **type-only** metadata object appended to your types and gets updated every time one of phantom types is used. This allows mimicking nominal typing inside TypeScript's structural typing.
|
|
96
96
|
|
|
@@ -449,11 +449,7 @@ Instead you can use `PhantomChain` class:
|
|
|
449
449
|
```ts
|
|
450
450
|
import { PhantomChain } from '@vicin/phantom';
|
|
451
451
|
|
|
452
|
-
const userId = new PhantomChain('123')
|
|
453
|
-
.with(asUserId)
|
|
454
|
-
.with(addPII)
|
|
455
|
-
.with(addValidated)
|
|
456
|
-
.end();
|
|
452
|
+
const userId = new PhantomChain('123').with(asUserId).with(addPII).with(addValidated).end();
|
|
457
453
|
```
|
|
458
454
|
|
|
459
455
|
The `.with()` is order sensitive, so previous example is equivalent to `addValidated(addPII(asUserId("123")))`. also if any `ErrorType` is retuned at any stage of the chain, the chain will break and error will propagate unchanged making debugging much easier.
|
|
@@ -784,12 +780,7 @@ Also it can be used in one-way transformations (e.g. `Hashed`):
|
|
|
784
780
|
import { Transformation, assertors } from '@vicin/phantom';
|
|
785
781
|
|
|
786
782
|
declare const __Hashed: unique symbol;
|
|
787
|
-
type Hashed<I = unknown> = Transformation.Declare<
|
|
788
|
-
I,
|
|
789
|
-
typeof __Hashed,
|
|
790
|
-
'Hashed value',
|
|
791
|
-
string
|
|
792
|
-
>;
|
|
783
|
+
type Hashed<I = unknown> = Transformation.Declare<I, typeof __Hashed, 'Hashed value', string>;
|
|
793
784
|
const applyHash = assertors.applyTransformation<Hashed>();
|
|
794
785
|
|
|
795
786
|
function hash<V>(value: V) {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
1
5
|
var __defProp = Object.defineProperty;
|
|
2
6
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
7
|
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
|
|
4
8
|
|
|
5
|
-
// src/assertors/brand.ts
|
|
6
|
-
var asBrand = () => (value) => value;
|
|
7
|
-
|
|
8
9
|
// src/assertors/identity.ts
|
|
9
10
|
var asIdentity = () => (value) => value;
|
|
10
11
|
|
|
@@ -22,14 +23,12 @@ var revertTransformation = () => (transformed, input) => input;
|
|
|
22
23
|
var _addTrait = addTrait;
|
|
23
24
|
var _addTraits = addTraits;
|
|
24
25
|
var _applyTransformation = applyTransformation;
|
|
25
|
-
var _asBrand = asBrand;
|
|
26
26
|
var _asIdentity = asIdentity;
|
|
27
27
|
var _dropTrait = dropTrait;
|
|
28
28
|
var _dropTraits = dropTraits;
|
|
29
29
|
var _revertTransformation = revertTransformation;
|
|
30
|
-
|
|
30
|
+
exports.assertors = void 0;
|
|
31
31
|
((assertors2) => {
|
|
32
|
-
assertors2.asBrand = _asBrand;
|
|
33
32
|
assertors2.asIdentity = _asIdentity;
|
|
34
33
|
assertors2.addTrait = _addTrait;
|
|
35
34
|
assertors2.addTraits = _addTraits;
|
|
@@ -37,7 +36,7 @@ var assertors;
|
|
|
37
36
|
assertors2.dropTraits = _dropTraits;
|
|
38
37
|
assertors2.applyTransformation = _applyTransformation;
|
|
39
38
|
assertors2.revertTransformation = _revertTransformation;
|
|
40
|
-
})(assertors || (assertors = {}));
|
|
39
|
+
})(exports.assertors || (exports.assertors = {}));
|
|
41
40
|
|
|
42
41
|
// src/chain/chain.ts
|
|
43
42
|
var PhantomChain = class _PhantomChain2 {
|
|
@@ -67,28 +66,26 @@ var PhantomChain = class _PhantomChain2 {
|
|
|
67
66
|
// src/core/phantom.ts
|
|
68
67
|
var stripPhantom = (value) => value;
|
|
69
68
|
var _stripPhantom = stripPhantom;
|
|
70
|
-
|
|
69
|
+
exports.PhantomCore = void 0;
|
|
71
70
|
((PhantomCore2) => {
|
|
72
71
|
PhantomCore2.stripPhantom = _stripPhantom;
|
|
73
|
-
})(PhantomCore || (PhantomCore = {}));
|
|
72
|
+
})(exports.PhantomCore || (exports.PhantomCore = {}));
|
|
74
73
|
|
|
75
74
|
// src/phantom.ts
|
|
76
75
|
var _addTrait2 = addTrait;
|
|
77
76
|
var _addTraits2 = addTraits;
|
|
78
77
|
var _applyTransformation2 = applyTransformation;
|
|
79
|
-
var _asBrand2 = asBrand;
|
|
80
78
|
var _asIdentity2 = asIdentity;
|
|
81
79
|
var _dropTrait2 = dropTrait;
|
|
82
80
|
var _dropTraits2 = dropTraits;
|
|
83
81
|
var _revertTransformation2 = revertTransformation;
|
|
84
82
|
var _PhantomChain = PhantomChain;
|
|
85
|
-
|
|
83
|
+
exports.Phantom = void 0;
|
|
86
84
|
((Phantom2) => {
|
|
87
85
|
((Inspect2) => {
|
|
88
|
-
Inspect2.stripPhantom = PhantomCore.stripPhantom;
|
|
86
|
+
Inspect2.stripPhantom = exports.PhantomCore.stripPhantom;
|
|
89
87
|
})(Phantom2.Inspect || (Phantom2.Inspect = {}));
|
|
90
88
|
((assertors3) => {
|
|
91
|
-
assertors3.asBrand = _asBrand2;
|
|
92
89
|
assertors3.asIdentity = _asIdentity2;
|
|
93
90
|
assertors3.addTrait = _addTrait2;
|
|
94
91
|
assertors3.addTraits = _addTraits2;
|
|
@@ -97,7 +94,6 @@ var Phantom;
|
|
|
97
94
|
assertors3.applyTransformation = _applyTransformation2;
|
|
98
95
|
assertors3.revertTransformation = _revertTransformation2;
|
|
99
96
|
})(Phantom2.assertors || (Phantom2.assertors = {}));
|
|
100
|
-
Phantom2.asBrand = _asBrand2;
|
|
101
97
|
Phantom2.asIdentity = _asIdentity2;
|
|
102
98
|
Phantom2.addTrait = _addTrait2;
|
|
103
99
|
Phantom2.addTraits = _addTraits2;
|
|
@@ -105,15 +101,24 @@ var Phantom;
|
|
|
105
101
|
Phantom2.dropTraits = _dropTraits2;
|
|
106
102
|
Phantom2.applyTransformation = _applyTransformation2;
|
|
107
103
|
Phantom2.revertTransformation = _revertTransformation2;
|
|
108
|
-
Phantom2.stripPhantom = PhantomCore.stripPhantom;
|
|
104
|
+
Phantom2.stripPhantom = exports.PhantomCore.stripPhantom;
|
|
109
105
|
class PhantomChain2 extends _PhantomChain {
|
|
110
106
|
}
|
|
111
107
|
Phantom2.PhantomChain = PhantomChain2;
|
|
112
|
-
})(Phantom || (Phantom = {}));
|
|
108
|
+
})(exports.Phantom || (exports.Phantom = {}));
|
|
113
109
|
|
|
114
110
|
// src/index.ts
|
|
115
|
-
var index_default = Phantom;
|
|
111
|
+
var index_default = exports.Phantom;
|
|
116
112
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
113
|
+
exports.PhantomChain = PhantomChain;
|
|
114
|
+
exports.addTrait = addTrait;
|
|
115
|
+
exports.addTraits = addTraits;
|
|
116
|
+
exports.applyTransformation = applyTransformation;
|
|
117
|
+
exports.asIdentity = asIdentity;
|
|
118
|
+
exports.default = index_default;
|
|
119
|
+
exports.dropTrait = dropTrait;
|
|
120
|
+
exports.dropTraits = dropTraits;
|
|
121
|
+
exports.revertTransformation = revertTransformation;
|
|
122
|
+
exports.stripPhantom = stripPhantom;
|
|
123
|
+
//# sourceMappingURL=index.cjs.map
|
|
124
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assertors/identity.ts","../src/assertors/trait.ts","../src/assertors/transformation.ts","../src/assertors/assertors.ts","../src/chain/chain.ts","../src/core/phantom.ts","../src/phantom.ts","../src/index.ts"],"names":["assertors","_PhantomChain","PhantomCore","_addTrait","_addTraits","_applyTransformation","_asIdentity","_dropTrait","_dropTraits","_revertTransformation","Phantom","Inspect","PhantomChain"],"mappings":"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","file":"index.cjs","sourcesContent":["import type { Identity } from '../core';\n\n/**\n * Creates a typed caster that assigns an {@link Identity} to a value.\n *\n * This is a zero-cost runtime assertion helper — it simply returns the value\n * with the identity's nominal type applied. Use it when you know a value\n * conforms to an identity but need to assert it for the type system.\n *\n * @template I - The identity declaration to assign.\n * @returns A function that casts any value to the assigned identity type.\n */\nexport const asIdentity =\n <I extends Identity.Any>() =>\n <V>(value: V): Identity.Assign<I, V> =>\n value as any;\n","import type { Trait } from '../core';\n\n/**\n * Creates a typed caster that adds a single {@link Trait} to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to add.\n * @returns A function that adds the trait to any value.\n */\nexport const addTrait =\n <Tr extends Trait.Any>() =>\n <V>(value: V): Trait.Add<Tr, V> =>\n value as any;\n\n/**\n * Creates a typed caster that adds multiple {@link Trait}s to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to add.\n * @returns A function that adds all traits to any value.\n */\nexport const addTraits =\n <Tr extends Trait.Any[]>() =>\n <V>(value: V): Trait.AddMulti<Tr, V> =>\n value as any;\n\n/**\n * Creates a typed caster that removes a single {@link Trait} from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to remove.\n * @returns A function that drops the trait from any value.\n */\nexport const dropTrait =\n <Tr extends Trait.Any>() =>\n <V>(value: V): Trait.Drop<Tr, V> =>\n value as any;\n\n/**\n * Creates a typed caster that removes multiple {@link Trait}s from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to remove.\n * @returns A function that drops all specified traits from any value.\n */\nexport const dropTraits =\n <Tr extends Trait.Any[]>() =>\n <V>(value: V): Trait.DropMulti<Tr, V> =>\n value as any;\n","import type { Transformation } from '../core';\n\n/**\n * Creates a typed applicator for a {@link Transformation}.\n *\n * Use this for \"forward\" operations (e.g., encrypt, encode, wrap).\n * The `input` parameter is only used for type inference — it is not used at runtime.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that applies the transformation while preserving the input type for later revert.\n */\nexport const applyTransformation =\n <Tr extends Transformation.Any>() =>\n <I, T>(input: I, transformed: T) =>\n transformed as Transformation.Apply<Tr, I, T>;\n\n/**\n * Creates a typed reverter for a {@link Transformation}.\n *\n * Use this for \"reverse\" operations (e.g., decrypt, decode, unwrap).\n * The `transformed` parameter is used for type inference of the expected input,\n * and `input` is the computed result that must match the stored input type.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that reverts the transformation, stripping phantom metadata.\n */\nexport const revertTransformation =\n <Tr extends Transformation.Any>() =>\n <T, I>(transformed: T, input: I) =>\n input as Transformation.Revert<Tr, T, I>;\n","import { asIdentity } from './identity';\nimport { addTrait, addTraits, dropTrait, dropTraits } from './trait';\nimport { applyTransformation, revertTransformation } from './transformation';\n\nconst _addTrait = addTrait;\nconst _addTraits = addTraits;\nconst _applyTransformation = applyTransformation;\nconst _asIdentity = asIdentity;\nconst _dropTrait = dropTrait;\nconst _dropTraits = dropTraits;\nconst _revertTransformation = revertTransformation;\n\nexport namespace assertors {\n /**\n * Creates a typed caster that assigns an {@link Identity} to a value.\n *\n * This is a zero-cost runtime assertion helper — it simply returns the value\n * with the identity's nominal type applied. Use it when you know a value\n * conforms to an identity but need to assert it for the type system.\n *\n * @template I - The identity declaration to assign.\n * @returns A function that casts any value to the assigned identity type.\n */\n export const asIdentity = _asIdentity;\n\n /**\n * Creates a typed caster that adds a single {@link Trait} to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to add.\n * @returns A function that adds the trait to any value.\n */\n export const addTrait = _addTrait;\n\n /**\n * Creates a typed caster that adds multiple {@link Trait}s to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to add.\n * @returns A function that adds all traits to any value.\n */\n export const addTraits = _addTraits;\n\n /**\n * Creates a typed caster that removes a single {@link Trait} from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to remove.\n * @returns A function that drops the trait from any value.\n */\n export const dropTrait = _dropTrait;\n\n /**\n * Creates a typed caster that removes multiple {@link Trait}s from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to remove.\n * @returns A function that drops all specified traits from any value.\n */\n export const dropTraits = _dropTraits;\n\n /**\n * Creates a typed applicator for a {@link Transformation}.\n *\n * Use this for \"forward\" operations (e.g., encrypt, encode, wrap).\n * The `input` parameter is only used for type inference — it is not used at runtime.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that applies the transformation while preserving the input type for later revert.\n */\n export const applyTransformation = _applyTransformation;\n\n /**\n * Creates a typed reverter for a {@link Transformation}.\n *\n * Use this for \"reverse\" operations (e.g., decrypt, decode, unwrap).\n * The `transformed` parameter is used for type inference of the expected input,\n * and `input` is the computed result that must match the stored input type.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that reverts the transformation, stripping phantom metadata.\n */\n export const revertTransformation = _revertTransformation;\n}\n","/**\n * A fluent PhantomChain class for chaining Phantom assertors.\n *\n * This provides a better developer experience (DX) by allowing method chaining\n * with `.with(assertor)` instead of nesting function calls or using a variadic chain.\n * Each `.with()` applies the assertor to the current value, updating the type incrementally.\n * Call `.end()` to retrieve the final value.\n *\n * At runtime, assertors are zero-cost casts, so the PhantomChain adds minimal overhead\n * (just object creation and method calls).\n *\n * Example:\n * ```ts\n * const asMyBrand = Phantom.assertors.asBrand<MyBrand>();\n * const asMyTrait = Phantom.assertors.asTrait<MyTrait>();\n * const applyMyTransform = Phantom.assertors.applyTransformation<MyTransform>();\n *\n * const result = new PhantomChain(\"value\")\n * .with(asMyBrand)\n * .with(asMyTrait)\n * .with(applyMyTransform)\n * .end();\n * ```\n */\nexport class PhantomChain<T> {\n private value: T;\n\n constructor(value: T) {\n this.value = value;\n }\n\n /**\n * Apply the next assertor in the chain.\n *\n * @param assertor A function that takes the current value and returns the updated value (with new type).\n * @returns A new PhantomChain instance with the updated value and type.\n */\n with<U>(assertor: (value: T) => U): PhantomChain<U> {\n return new PhantomChain(assertor(this.value));\n }\n\n /**\n * End the chain and return the final value.\n *\n * @returns The value after all transformations.\n */\n end(): T {\n return this.value;\n }\n}\n","import type { __Phantom, __OriginalType, __Base } from './symbols';\n\n/** Stip phantom metadata object from a type */\ntype StripPhantom<T> = T extends {\n [__Phantom]: { [__OriginalType]?: infer O }; // If type result from ( .Assign / .Add / .Apply )\n}\n ? Exclude<O, undefined>\n : T extends {\n [__Phantom]: { [__Base]?: infer B }; // If type result from ( Identity.Declare / Transformation.Declare )\n }\n ? Exclude<B, undefined>\n : T extends { [__Phantom]: object } // If type result from ( Trait.Declare )\n ? unknown\n : T;\n\n/** run-time helper for 'StringPhantom', used for debugging mainly */\nexport const stripPhantom = <T>(value: T): StripPhantom<T> => value as any;\n\n// Avoid bundler bugs\ntype _StripPhantom<T> = StripPhantom<T>;\nconst _stripPhantom = stripPhantom;\n\n/**\n * Phantom meatadata object manipulators.\n *\n * Phantom matadata object holds all metadata used by 'phantom'.\n */\nexport namespace PhantomCore {\n /** Get phantom metadata object from a type */\n export type PhantomOf<T> = T extends {\n __Phantom: infer Phantom extends object;\n }\n ? Phantom\n : never;\n\n /** Stip phantom metadata object from a type */\n export type StripPhantom<T> = _StripPhantom<T>;\n\n /** run-time helper for 'StringPhantom', used for debugging mainly */\n export const stripPhantom = _stripPhantom;\n}\n","import {\n addTrait,\n addTraits,\n applyTransformation,\n asIdentity,\n dropTrait,\n dropTraits,\n revertTransformation,\n} from './assertors';\nimport { PhantomChain } from './chain';\nimport type {\n BaseCore,\n InputCore,\n LabelCore,\n TagCore,\n TraitsCore,\n VariantsCore,\n IdentityCore,\n TraitCore,\n TransformationCore,\n ErrorType,\n __Base,\n __Input,\n __Label,\n __OriginalType,\n __Phantom,\n __Tag,\n __Traits,\n __Variants,\n} from './core';\nimport { PhantomCore } from './core';\n\nconst _addTrait = addTrait;\nconst _addTraits = addTraits;\nconst _applyTransformation = applyTransformation;\nconst _asIdentity = asIdentity;\nconst _dropTrait = dropTrait;\nconst _dropTraits = dropTraits;\nconst _revertTransformation = revertTransformation;\nconst _PhantomChain = PhantomChain;\ntype _ErrorType<E> = ErrorType<E>;\n\nexport namespace Phantom {\n /** --------------------------------------\n * Types\n * --------------------------------------- */\n\n /**\n * Optional human-readable label metadata.\n *\n * Labels are descriptive only and do not affect identity.\n */\n export namespace Label {\n /** Marker type for labeled values */\n export type Any = LabelCore.Any;\n /** Extract the label */\n export type LabelOf<T> = LabelCore.LabelOf<T>;\n /** Check whether a label exists */\n export type HasLabel<T, L extends string = string> = LabelCore.HasLabel<T, L>;\n }\n\n /**\n * Nominal tag metadata.\n *\n * Tags uniquely identify a branded or identified type.\n * A value may only have a single tag.\n */\n export namespace Tag {\n /** Marker type for any tagged value */\n export type Any = TagCore.Any;\n /** Extract the tag from a type */\n export type TagOf<T> = TagCore.TagOf<T>;\n /** Check whether a type is tagged */\n export type HasTag<T, Ta extends string | symbol = string | symbol> = TagCore.HasTag<T, Ta>;\n }\n\n /**\n * Variant metadata.\n *\n * Variants represent mutually exclusive states of a type.\n */\n export namespace Variants {\n /** Marker type for variant-bearing values */\n export type Any = VariantsCore.Any;\n /** Extract variant union */\n export type VariantsOf<T> = VariantsCore.VariantsOf<T>;\n /** Check whether variants exist */\n export type HasVariants<T> = VariantsCore.HasVariants<T>;\n }\n\n /**\n * Base-type metadata.\n *\n * Used to constrain which runtime types a brand, identity,\n * or transformation may be applied to.\n */\n export namespace Base {\n /** Marker type for base constraints */\n export type Any = BaseCore.Any;\n /** Extract the base type */\n export type BaseOf<T> = BaseCore.BaseOf<T>;\n /** Check whether a base constraint exists */\n export type HasBase<T, B = unknown> = BaseCore.HasBase<T, B>;\n }\n\n /**\n * Input metadata.\n *\n * Utilities for attaching and querying input metadata in transformations.\n */\n export namespace Input {\n /** Marker type for input value */\n export type Any = InputCore.Any;\n /** Extract the input */\n export type InputOf<T> = InputCore.InputOf<T>;\n /** Check whether an input exists */\n export type HasInput<T, I = unknown> = InputCore.HasInput<T, I>;\n }\n\n /**\n * Trait metadata.\n *\n * Traits behave like a set of capabilities that can be\n * added or removed independently.\n */\n export namespace Traits {\n /** Marker type for trait-bearing values */\n export type Any = TraitsCore.Any;\n /** Extract the trait map */\n export type TraitsOf<T> = TraitsCore.TraitsOf<T>;\n /** Extract trait keys */\n export type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;\n /** Check if any traits exist */\n export type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<\n T,\n Tr\n >;\n }\n\n /**\n * Identity API.\n *\n * Identities are brands with additional constraints:\n * - Base type\n * - Variants\n */\n export namespace Identity {\n /** Type guard for any identity. */\n export type Any = IdentityCore.Any;\n /** Declare an identity */\n export type Declare<\n T extends string | symbol,\n L extends string = never,\n B = never,\n V extends string = never,\n > = IdentityCore.Declare<T, L, B, V>;\n /** Assign an identity to a value. Enforces base-type compatibility */\n export type Assign<I extends Any, T> = IdentityCore.Assign<I, T>;\n /** Safe identity assignment */\n export type AssignSafe<I extends Any, T> = IdentityCore.AssignSafe<I, T>;\n /** Set the active variant on an identity */\n export type WithVariant<\n I extends Any,\n V extends Variants.VariantsOf<I>,\n > = IdentityCore.WithVariant<I, V>;\n /** Set the active variant on a value */\n export type WithTypeVariant<T, V extends Variants.VariantsOf<T>> = IdentityCore.WithTypeVariant<\n T,\n V\n >;\n /** Check whether value is branded with */\n export type isIdentity<T, I extends Any> = IdentityCore.isIdentity<T, I>;\n }\n\n /**\n * Trait API.\n *\n * Traits are additive capabilities that can be attached\n * or removed independently.\n */\n export namespace Trait {\n /** Type guard for any trait. */\n export type Any = TraitCore.Any;\n /** Declare a trait */\n export type Declare<Tr extends string | symbol> = TraitCore.Declare<Tr>;\n /** Add a trait */\n export type Add<Tr extends Any, T> = TraitCore.Add<Tr, T>;\n /** Add multiple traits */\n export type AddMulti<Tr extends readonly Any[], T> = TraitCore.AddMulti<Tr, T>;\n /** Remove a trait */\n export type Drop<Tr extends Any, T> = TraitCore.Drop<Tr, T>;\n /** Remove multiple traits */\n export type DropMulti<Tr extends readonly Any[], T> = TraitCore.DropMulti<Tr, T>;\n /** Check whether value has trait */\n export type HasTrait<T, Tr extends Any> = TraitCore.HasTrait<T, Tr>;\n }\n\n /**\n * Transformation API.\n *\n * Transformations represent reversible operations that\n * change the shape of a value while preserving its origin.\n */\n export namespace Transformation {\n /** Type guard for any transformation. */\n export type Any = TransformationCore.Any;\n /** Declare a transformation */\n export type Declare<\n I,\n T extends string | symbol,\n L extends string = never,\n B = never,\n V extends string = never,\n > = TransformationCore.Declare<I, T, L, B, V>;\n /** Apply a transformation to a value. Enforces base-type compatibility */\n export type Apply<Tr extends Any, I, T> = TransformationCore.Apply<Tr, I, T>;\n /** Revert a transformation */\n export type Revert<Tr extends Any, T, I> = TransformationCore.Revert<Tr, T, I>;\n /** Revert a transformation whatever transformation was */\n export type RevertAny<T, I> = TransformationCore.RevertAny<T, I>;\n /** Check whether value is transformed with */\n export type isTransformed<T, Tr extends Any> = TransformationCore.isTransformed<T, Tr>;\n }\n\n /**\n * Inspect API.\n *\n * Inspection helpers of phantom types.\n */\n export namespace Inspect {\n /** Get phantom metadata object from a type */\n export type PhantomOf<T> = PhantomCore.PhantomOf<T>;\n /** Stip phantom metadata object from a type */\n export type StripPhantom<T> = PhantomCore.StripPhantom<T>;\n /** run-time helper for 'StringPhantom', used for debugging mainly */\n export const stripPhantom = PhantomCore.stripPhantom;\n /** Extract the label */\n export type LabelOf<T> = LabelCore.LabelOf<T>;\n /** Check whether a base constraint exists */\n export type HasLabel<T, L extends string = string> = LabelCore.HasLabel<T, L>;\n /** Extract the tag from a type */\n export type TagOf<T> = TagCore.TagOf<T>;\n /** Check whether a type is tagged */\n export type HasTag<T, Ta extends string | symbol = string | symbol> = TagCore.HasTag<T, Ta>;\n /** Extract variant union */\n export type VariantsOf<T> = VariantsCore.VariantsOf<T>;\n /** Check whether variants exist */\n export type HasVariants<T> = VariantsCore.HasVariants<T>;\n /** Extract the base type */\n export type BaseOf<T> = BaseCore.BaseOf<T>;\n /** Check whether a base constraint exists */\n export type HasBase<T, B = unknown> = BaseCore.HasBase<T, B>;\n /** Extract the input */\n export type InputOf<T> = InputCore.InputOf<T>;\n /** Check whether an input exists */\n export type HasInput<T, I = unknown> = InputCore.HasInput<T, I>;\n /** Extract the trait map */\n export type TraitsOf<T> = TraitsCore.TraitsOf<T>;\n /** Extract trait keys */\n export type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;\n /** Check if any traits exist */\n export type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<\n T,\n Tr\n >;\n /** Check whether value is branded with */\n export type isIdentity<T, I extends Identity.Any> = IdentityCore.isIdentity<T, I>;\n /** Check whether value has trait */\n export type HasTrait<T, Tr extends Trait.Any> = TraitCore.HasTrait<T, Tr>;\n /** Check whether value is transformed with */\n export type isTransformed<T, Tr extends Transformation.Any> = TransformationCore.isTransformed<\n T,\n Tr\n >;\n }\n\n /** --------------------------------------\n * assertors\n * --------------------------------------- */\n\n export namespace assertors {\n /**\n * Creates a typed caster that assigns an {@link Identity} to a value.\n *\n * This is a zero-cost runtime assertion helper — it simply returns the value\n * with the identity's nominal type applied. Use it when you know a value\n * conforms to an identity but need to assert it for the type system.\n *\n * @template I - The identity declaration to assign.\n * @returns A function that casts any value to the assigned identity type.\n */\n export const asIdentity = _asIdentity;\n\n /**\n * Creates a typed caster that adds a single {@link Trait} to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to add.\n * @returns A function that adds the trait to any value.\n */\n export const addTrait = _addTrait;\n\n /**\n * Creates a typed caster that adds multiple {@link Trait}s to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to add.\n * @returns A function that adds all traits to any value.\n */\n export const addTraits = _addTraits;\n\n /**\n * Creates a typed caster that removes a single {@link Trait} from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to remove.\n * @returns A function that drops the trait from any value.\n */\n export const dropTrait = _dropTrait;\n\n /**\n * Creates a typed caster that removes multiple {@link Trait}s from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to remove.\n * @returns A function that drops all specified traits from any value.\n */\n export const dropTraits = _dropTraits;\n\n /**\n * Creates a typed applicator for a {@link Transformation}.\n *\n * Use this for \"forward\" operations (e.g., encrypt, encode, wrap).\n * The `input` parameter is only used for type inference — it is not used at runtime.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that applies the transformation while preserving the input type for later revert.\n */\n export const applyTransformation = _applyTransformation;\n\n /**\n * Creates a typed reverter for a {@link Transformation}.\n *\n * Use this for \"reverse\" operations (e.g., decrypt, decode, unwrap).\n * The `transformed` parameter is used for type inference of the expected input,\n * and `input` is the computed result that must match the stored input type.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that reverts the transformation, stripping phantom metadata.\n */\n export const revertTransformation = _revertTransformation;\n }\n\n /**\n * Creates a typed caster that assigns an {@link Identity} to a value.\n *\n * This is a zero-cost runtime assertion helper — it simply returns the value\n * with the identity's nominal type applied. Use it when you know a value\n * conforms to an identity but need to assert it for the type system.\n *\n * @template I - The identity declaration to assign.\n * @returns A function that casts any value to the assigned identity type.\n */\n export const asIdentity = _asIdentity;\n\n /**\n * Creates a typed caster that adds a single {@link Trait} to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to add.\n * @returns A function that adds the trait to any value.\n */\n export const addTrait = _addTrait;\n\n /**\n * Creates a typed caster that adds multiple {@link Trait}s to a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to add.\n * @returns A function that adds all traits to any value.\n */\n export const addTraits = _addTraits;\n\n /**\n * Creates a typed caster that removes a single {@link Trait} from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The trait declaration to remove.\n * @returns A function that drops the trait from any value.\n */\n export const dropTrait = _dropTrait;\n\n /**\n * Creates a typed caster that removes multiple {@link Trait}s from a value.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - Tuple of trait declarations to remove.\n * @returns A function that drops all specified traits from any value.\n */\n export const dropTraits = _dropTraits;\n\n /**\n * Creates a typed applicator for a {@link Transformation}.\n *\n * Use this for \"forward\" operations (e.g., encrypt, encode, wrap).\n * The `input` parameter is only used for type inference — it is not used at runtime.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that applies the transformation while preserving the input type for later revert.\n */\n export const applyTransformation = _applyTransformation;\n\n /**\n * Creates a typed reverter for a {@link Transformation}.\n *\n * Use this for \"reverse\" operations (e.g., decrypt, decode, unwrap).\n * The `transformed` parameter is used for type inference of the expected input,\n * and `input` is the computed result that must match the stored input type.\n *\n * Zero-runtime-cost assertion helper.\n *\n * @template Tr - The transformation declaration.\n * @returns A function that reverts the transformation, stripping phantom metadata.\n */\n export const revertTransformation = _revertTransformation;\n\n /** --------------------------------------\n * Phantom object manipulators\n * --------------------------------------- */\n\n /** Get phantom metadata object from a type */\n export type PhantomOf<T> = PhantomCore.PhantomOf<T>;\n /** Stip phantom metadata object from a type */\n export type StripPhantom<T> = PhantomCore.StripPhantom<T>;\n /** run-time helper for 'StringPhantom', used for debugging mainly */\n export const stripPhantom = PhantomCore.stripPhantom;\n\n /** --------------------------------------\n * Error type\n * --------------------------------------- */\n\n /** Unique Error type for rules validation in phantom. */\n export type ErrorType<E> = _ErrorType<E>;\n\n /** --------------------------------------\n * Chain class\n * --------------------------------------- */\n\n /**\n * A fluent PhantomChain class for chaining Phantom assertors.\n *\n * This provides a better developer experience (DX) by allowing method chaining\n * with `.with(assertor)` instead of nesting function calls or using a variadic chain.\n * Each `.with()` applies the assertor to the current value, updating the type incrementally.\n * Call `.end()` to retrieve the final value.\n *\n * At runtime, assertors are zero-cost casts, so the PhantomChain adds minimal overhead\n * (just object creation and method calls).\n *\n * Example:\n * ```ts\n * const asMyBrand = Phantom.assertors.asBrand<MyBrand>();\n * const asMyTrait = Phantom.assertors.asTrait<MyTrait>();\n * const applyMyTransform = Phantom.assertors.applyTransformation<MyTransform>();\n *\n * const result = new PhantomChain(\"value\")\n * .with(asMyBrand)\n * .with(asMyTrait)\n * .with(applyMyTransform)\n * .end();\n * ```\n */\n export class PhantomChain<T> extends _PhantomChain<T> {}\n}\n","export {\n assertors,\n addTrait,\n addTraits,\n applyTransformation,\n asIdentity,\n dropTrait,\n dropTraits,\n revertTransformation,\n} from './assertors';\nexport { PhantomChain } from './chain';\nexport { Phantom } from './phantom';\nexport type {\n Base,\n ErrorType,\n Identity,\n Input,\n Inspect,\n Label,\n Tag,\n Trait,\n Traits,\n Transformation,\n Variants,\n __Base,\n __Input,\n __Label,\n __OriginalType,\n __Phantom,\n __Tag,\n __Traits,\n __Variants,\n} from './core';\nexport { PhantomCore, stripPhantom } from './core';\nimport { Phantom } from './phantom';\nexport default Phantom;\n"]}
|