@vicin/phantom 1.0.8 → 1.1.1
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 +39 -82
- package/dist/index.d.mts +74 -11
- package/dist/index.d.ts +74 -11
- package/dist/index.global.js +8 -5
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +8 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +15 -2
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
> - 🎉 First stable release — v1.0! Happy coding! 😄💻
|
|
6
6
|
> - 📄 **Changelog:** [CHANGELOG.md](./CHANGELOG.md)
|
|
7
7
|
|
|
8
|
-
`Phantom` is a powerful, lightweight TypeScript library for nominal typing. it uses **type-only** metadata object that can be attached to any type with clean IDE. It enables compile-time distinctions between structurally identical types (e.g., `Email` vs. `Username` as branded strings) while supporting advanced features like **constrained identities**,**state variants**, **additive traits**, and **reversible transformations**. making it ideal for domain-driven design (DDD), APIs, and type-safe primitives with
|
|
8
|
+
`Phantom` is a powerful, lightweight TypeScript library for nominal typing. it uses **type-only** metadata object that can be attached to any type with clean IDE. It enables compile-time distinctions between structurally identical types (e.g., `Email` vs. `Username` as branded strings) while supporting advanced features like **constrained identities**,**state variants**, **additive traits**, and **reversible transformations**. making it ideal for domain-driven design (DDD), APIs, and type-safe primitives with near zero performance overhead.
|
|
9
9
|
|
|
10
10
|
---
|
|
11
11
|
|
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
- [Terminology](#terminology)
|
|
18
18
|
- [\_\_Phantom object](#__phantom-object)
|
|
19
19
|
- [Type constructs](#type-constructs)
|
|
20
|
-
- [Branding](#branding)
|
|
21
20
|
- [Identities with constraints](#identities-with-constraints)
|
|
22
21
|
- [Traits (additive capabilities)](#traits-additive-capabilities)
|
|
23
22
|
- [Transformations (Type pipe-like behavior)](#transformations-type-pipe-like-behavior)
|
|
@@ -29,6 +28,7 @@
|
|
|
29
28
|
- [Debugging](#debugging)
|
|
30
29
|
- [API reference](#api-reference)
|
|
31
30
|
- [Full examples](#full-examples)
|
|
31
|
+
- [Deprecated API](#deprecated-api)
|
|
32
32
|
- [Sigil](#sigil)
|
|
33
33
|
- [Contributing](#contributing)
|
|
34
34
|
- [License](#license)
|
|
@@ -101,6 +101,7 @@ type X = string & {
|
|
|
101
101
|
__Phantom: {
|
|
102
102
|
__Tag: 'X';
|
|
103
103
|
__Label?: 'Label of X';
|
|
104
|
+
__Base?: string;
|
|
104
105
|
__Variants: 'Variant1' | 'Variant2';
|
|
105
106
|
__Traits: {
|
|
106
107
|
trait1: true;
|
|
@@ -118,6 +119,8 @@ From the first glance you can spot that`Phantom` is designed to separate origina
|
|
|
118
119
|
|
|
119
120
|
- **\_\_Label?:** Optional label describing this nominal identity.
|
|
120
121
|
|
|
122
|
+
- **\_\_Base?:** Optional constraint to nominal identity, so only values that extend this type can be branded.
|
|
123
|
+
|
|
121
124
|
- **\_\_Variants:** Optional states of the nominal identity.
|
|
122
125
|
|
|
123
126
|
- **\_\_Traits:** Additional properties the type holds, unlike `__Tag` that tells who `__Traits` tell what this type hold or can do (e.g. `Validated`, `PII` etc...).
|
|
@@ -132,51 +135,6 @@ More details of these properties will be discussed later.
|
|
|
132
135
|
|
|
133
136
|
## Type constructs
|
|
134
137
|
|
|
135
|
-
### Branding
|
|
136
|
-
|
|
137
|
-
Use brands for simple nominal distinctions on primitives.
|
|
138
|
-
|
|
139
|
-
#### Structure
|
|
140
|
-
|
|
141
|
-
**`Brand.Declare<Tag, Label?>`**
|
|
142
|
-
|
|
143
|
-
- **Tag:** Unique identifier which can string or symbol.
|
|
144
|
-
- **Label?:** Optional label description. can be set to `never`.
|
|
145
|
-
|
|
146
|
-
#### Basic example
|
|
147
|
-
|
|
148
|
-
```ts
|
|
149
|
-
import { Phantom } from '@vicin/phantom';
|
|
150
|
-
|
|
151
|
-
// Declare a brand
|
|
152
|
-
type UserId = Phantom.Brand.Declare<'UserId', 'Unique user identifier'>;
|
|
153
|
-
|
|
154
|
-
// Assertor for assigning the brand
|
|
155
|
-
const asUserId = Phantom.assertors.asBrand<UserId>();
|
|
156
|
-
|
|
157
|
-
// Usage
|
|
158
|
-
const id: string = '123';
|
|
159
|
-
const userId = asUserId(id); // type: string & { __Phantom: { __Tag: "UserId"; __Label?: "Unique user identifier"; __OriginalType: string; } }
|
|
160
|
-
|
|
161
|
-
// Type safety: prevents assignment
|
|
162
|
-
let postId: string = 'abc';
|
|
163
|
-
postId = userId; // Type error: 'UserId' is not assignable to unbranded string
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
#### Re-brand
|
|
167
|
-
|
|
168
|
-
Branding already branded value is prohibited and results in Error type:
|
|
169
|
-
|
|
170
|
-
```ts
|
|
171
|
-
const value = '123';
|
|
172
|
-
|
|
173
|
-
// Branding string as UserId
|
|
174
|
-
const userId = asUserId(value);
|
|
175
|
-
|
|
176
|
-
// Trying to brand userId as PostId returns type error
|
|
177
|
-
const postId = asPostId(userId); // type: Flow.TypeError<{ code: "ALREADY_BRANDED"; message: "Type already branded"; context: { type: <userId type>; }; }>
|
|
178
|
-
```
|
|
179
|
-
|
|
180
138
|
---
|
|
181
139
|
|
|
182
140
|
### Identities with constraints
|
|
@@ -189,7 +147,8 @@ const postId = asPostId(userId); // type: Flow.TypeError<{ code: "ALREADY_BRANDE
|
|
|
189
147
|
|
|
190
148
|
- **Tag:** Unique identifier which can string or symbol.
|
|
191
149
|
- **Label?:** Optional label description. can be set to `never`.
|
|
192
|
-
- **Base?:** Optional base that constrains identity so only `string` for example can be casted as `Email`. can be set to `never`.
|
|
150
|
+
- **Base?:** Optional base that constrains identity so only `string` for example can be casted as `Email`. can be set to `never` or `unknown`.
|
|
151
|
+
Type passed is also added to declared `Identity` (`<Base> & <PhantomObject>`).
|
|
193
152
|
- **Variants?:** Optional variants (states) of the identity. can be set to `never`.
|
|
194
153
|
|
|
195
154
|
#### Basic example
|
|
@@ -320,7 +279,8 @@ Type identity is `Transformed` and stores `Input` under special field inside `__
|
|
|
320
279
|
- **Input:** Type input to the transformation.
|
|
321
280
|
- **Tag:** Unique identifier of transformed which can string or symbol.
|
|
322
281
|
- **Label?:** Optional label description of transformed. can be set to `never`.
|
|
323
|
-
- **Base?:** Optional base that constrains identity of transformed so only `Uint8Array` for example can be casted as `Encoded`. can be set to `never`.
|
|
282
|
+
- **Base?:** Optional base that constrains identity of transformed so only `Uint8Array` for example can be casted as `Encoded`. can be set to `never` or `unknown`.
|
|
283
|
+
Type passed is also added to declared `Transformation` (`<Base> & <PhantomObject>`).
|
|
324
284
|
- **Variants?:** Optional variants (states) of the identity. can be set to `never`.
|
|
325
285
|
|
|
326
286
|
#### Basic example
|
|
@@ -394,7 +354,7 @@ When passed value violate specific rule, descripted `ErrorType` is returned.
|
|
|
394
354
|
|
|
395
355
|
**ErrorTypes:**
|
|
396
356
|
|
|
397
|
-
- **ALREADY_BRANDED:** Error returned if already branded value (with
|
|
357
|
+
- **ALREADY_BRANDED:** Error returned if already branded value (with `Identity`) is passed to `.Assign<>` again.
|
|
398
358
|
|
|
399
359
|
- **TYPE_NOT_EXTEND_BASE:** Error returned if type not extend `Base` constraint of `Identity`.
|
|
400
360
|
|
|
@@ -402,7 +362,7 @@ When passed value violate specific rule, descripted `ErrorType` is returned.
|
|
|
402
362
|
|
|
403
363
|
- **NOT_TRANSFORMED:** Error returned if you tried to revert a non-transformed type.
|
|
404
364
|
|
|
405
|
-
All the types inside `Phantom` check for `ErrorType` first before applying any change, so if you tried to pass `ErrorType` to `
|
|
365
|
+
All the types inside `Phantom` check for `ErrorType` first before applying any change, so if you tried to pass `ErrorType` to `Identity.Assign<>` or `asIdentity<B>()` for example the error will return unchanged.
|
|
406
366
|
|
|
407
367
|
---
|
|
408
368
|
|
|
@@ -415,7 +375,7 @@ import { Phantom } from '@vicin/phantom';
|
|
|
415
375
|
|
|
416
376
|
// type
|
|
417
377
|
declare const __UserId: unique symbol;
|
|
418
|
-
type UserId = Phantom.
|
|
378
|
+
type UserId = Phantom.Identity.Declare<typeof __UserId, 'User id'>;
|
|
419
379
|
|
|
420
380
|
// assertor
|
|
421
381
|
export const asUserId = Phantom.assertors.asBrand<UserId>();
|
|
@@ -430,11 +390,11 @@ Now `UserId` can't be re-defined anywhere else in the codebase.
|
|
|
430
390
|
Some devs (including me) may find `Phantom` namespace everywhere repetitive and prefer using `Brand` or `assertors` directly, every namespace under `Phantom` can be imported alone:
|
|
431
391
|
|
|
432
392
|
```ts
|
|
433
|
-
import {
|
|
393
|
+
import { Identity, assertors } from '@vicin/phantom';
|
|
434
394
|
|
|
435
395
|
// type
|
|
436
396
|
declare const __UserId: unique symbol; // declare type only unique symbol to be our tag
|
|
437
|
-
type UserId =
|
|
397
|
+
type UserId = Identity.Declare<typeof __UserId, 'User id'>;
|
|
438
398
|
|
|
439
399
|
// assertor
|
|
440
400
|
export const asUserId = assertors.asBrand<UserId>();
|
|
@@ -447,13 +407,13 @@ import P from '@vicin/phantom';
|
|
|
447
407
|
|
|
448
408
|
// type
|
|
449
409
|
declare const __UserId: unique symbol; // declare type only unique symbol to be our tag
|
|
450
|
-
type UserId = P.
|
|
410
|
+
type UserId = P.Identity.Declare<typeof __UserId, 'User id'>;
|
|
451
411
|
|
|
452
412
|
// assertor
|
|
453
413
|
export const asUserId = P.assertors.asBrand<UserId>();
|
|
454
414
|
```
|
|
455
415
|
|
|
456
|
-
Also you can import single assertor function:
|
|
416
|
+
Also you can import single run-time function as `assertor functions` and `stripPhantom` function:
|
|
457
417
|
|
|
458
418
|
```ts
|
|
459
419
|
import { asBrand } from '@vicin/phantom';
|
|
@@ -470,9 +430,9 @@ You are free to pick whatever pattern you are comfortable with.
|
|
|
470
430
|
If you need to call multiple assertors on a single value (e.g. mark brand and attach two traits) the code can get messy:
|
|
471
431
|
|
|
472
432
|
```ts
|
|
473
|
-
import {
|
|
433
|
+
import { Identity, Trait, assertors } from '@vicin/phantom';
|
|
474
434
|
|
|
475
|
-
type UserId =
|
|
435
|
+
type UserId = Identity.Declare<'UserId', 'User id', string>;
|
|
476
436
|
const asUserId = assertors.asBrand<UserId>();
|
|
477
437
|
|
|
478
438
|
type PII = Trait.Declare<'PII'>;
|
|
@@ -508,17 +468,6 @@ The `.with()` is order sensitive, so previous example is equivalent to `addValid
|
|
|
508
468
|
|
|
509
469
|
In hot-path code every function call matters, and although assertor functions makes code much cleaner but you may prefer to use `as` in performance critical situations so `Phantom` lives in type system entirely and have zero run-time trace, these examples defines best practices to do so:
|
|
510
470
|
|
|
511
|
-
**Brand**:
|
|
512
|
-
|
|
513
|
-
```ts
|
|
514
|
-
import { Brand } from '@vicin/phantom';
|
|
515
|
-
|
|
516
|
-
type UserId = Brand.Declare<'UserId'>;
|
|
517
|
-
type AsUserId<T> = Brand.Assign<UserId, T>;
|
|
518
|
-
|
|
519
|
-
const userId = '123' as AsUserId<string>;
|
|
520
|
-
```
|
|
521
|
-
|
|
522
471
|
**Identity**:
|
|
523
472
|
|
|
524
473
|
```ts
|
|
@@ -568,10 +517,10 @@ function decode<E extends Encoded>(encoded: E) {
|
|
|
568
517
|
Chaining is not supported in **type-only pattern** and nesting is the only way to reliably calculate types:
|
|
569
518
|
|
|
570
519
|
```ts
|
|
571
|
-
import {
|
|
520
|
+
import { Identity, Trait, assertors } from '@vicin/phantom';
|
|
572
521
|
|
|
573
|
-
type UserId =
|
|
574
|
-
type AsUserId<T> =
|
|
522
|
+
type UserId = Identity.Declare<'UserId', 'User id', string>;
|
|
523
|
+
type AsUserId<T> = Identity.Assign<UserId, T>;
|
|
575
524
|
|
|
576
525
|
type PII = Trait.Declare<'PII'>;
|
|
577
526
|
type AddPII<T> = Trait.Add<PII, T>;
|
|
@@ -635,13 +584,6 @@ These handle individual metadata fields in the `__Phantom` object.
|
|
|
635
584
|
|
|
636
585
|
These provide nominal typing and metadata operations.
|
|
637
586
|
|
|
638
|
-
- **Brand:** Simple nominal branding.
|
|
639
|
-
- `Any`: Marker for branded types.
|
|
640
|
-
- `Declare<T, L>`: Declare a brand with tag `T` and optional label `L`.
|
|
641
|
-
- `Assign<B, T>`: Assign brand `B` to `T` (fails if already branded).
|
|
642
|
-
- `AssignSafe<B, T>`: Assign if possible, else return `T`.
|
|
643
|
-
- `isBrand<T, B>`: Check if `T` is branded with `B`.
|
|
644
|
-
|
|
645
587
|
- **Identity**: Brands with bases and variants.
|
|
646
588
|
- `Any`: Marker for identity types.
|
|
647
589
|
- `Declare<T, L, B, V>`: Declare identity with tag `T`, label `L`, base `B`, variants `V`.
|
|
@@ -682,7 +624,6 @@ Aliases for core and feature extractors: `LabelOf<T>`, `HasLabel<T, L>`, `TagOf<
|
|
|
682
624
|
|
|
683
625
|
Zero-cost casting functions.
|
|
684
626
|
|
|
685
|
-
`asBrand<B>()`: Assign brand `B`.
|
|
686
627
|
`asIdentity<I>()`: Assign identity `I`.
|
|
687
628
|
`addTrait<Tr>()`: Add trait `Tr`.
|
|
688
629
|
`addTraits<Tr[]>()`: Add multiple traits `Tr[]`.
|
|
@@ -705,10 +646,10 @@ Zero-cost casting functions.
|
|
|
705
646
|
Defining brands and identities at our app entry points after validation:
|
|
706
647
|
|
|
707
648
|
```ts
|
|
708
|
-
import {
|
|
649
|
+
import { Identity, assertors } from '@vicin/phantom';
|
|
709
650
|
|
|
710
651
|
declare const __UserId: unique symbol;
|
|
711
|
-
type UserId =
|
|
652
|
+
type UserId = Identity.Declare<typeof __UserId, 'UserId', string>;
|
|
712
653
|
const asUserId = assertors.asBrand<UserId>();
|
|
713
654
|
|
|
714
655
|
function validateUserId(userId: string) {
|
|
@@ -859,6 +800,22 @@ function hash<V>(value: V) {
|
|
|
859
800
|
|
|
860
801
|
---
|
|
861
802
|
|
|
803
|
+
## Deprecated API
|
|
804
|
+
|
|
805
|
+
### Brand
|
|
806
|
+
|
|
807
|
+
To unify Api surface `Identity` should be used instead. **marked with `deprecated` and will be removed in v2.0.0**
|
|
808
|
+
|
|
809
|
+
### asBrand
|
|
810
|
+
|
|
811
|
+
To unify Api surface `asIdentity` should be used instead. **marked with `deprecated` and will be removed in v2.0.0**
|
|
812
|
+
|
|
813
|
+
### isBrand
|
|
814
|
+
|
|
815
|
+
To unify Api surface `isIdentity` should be used instead. **marked with `deprecated` and will be removed in v2.0.0**
|
|
816
|
+
|
|
817
|
+
---
|
|
818
|
+
|
|
862
819
|
## Sigil
|
|
863
820
|
|
|
864
821
|
`Sigil` is another lightweight TypeScript library I created for **nominal identity on classes**, providing compile-time branding and reliable runtime type checks. It solves the unreliability of `instanceof` in monorepos, bundled/HMR environments (where classes can duplicate), and manual branding boilerplate in DDD by using `symbols`, `lineages`, and a `centralized registry` for stable identity across codebases.
|
package/dist/index.d.mts
CHANGED
|
@@ -6,6 +6,8 @@ declare namespace assertors {
|
|
|
6
6
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
7
7
|
* where you know the value is valid.
|
|
8
8
|
*
|
|
9
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
10
|
+
*
|
|
9
11
|
* @template B - The brand declaration to assign.
|
|
10
12
|
* @returns A function that casts any value to the branded type.
|
|
11
13
|
*/
|
|
@@ -311,6 +313,21 @@ declare namespace VariantsCore {
|
|
|
311
313
|
} ? true : false;
|
|
312
314
|
}
|
|
313
315
|
|
|
316
|
+
/** Stip phantom metadata object from a type */
|
|
317
|
+
type StripPhantom<T> = T extends {
|
|
318
|
+
__Phantom: {
|
|
319
|
+
__OriginalType?: infer O;
|
|
320
|
+
};
|
|
321
|
+
} ? Exclude<O, undefined> : T extends {
|
|
322
|
+
__Phantom: {
|
|
323
|
+
__Base?: infer B;
|
|
324
|
+
};
|
|
325
|
+
} ? Exclude<B, undefined> : T extends {
|
|
326
|
+
__Phantom: object;
|
|
327
|
+
} ? unknown : T;
|
|
328
|
+
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
329
|
+
declare const stripPhantom: <T>(value: T) => StripPhantom<T>;
|
|
330
|
+
type _StripPhantom<T> = StripPhantom<T>;
|
|
314
331
|
/**
|
|
315
332
|
* Phantom meatadata object manipulators.
|
|
316
333
|
*
|
|
@@ -322,13 +339,19 @@ declare namespace PhantomCore {
|
|
|
322
339
|
__Phantom: infer Phantom extends object;
|
|
323
340
|
} ? Phantom : never;
|
|
324
341
|
/** Stip phantom metadata object from a type */
|
|
325
|
-
type StripPhantom<T> = T
|
|
342
|
+
type StripPhantom<T> = _StripPhantom<T>;
|
|
343
|
+
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
344
|
+
const stripPhantom: <T>(value: T) => T extends {
|
|
326
345
|
__Phantom: {
|
|
327
346
|
__OriginalType?: infer O;
|
|
328
347
|
};
|
|
329
|
-
} ? Exclude<O, undefined> : T
|
|
330
|
-
|
|
331
|
-
|
|
348
|
+
} ? Exclude<O, undefined> : T extends {
|
|
349
|
+
__Phantom: {
|
|
350
|
+
__Base?: infer B;
|
|
351
|
+
};
|
|
352
|
+
} ? Exclude<B, undefined> : T extends {
|
|
353
|
+
__Phantom: object;
|
|
354
|
+
} ? unknown : T;
|
|
332
355
|
}
|
|
333
356
|
|
|
334
357
|
/** Interface of 'Brand' and 'Identity' errors. */
|
|
@@ -441,6 +464,8 @@ type Merge<O1 extends object, O2 extends object> = Prettify<Omit<O1, keyof O2> &
|
|
|
441
464
|
*
|
|
442
465
|
* Brands provide nominal typing for otherwise identical values.
|
|
443
466
|
* A value may only be branded once.
|
|
467
|
+
*
|
|
468
|
+
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
444
469
|
*/
|
|
445
470
|
declare namespace BrandCore {
|
|
446
471
|
/** Type guard for any brand. */
|
|
@@ -476,7 +501,7 @@ declare namespace IdentityCore {
|
|
|
476
501
|
/** Type guard for any identity. */
|
|
477
502
|
export type Any = TagCore.Of<string | symbol>;
|
|
478
503
|
/** Declare an identity */
|
|
479
|
-
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>>;
|
|
504
|
+
export type Declare<T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = IfNever<B, unknown> & Prettify<TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
480
505
|
/**
|
|
481
506
|
* Assign an identity to a value.
|
|
482
507
|
* Enforces base-type compatibility.
|
|
@@ -541,7 +566,7 @@ declare namespace TransformationCore {
|
|
|
541
566
|
/** Type guard for any transformation. */
|
|
542
567
|
export type Any = InputCore.Of<any> & TagCore.Of<string | symbol>;
|
|
543
568
|
/** Declare a transformation */
|
|
544
|
-
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>>;
|
|
569
|
+
export type Declare<I, T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = IfNever<B, unknown> & Prettify<InputCore.Of<I> & TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
545
570
|
/**
|
|
546
571
|
* Apply a transformation to a value.
|
|
547
572
|
* Enforces base-type compatibility.
|
|
@@ -656,6 +681,8 @@ declare namespace Traits {
|
|
|
656
681
|
*
|
|
657
682
|
* Brands provide nominal typing for otherwise identical values.
|
|
658
683
|
* A value may only be branded once.
|
|
684
|
+
*
|
|
685
|
+
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
659
686
|
*/
|
|
660
687
|
declare namespace Brand {
|
|
661
688
|
/** Type guard for any brand. */
|
|
@@ -767,7 +794,11 @@ declare namespace Inspect {
|
|
|
767
794
|
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
768
795
|
/** Check if any traits exist */
|
|
769
796
|
type HasTraits<T> = TraitsCore.HasTraits<T>;
|
|
770
|
-
/**
|
|
797
|
+
/**
|
|
798
|
+
* Check whether value is branded with
|
|
799
|
+
*
|
|
800
|
+
* @deprecated To unify Api surface 'isIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
801
|
+
*/
|
|
771
802
|
type isBrand<T, B extends BrandCore.Any> = BrandCore.isBrand<T, B>;
|
|
772
803
|
/** Check whether value is branded with */
|
|
773
804
|
type isIdentity<T, I extends IdentityCore.Any> = IdentityCore.isIdentity<T, I>;
|
|
@@ -784,6 +815,8 @@ declare namespace Inspect {
|
|
|
784
815
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
785
816
|
* where you know the value is valid.
|
|
786
817
|
*
|
|
818
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
819
|
+
*
|
|
787
820
|
* @template B - The brand declaration to assign.
|
|
788
821
|
* @returns A function that casts any value to the branded type.
|
|
789
822
|
*/
|
|
@@ -1000,6 +1033,8 @@ declare namespace Phantom {
|
|
|
1000
1033
|
*
|
|
1001
1034
|
* Brands provide nominal typing for otherwise identical values.
|
|
1002
1035
|
* A value may only be branded once.
|
|
1036
|
+
*
|
|
1037
|
+
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1003
1038
|
*/
|
|
1004
1039
|
namespace Brand {
|
|
1005
1040
|
/** Type guard for any brand. */
|
|
@@ -1089,7 +1124,17 @@ declare namespace Phantom {
|
|
|
1089
1124
|
/** Stip phantom metadata object from a type */
|
|
1090
1125
|
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1091
1126
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1092
|
-
const stripPhantom: <T>(value: T) =>
|
|
1127
|
+
const stripPhantom: <T>(value: T) => T extends {
|
|
1128
|
+
__Phantom: {
|
|
1129
|
+
__OriginalType?: infer O;
|
|
1130
|
+
};
|
|
1131
|
+
} ? Exclude<O, undefined> : T extends {
|
|
1132
|
+
__Phantom: {
|
|
1133
|
+
__Base?: infer B;
|
|
1134
|
+
};
|
|
1135
|
+
} ? Exclude<B, undefined> : T extends {
|
|
1136
|
+
__Phantom: object;
|
|
1137
|
+
} ? unknown : T;
|
|
1093
1138
|
/** Extract the label */
|
|
1094
1139
|
type LabelOf<T> = LabelCore.LabelOf<T>;
|
|
1095
1140
|
/** Check whether a base constraint exists */
|
|
@@ -1116,7 +1161,11 @@ declare namespace Phantom {
|
|
|
1116
1161
|
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
1117
1162
|
/** Check if any traits exist */
|
|
1118
1163
|
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
1119
|
-
/**
|
|
1164
|
+
/**
|
|
1165
|
+
* Check whether value is branded with
|
|
1166
|
+
*
|
|
1167
|
+
* @deprecated To unify Api surface 'isIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1168
|
+
*/
|
|
1120
1169
|
type isBrand<T, B extends Brand.Any> = BrandCore.isBrand<T, B>;
|
|
1121
1170
|
/** Check whether value is branded with */
|
|
1122
1171
|
type isIdentity<T, I extends Identity.Any> = IdentityCore.isIdentity<T, I>;
|
|
@@ -1136,6 +1185,8 @@ declare namespace Phantom {
|
|
|
1136
1185
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1137
1186
|
* where you know the value is valid.
|
|
1138
1187
|
*
|
|
1188
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1189
|
+
*
|
|
1139
1190
|
* @template B - The brand declaration to assign.
|
|
1140
1191
|
* @returns A function that casts any value to the branded type.
|
|
1141
1192
|
*/
|
|
@@ -1220,6 +1271,8 @@ declare namespace Phantom {
|
|
|
1220
1271
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1221
1272
|
* where you know the value is valid.
|
|
1222
1273
|
*
|
|
1274
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1275
|
+
*
|
|
1223
1276
|
* @template B - The brand declaration to assign.
|
|
1224
1277
|
* @returns A function that casts any value to the branded type.
|
|
1225
1278
|
*/
|
|
@@ -1304,7 +1357,17 @@ declare namespace Phantom {
|
|
|
1304
1357
|
/** Stip phantom metadata object from a type */
|
|
1305
1358
|
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1306
1359
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1307
|
-
const stripPhantom: <T>(value: T) =>
|
|
1360
|
+
const stripPhantom: <T>(value: T) => T extends {
|
|
1361
|
+
__Phantom: {
|
|
1362
|
+
__OriginalType?: infer O;
|
|
1363
|
+
};
|
|
1364
|
+
} ? Exclude<O, undefined> : T extends {
|
|
1365
|
+
__Phantom: {
|
|
1366
|
+
__Base?: infer B;
|
|
1367
|
+
};
|
|
1368
|
+
} ? Exclude<B, undefined> : T extends {
|
|
1369
|
+
__Phantom: object;
|
|
1370
|
+
} ? unknown : T;
|
|
1308
1371
|
/** --------------------------------------
|
|
1309
1372
|
* Error type
|
|
1310
1373
|
* --------------------------------------- */
|
|
@@ -1341,4 +1404,4 @@ declare namespace Phantom {
|
|
|
1341
1404
|
}
|
|
1342
1405
|
}
|
|
1343
1406
|
|
|
1344
|
-
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 };
|
|
1407
|
+
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, stripPhantom };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ declare namespace assertors {
|
|
|
6
6
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
7
7
|
* where you know the value is valid.
|
|
8
8
|
*
|
|
9
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
10
|
+
*
|
|
9
11
|
* @template B - The brand declaration to assign.
|
|
10
12
|
* @returns A function that casts any value to the branded type.
|
|
11
13
|
*/
|
|
@@ -311,6 +313,21 @@ declare namespace VariantsCore {
|
|
|
311
313
|
} ? true : false;
|
|
312
314
|
}
|
|
313
315
|
|
|
316
|
+
/** Stip phantom metadata object from a type */
|
|
317
|
+
type StripPhantom<T> = T extends {
|
|
318
|
+
__Phantom: {
|
|
319
|
+
__OriginalType?: infer O;
|
|
320
|
+
};
|
|
321
|
+
} ? Exclude<O, undefined> : T extends {
|
|
322
|
+
__Phantom: {
|
|
323
|
+
__Base?: infer B;
|
|
324
|
+
};
|
|
325
|
+
} ? Exclude<B, undefined> : T extends {
|
|
326
|
+
__Phantom: object;
|
|
327
|
+
} ? unknown : T;
|
|
328
|
+
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
329
|
+
declare const stripPhantom: <T>(value: T) => StripPhantom<T>;
|
|
330
|
+
type _StripPhantom<T> = StripPhantom<T>;
|
|
314
331
|
/**
|
|
315
332
|
* Phantom meatadata object manipulators.
|
|
316
333
|
*
|
|
@@ -322,13 +339,19 @@ declare namespace PhantomCore {
|
|
|
322
339
|
__Phantom: infer Phantom extends object;
|
|
323
340
|
} ? Phantom : never;
|
|
324
341
|
/** Stip phantom metadata object from a type */
|
|
325
|
-
type StripPhantom<T> = T
|
|
342
|
+
type StripPhantom<T> = _StripPhantom<T>;
|
|
343
|
+
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
344
|
+
const stripPhantom: <T>(value: T) => T extends {
|
|
326
345
|
__Phantom: {
|
|
327
346
|
__OriginalType?: infer O;
|
|
328
347
|
};
|
|
329
|
-
} ? Exclude<O, undefined> : T
|
|
330
|
-
|
|
331
|
-
|
|
348
|
+
} ? Exclude<O, undefined> : T extends {
|
|
349
|
+
__Phantom: {
|
|
350
|
+
__Base?: infer B;
|
|
351
|
+
};
|
|
352
|
+
} ? Exclude<B, undefined> : T extends {
|
|
353
|
+
__Phantom: object;
|
|
354
|
+
} ? unknown : T;
|
|
332
355
|
}
|
|
333
356
|
|
|
334
357
|
/** Interface of 'Brand' and 'Identity' errors. */
|
|
@@ -441,6 +464,8 @@ type Merge<O1 extends object, O2 extends object> = Prettify<Omit<O1, keyof O2> &
|
|
|
441
464
|
*
|
|
442
465
|
* Brands provide nominal typing for otherwise identical values.
|
|
443
466
|
* A value may only be branded once.
|
|
467
|
+
*
|
|
468
|
+
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
444
469
|
*/
|
|
445
470
|
declare namespace BrandCore {
|
|
446
471
|
/** Type guard for any brand. */
|
|
@@ -476,7 +501,7 @@ declare namespace IdentityCore {
|
|
|
476
501
|
/** Type guard for any identity. */
|
|
477
502
|
export type Any = TagCore.Of<string | symbol>;
|
|
478
503
|
/** Declare an identity */
|
|
479
|
-
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>>;
|
|
504
|
+
export type Declare<T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = IfNever<B, unknown> & Prettify<TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
480
505
|
/**
|
|
481
506
|
* Assign an identity to a value.
|
|
482
507
|
* Enforces base-type compatibility.
|
|
@@ -541,7 +566,7 @@ declare namespace TransformationCore {
|
|
|
541
566
|
/** Type guard for any transformation. */
|
|
542
567
|
export type Any = InputCore.Of<any> & TagCore.Of<string | symbol>;
|
|
543
568
|
/** Declare a transformation */
|
|
544
|
-
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>>;
|
|
569
|
+
export type Declare<I, T extends string | symbol, L extends string = never, B extends unknown = never, V extends string = never> = IfNever<B, unknown> & Prettify<InputCore.Of<I> & TagCore.Of<T> & LabelCore.OfIfExists<L> & BaseCore.OfIfExists<B> & VariantsCore.OfIfExists<V>>;
|
|
545
570
|
/**
|
|
546
571
|
* Apply a transformation to a value.
|
|
547
572
|
* Enforces base-type compatibility.
|
|
@@ -656,6 +681,8 @@ declare namespace Traits {
|
|
|
656
681
|
*
|
|
657
682
|
* Brands provide nominal typing for otherwise identical values.
|
|
658
683
|
* A value may only be branded once.
|
|
684
|
+
*
|
|
685
|
+
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
659
686
|
*/
|
|
660
687
|
declare namespace Brand {
|
|
661
688
|
/** Type guard for any brand. */
|
|
@@ -767,7 +794,11 @@ declare namespace Inspect {
|
|
|
767
794
|
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
768
795
|
/** Check if any traits exist */
|
|
769
796
|
type HasTraits<T> = TraitsCore.HasTraits<T>;
|
|
770
|
-
/**
|
|
797
|
+
/**
|
|
798
|
+
* Check whether value is branded with
|
|
799
|
+
*
|
|
800
|
+
* @deprecated To unify Api surface 'isIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
801
|
+
*/
|
|
771
802
|
type isBrand<T, B extends BrandCore.Any> = BrandCore.isBrand<T, B>;
|
|
772
803
|
/** Check whether value is branded with */
|
|
773
804
|
type isIdentity<T, I extends IdentityCore.Any> = IdentityCore.isIdentity<T, I>;
|
|
@@ -784,6 +815,8 @@ declare namespace Inspect {
|
|
|
784
815
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
785
816
|
* where you know the value is valid.
|
|
786
817
|
*
|
|
818
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
819
|
+
*
|
|
787
820
|
* @template B - The brand declaration to assign.
|
|
788
821
|
* @returns A function that casts any value to the branded type.
|
|
789
822
|
*/
|
|
@@ -1000,6 +1033,8 @@ declare namespace Phantom {
|
|
|
1000
1033
|
*
|
|
1001
1034
|
* Brands provide nominal typing for otherwise identical values.
|
|
1002
1035
|
* A value may only be branded once.
|
|
1036
|
+
*
|
|
1037
|
+
* @deprecated To unify Api surface 'Identity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1003
1038
|
*/
|
|
1004
1039
|
namespace Brand {
|
|
1005
1040
|
/** Type guard for any brand. */
|
|
@@ -1089,7 +1124,17 @@ declare namespace Phantom {
|
|
|
1089
1124
|
/** Stip phantom metadata object from a type */
|
|
1090
1125
|
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1091
1126
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1092
|
-
const stripPhantom: <T>(value: T) =>
|
|
1127
|
+
const stripPhantom: <T>(value: T) => T extends {
|
|
1128
|
+
__Phantom: {
|
|
1129
|
+
__OriginalType?: infer O;
|
|
1130
|
+
};
|
|
1131
|
+
} ? Exclude<O, undefined> : T extends {
|
|
1132
|
+
__Phantom: {
|
|
1133
|
+
__Base?: infer B;
|
|
1134
|
+
};
|
|
1135
|
+
} ? Exclude<B, undefined> : T extends {
|
|
1136
|
+
__Phantom: object;
|
|
1137
|
+
} ? unknown : T;
|
|
1093
1138
|
/** Extract the label */
|
|
1094
1139
|
type LabelOf<T> = LabelCore.LabelOf<T>;
|
|
1095
1140
|
/** Check whether a base constraint exists */
|
|
@@ -1116,7 +1161,11 @@ declare namespace Phantom {
|
|
|
1116
1161
|
type TraitKeysOf<T> = TraitsCore.TraitKeysOf<T>;
|
|
1117
1162
|
/** Check if any traits exist */
|
|
1118
1163
|
type HasTraits<T, Tr extends string | symbol = string | symbol> = TraitsCore.HasTraits<T, Tr>;
|
|
1119
|
-
/**
|
|
1164
|
+
/**
|
|
1165
|
+
* Check whether value is branded with
|
|
1166
|
+
*
|
|
1167
|
+
* @deprecated To unify Api surface 'isIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1168
|
+
*/
|
|
1120
1169
|
type isBrand<T, B extends Brand.Any> = BrandCore.isBrand<T, B>;
|
|
1121
1170
|
/** Check whether value is branded with */
|
|
1122
1171
|
type isIdentity<T, I extends Identity.Any> = IdentityCore.isIdentity<T, I>;
|
|
@@ -1136,6 +1185,8 @@ declare namespace Phantom {
|
|
|
1136
1185
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1137
1186
|
* where you know the value is valid.
|
|
1138
1187
|
*
|
|
1188
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1189
|
+
*
|
|
1139
1190
|
* @template B - The brand declaration to assign.
|
|
1140
1191
|
* @returns A function that casts any value to the branded type.
|
|
1141
1192
|
*/
|
|
@@ -1220,6 +1271,8 @@ declare namespace Phantom {
|
|
|
1220
1271
|
* with the brand's nominal type applied. Use it for simple branded primitives
|
|
1221
1272
|
* where you know the value is valid.
|
|
1222
1273
|
*
|
|
1274
|
+
* @deprecated To unify Api surface 'asIdentity' should be used instea, will be removed in v2.0.0. for more info check 'https://www.npmjs.com/package/@vicin/phantom#deprecated-api'
|
|
1275
|
+
*
|
|
1223
1276
|
* @template B - The brand declaration to assign.
|
|
1224
1277
|
* @returns A function that casts any value to the branded type.
|
|
1225
1278
|
*/
|
|
@@ -1304,7 +1357,17 @@ declare namespace Phantom {
|
|
|
1304
1357
|
/** Stip phantom metadata object from a type */
|
|
1305
1358
|
type StripPhantom<T> = PhantomCore.StripPhantom<T>;
|
|
1306
1359
|
/** run-time helper for 'StringPhantom', used for debugging mainly */
|
|
1307
|
-
const stripPhantom: <T>(value: T) =>
|
|
1360
|
+
const stripPhantom: <T>(value: T) => T extends {
|
|
1361
|
+
__Phantom: {
|
|
1362
|
+
__OriginalType?: infer O;
|
|
1363
|
+
};
|
|
1364
|
+
} ? Exclude<O, undefined> : T extends {
|
|
1365
|
+
__Phantom: {
|
|
1366
|
+
__Base?: infer B;
|
|
1367
|
+
};
|
|
1368
|
+
} ? Exclude<B, undefined> : T extends {
|
|
1369
|
+
__Phantom: object;
|
|
1370
|
+
} ? unknown : T;
|
|
1308
1371
|
/** --------------------------------------
|
|
1309
1372
|
* Error type
|
|
1310
1373
|
* --------------------------------------- */
|
|
@@ -1341,4 +1404,4 @@ declare namespace Phantom {
|
|
|
1341
1404
|
}
|
|
1342
1405
|
}
|
|
1343
1406
|
|
|
1344
|
-
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 };
|
|
1407
|
+
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, stripPhantom };
|