@vicin/sigil 3.3.0 → 4.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/dist/index.d.mts DELETED
@@ -1,546 +0,0 @@
1
- /** -----------------------------------------
2
- * Nominal identity symbol
3
- * ----------------------------------------- */
4
- /**
5
- * Symbol used for nominal typing
6
- */
7
- declare const sigil: unique symbol;
8
- /** -----------------------------------------
9
- * Class and instance
10
- * ----------------------------------------- */
11
- /**
12
- * Static-side interface describing methods and properties added to a class
13
- * constructor when it is sigilified.
14
- *
15
- * The properties and methods described here mirror the getters and static
16
- * predicates implemented by the `Sigilify` mixin.
17
- *
18
- * @template L - Narrow string literal type representing the label.
19
- * @template P - Optinal parent to extend its '[sigil]'.
20
- */
21
- interface ISigilStatic<L extends string = string> {
22
- /** Class-level label constant (identity). */
23
- readonly SigilLabel: L;
24
- /** Class-level label constant (human readable). */
25
- readonly SigilEffectiveLabel: L;
26
- /**
27
- * Copy of the linearized sigil type label chain for the current constructor.
28
- * Useful for debugging.
29
- */
30
- readonly SigilLabelLineage: readonly string[];
31
- /**
32
- * Copy of the sigil type label set for the current constructor.
33
- * Useful for debugging.
34
- */
35
- readonly SigilLabelSet: Readonly<Set<string>>;
36
- /**
37
- * Check whether `other` is (or inherits from) the instance represented by the
38
- * calling constructor.
39
- *
40
- * This replaces `instanceof` so that checks remain valid across bundles/realms
41
- * and when subclassing.
42
- *
43
- * @typeParam T - The specific sigil constructor (`this`).
44
- * @param this - The constructor performing the type check.
45
- * @param other - The object to test.
46
- * @returns A type guard asserting `other` is an instance of the constructor.
47
- */
48
- isOfType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
49
- /**
50
- * Check whether `other` is exactly the same instance represented by the
51
- * calling constructor.
52
- *
53
- * @typeParam T - The specific sigil constructor (`this`).
54
- * @param this - The constructor performing the type check.
55
- * @param other - The object to test.
56
- * @returns A type guard asserting `other` is an instance of the constructor.
57
- */
58
- isExactType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
59
- }
60
- /**
61
- * Instance-side interface describing properties present on sigil instances.
62
- * The methods mirror the instance helpers injected by the mixin.
63
- *
64
- * @template L - Narrow string literal type for the label returned by `getSigilLabel`.
65
- * @template P - Optinal parent to extend its '[sigil]'.
66
- */
67
- interface ISigilInstance<L extends string = string, P extends Function = never> {
68
- /** Compile-time nominal brand that encodes the class label `L` plus parent's sigil labels `SigilOf<P>`. */
69
- readonly [sigil]: Prettify<{
70
- Sigil: true;
71
- } & IfNever<SigilOf<P>, {}> & {
72
- [k in L]: true;
73
- }>;
74
- /** Returns identity sigil label of the class constructor. */
75
- getSigilLabel(): string;
76
- /** Returns human-readable sigil label of the class constructor. */
77
- getSigilEffectiveLabel(): string;
78
- /** Returns copy of sigil type label lineage of the class constructor. */
79
- getSigilLabelLineage(): readonly string[];
80
- /** Returns copy of sigil type label set of the class constructor. */
81
- getSigilLabelSet(): Readonly<Set<string>>;
82
- /**
83
- * Check whether `other` is (or inherits from) the instance represented by the
84
- * calling constructor.
85
- *
86
- * This replaces `instanceof` so that checks remain valid across bundles/realms
87
- * and when subclassing.
88
- *
89
- * @typeParam T - The specific sigil constructor (`this`).
90
- * @param this - The constructor performing the type check.
91
- * @param other - The object to test.
92
- * @returns A type guard asserting `other` is an instance of the constructor.
93
- */
94
- isOfType<T extends ISigilInstance>(this: T, other: unknown): other is T;
95
- /**
96
- * Check whether `other` is exactly the same instance represented by the
97
- * calling constructor.
98
- *
99
- * @typeParam T - The specific sigil constructor (`this`).
100
- * @param this - The constructor performing the type check.
101
- * @param other - The object to test.
102
- * @returns A type guard asserting `other` is an instance of the constructor.
103
- */
104
- isExactType<T extends ISigilInstance>(this: T, other: unknown): other is T;
105
- }
106
- /**
107
- * Combined constructor + static interface for a sigil class.
108
- *
109
- * @template L - Narrow string literal type for the label.
110
- * @template P - Optinal parent to extend its '[sigil]'.
111
- */
112
- type ISigil<L extends string = string, P extends Function = never> = ConstructorAbstract<ISigilInstance<L, P>> & ISigilStatic<L>;
113
- /** Update '[sigil]' field for nominal typing */
114
- type ExtendSigil<L extends string, P extends ISigilInstance> = Prettify<IfNever<SigilOf<P>, {}> & {
115
- [K in L]: true;
116
- }>;
117
- /**
118
- * Extract the compile-time label map from a sigil instance `S`.
119
- *
120
- * @typeParam S - A sigil instance type.
121
- * @returns The sigil label record (e.g. `{ User: true, Admin: true }`) or never if not Sigil class instance.
122
- */
123
- type SigilOf<S> = S extends {
124
- readonly [sigil]: infer Sigil;
125
- } ? Sigil : never;
126
- /** -----------------------------------------
127
- * Generic types
128
- * ----------------------------------------- */
129
- /**
130
- * Generic type for class constructors used by the Sigil utilities.
131
- *
132
- * - `T` is the instance type produced by the constructor.
133
- * - `P` is the tuple of parameter types accepted by the constructor.
134
- *
135
- * @template T - Instance type produced by the constructor (defaults to `object`).
136
- * @template P - Parameter tuple type for the constructor.
137
- */
138
- type Constructor<T = object, P extends any[] = any[]> = new (...args: P) => T;
139
- /**
140
- * Generic type for class constructors used by the Sigil utilities. for 'abstract classes'.
141
- *
142
- * - `T` is the instance type produced by the constructor.
143
- * - `P` is the tuple of parameter types accepted by the constructor.
144
- *
145
- * @template T - Instance type produced by the constructor (defaults to `object`).
146
- * @template P - Parameter tuple type for the constructor.
147
- */
148
- type ConstructorAbstract<T = object, P extends any[] = any[]> = abstract new (...args: P) => T;
149
- /** Helper type to prettify value */
150
- type Prettify<T> = {
151
- [K in keyof T]: T[K];
152
- } & {};
153
- /** Helper type to replace 'never' with another type */
154
- type IfNever<T, R = {}> = [T] extends [never] ? R : T;
155
- /**
156
- * Helper type to get prototype of class
157
- *
158
- * @template T - Class constructor.
159
- */
160
- type GetPrototype<T> = T extends {
161
- prototype: infer P;
162
- } ? P : never;
163
-
164
- /**
165
- * A minimal root Sigil class used by the library as a base identity.
166
- *
167
- * This is produced by `Sigilify` and can serve as a basic sentinel/base
168
- * class for other sigil classes or for debugging/inspection.
169
- */
170
- declare const Sigil: {
171
- new (...args: any[]): {
172
- isOfType<T extends ISigilInstance>(this: T, other: unknown): other is T;
173
- isExactType<T extends ISigilInstance>(this: T, other: unknown): other is T;
174
- getSigilLabel(): string;
175
- getSigilEffectiveLabel(): string;
176
- getSigilLabelLineage(): readonly string[];
177
- getSigilLabelSet(): Readonly<Set<string>>;
178
- readonly [sigil]: {
179
- Sigil: true;
180
- };
181
- };
182
- get SigilLabel(): "Sigil";
183
- get SigilEffectiveLabel(): "Sigil";
184
- get SigilLabelLineage(): readonly string[];
185
- get SigilLabelSet(): Readonly<Set<string>>;
186
- isOfType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
187
- isExactType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
188
- } & {
189
- new (): {};
190
- };
191
- type Sigil = InstanceType<typeof Sigil>;
192
- /**
193
- * A sigil variant of the built-in `Error` constructor used by the library
194
- * to represent Sigil-specific errors.
195
- *
196
- * Use `SigilError` when you want an Error type that is identifiable via sigil
197
- * runtime checks (e.g. `SigilError.isOfType(someError)`).
198
- */
199
- declare const SigilError: {
200
- new (...args: any[]): {
201
- isOfType<T extends ISigilInstance>(this: T, other: unknown): other is T;
202
- isExactType<T extends ISigilInstance>(this: T, other: unknown): other is T;
203
- getSigilLabel(): string;
204
- getSigilEffectiveLabel(): string;
205
- getSigilLabelLineage(): readonly string[];
206
- getSigilLabelSet(): Readonly<Set<string>>;
207
- readonly [sigil]: {
208
- Sigil: true;
209
- SigilError: true;
210
- };
211
- };
212
- get SigilLabel(): "SigilError";
213
- get SigilEffectiveLabel(): "SigilError";
214
- get SigilLabelLineage(): readonly string[];
215
- get SigilLabelSet(): Readonly<Set<string>>;
216
- isOfType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
217
- isExactType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
218
- } & ErrorConstructor;
219
- type SigilError = InstanceType<typeof SigilError>;
220
-
221
- /** -----------------------------------------
222
- * Types
223
- * ----------------------------------------- */
224
- /**
225
- * Configuration options for the Sigil library.
226
- *
227
- * These options control runtime validation, inheritance checks, label autofill behavior.
228
- */
229
- interface SigilOptions {
230
- /**
231
- * Validation rule applied to sigil labels before registration.
232
- *
233
- * - A function receives the label and must return `true` if valid.
234
- * - A `RegExp` must match the label.
235
- * - `null` disables validation entirely.
236
- *
237
- * Defaults to `null`.
238
- */
239
- labelValidation?: ((label: string) => boolean) | RegExp | null;
240
- /**
241
- * When enabled, non-decorated subclasses that would otherwise inherit an ancestor's label
242
- * will be assigned an autogenerated random label (so that explicit labels stay unique).
243
- */
244
- autofillLabels?: boolean;
245
- /**
246
- * Option for Hot module reload set-ups, reload of files can result in class redefinition which will throw
247
- * duplicate label error, setting this to 'true' will disabel this error.
248
- * However as it disables unique label check bugs can appear if the same label is passed to two different
249
- * classes so set this to 'true' only when needed and ensure uniqueness of passed labels.
250
- */
251
- skipLabelUniquenessCheck?: boolean;
252
- }
253
- /** -----------------------------------------
254
- * Update options
255
- * ----------------------------------------- */
256
- /**
257
- * Update runtime options for the Sigil library.
258
- * Call this early during application startup if you want non-default behavior.
259
- *
260
- * @param opts - Partial options to merge into the global `OPTIONS` object.
261
- */
262
- declare const updateSigilOptions: (opts: SigilOptions) => void;
263
- /** -----------------------------------------
264
- * Label validation
265
- * ----------------------------------------- */
266
- /**
267
- * Label validation regex. Labels must follow the pattern
268
- * `@scope/package.ClassName` where `ClassName` begins with an uppercase
269
- * letter. This avoids collisions across packages and helps debugging.
270
- *
271
- * It's advised to use this regex in 'SigilOptions.labelValidation'.
272
- */
273
- declare const RECOMMENDED_LABEL_REGEX: RegExp;
274
- /** @deprecated - Use 'RECOMMENDED_LABEL_REGEX' instead, will be removed in v4 */
275
- declare const DEFAULT_LABEL_REGEX: RegExp;
276
-
277
- /**
278
- * Class decorator factory that attaches sigil statics to a class constructor.
279
- *
280
- * @param label - Sigil label string to assign to the decorated class (e.g. `@scope/pkg.ClassName`).
281
- * @param opts - Options object to override any global options if needed.
282
- * @returns A class decorator compatible with the ECMAScript decorator context.
283
- */
284
- declare function WithSigil(label: string, opts?: SigilOptions): (value: Function, context: any) => void;
285
-
286
- /**
287
- * HOF (class inhancer) that attaches runtime sigil metadata to Sigil class.
288
- * Alternative to '@WithSigil' if you prefer HOFs.
289
- *
290
- * @typeParam S - Constructor type (should be an instance of sigil class).
291
- * @param Class - The constructor (class) to enhance.
292
- * @param label - Sigil label string to assign to the decorated class (e.g. `@scope/pkg.ClassName`).
293
- * @param opts - Options object to override any global options if needed.
294
- * @returns The same constructor value, with runtime metadata ensured.
295
- */
296
- declare function withSigil<S extends Function>(Class: S, label: string, opts?: SigilOptions): S;
297
-
298
- /** -----------------------------------------
299
- * Inspection helpers
300
- * ----------------------------------------- */
301
- /**
302
- * Runtime predicate that checks whether the provided value is a sigil constructor.
303
- *
304
- * @param ctor - Constructor to test.
305
- * @returns `true` if `value` is a sigil constructor, otherwise `false`.
306
- */
307
- declare function isSigilCtor(ctor: unknown): ctor is ISigil;
308
- /**
309
- * Runtime predicate that checks whether the provided object is an instance
310
- * of a sigil class.
311
- *
312
- * @param inst - The instanca to test.
313
- * @returns `true` if `obj` is an instance produced by a sigil constructor.
314
- */
315
- declare function isSigilInstance(inst: unknown): inst is ISigilInstance;
316
- /**
317
- * Helper function to get labels registered by 'Sigil'
318
- * @returns Sigil labels registered
319
- */
320
- declare function getSigilLabels(): string[];
321
-
322
- /**
323
- * Mixin factory that augments an existing class with Sigil runtime metadata and helpers.
324
- *
325
- * @param Base - The base constructor to extend.
326
- * @param label - Identity label to attach to the resulting class (e.g. '@scope/pkg.ClassName').
327
- * @param opts - Options object to override any global options if needed.
328
- * @returns A new constructor that extends `Base` and includes Sigil statics/instance methods.
329
- * @throws Error if `Base` is already sigilified.
330
- */
331
- declare function Sigilify<B extends Constructor, L extends string>(Base: B, label: L, opts?: SigilOptions): {
332
- new (...args: any[]): {
333
- /**
334
- * Check whether `other` is (or inherits from) the instance represented by the
335
- * calling constructor.
336
- *
337
- * This replaces `instanceof` so that checks remain valid across bundles/realms
338
- * and when subclassing.
339
- *
340
- * @typeParam T - The specific sigil constructor (`this`).
341
- * @param this - The constructor performing the type check.
342
- * @param other - The object to test.
343
- * @returns A type guard asserting `other` is an instance of the constructor.
344
- */
345
- isOfType<T extends ISigilInstance>(this: T, other: unknown): other is T;
346
- /**
347
- * Check whether `other` is exactly the same instance represented by the
348
- * calling constructor.
349
- *
350
- * @typeParam T - The specific sigil constructor (`this`).
351
- * @param this - The constructor performing the type check.
352
- * @param other - The object to test.
353
- * @returns A type guard asserting `other` is an instance of the constructor.
354
- */
355
- isExactType<T extends ISigilInstance>(this: T, other: unknown): other is T;
356
- /**
357
- * Returns the identity sigil label of this instance's constructor.
358
- *
359
- * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil-auto:ClassName:1:pnf11bgl').
360
- */
361
- getSigilLabel(): string;
362
- /**
363
- * Returns the human-readable sigil label of this instance's constructor.
364
- *
365
- * @returns The last passed label string (e.g. '@scope/pkg.ClassName').
366
- */
367
- getSigilEffectiveLabel(): string;
368
- /**
369
- * Returns a copy of the sigil type label lineage for this instance's constructor.
370
- *
371
- * @returns readonly array of labels representing the type lineage.
372
- */
373
- getSigilLabelLineage(): readonly string[];
374
- /**
375
- * Returns a copy of the sigil type label lineage set for this instance's constructor.
376
- *
377
- * @returns readonly array of labels representing the type lineage.
378
- */
379
- getSigilLabelSet(): Readonly<Set<string>>;
380
- /**
381
- * Compile-time nominal brand that encodes the class sigil labels object.
382
- */
383
- readonly [sigil]: {
384
- Sigil: true;
385
- } & { [K_1 in L]: true; } extends infer T ? { [K in keyof T]: T[K]; } : never;
386
- };
387
- /**
388
- * Class-level identity label constant for this sigil constructor.
389
- */
390
- get SigilLabel(): L;
391
- /**
392
- * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
393
- */
394
- get SigilEffectiveLabel(): L;
395
- /**
396
- * Linearized sigil type label chain for the current constructor.
397
- *
398
- * Useful for debugging and performing strict lineage comparisons.
399
- *
400
- * @returns An array of labels representing parent → child type labels.
401
- */
402
- get SigilLabelLineage(): readonly string[];
403
- /**
404
- * Sigil type label set for the current constructor.
405
- * Useful for debugging.
406
- *
407
- * @returns A Readonly Set of labels that represent the type lineage.
408
- */
409
- get SigilLabelSet(): Readonly<Set<string>>;
410
- /**
411
- * Check whether `other` is (or inherits from) the instance represented by the
412
- * calling constructor.
413
- *
414
- * This replaces `instanceof` so that checks remain valid across bundles/realms
415
- * and when subclassing.
416
- *
417
- * @typeParam T - The specific sigil constructor (`this`).
418
- * @param this - The constructor performing the type check.
419
- * @param other - The object to test.
420
- * @returns A type guard asserting `other` is an instance of the constructor.
421
- */
422
- isOfType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
423
- /**
424
- * Check whether `other` is exactly the same instance represented by the
425
- * calling constructor.
426
- *
427
- * @typeParam T - The specific sigil constructor (`this`).
428
- * @param this - The constructor performing the type check.
429
- * @param other - The object to test.
430
- * @returns A type guard asserting `other` is an instance of the constructor.
431
- */
432
- isExactType<T extends ISigilStatic>(this: T, other: unknown): other is GetPrototype<T>;
433
- } & B;
434
- /**
435
- * Mixin factory that augments an existing class with Sigil runtime metadata and helpers. Accept and return 'abstract' class.
436
- *
437
- * @param Base - The base constructor to extend.
438
- * @param label - Identity label to attach to the resulting class (e.g. '@scope/pkg.ClassName').
439
- * @param opts - Options object to override any global options if needed.
440
- * @returns A new abstract constructor that extends `Base` and includes Sigil statics/instance methods.
441
- * @throws Error if `Base` is already sigilified.
442
- */
443
- declare function SigilifyAbstract<B extends ConstructorAbstract, L extends string>(Base: B, label: L, opts?: SigilOptions): ((abstract new (...args: any[]) => {
444
- /**
445
- * Check whether `other` is (or inherits from) the instance represented by the
446
- * calling constructor.
447
- *
448
- * This replaces `instanceof` so that checks remain valid across bundles/realms
449
- * and when subclassing.
450
- *
451
- * @typeParam T - The specific sigil constructor (`this`).
452
- * @param this - The constructor performing the type check.
453
- * @param other - The object to test.
454
- * @returns A type guard asserting `other` is an instance of the constructor.
455
- */
456
- isOfType<T>(this: T, other: unknown): other is T;
457
- /**
458
- * Check whether `other` is exactly the same instance represented by the
459
- * calling constructor.
460
- *
461
- * @typeParam T - The specific sigil constructor (`this`).
462
- * @param this - The constructor performing the type check.
463
- * @param other - The object to test.
464
- * @returns A type guard asserting `other` is an instance of the constructor.
465
- */
466
- isExactType<T>(this: T, other: unknown): other is T;
467
- /**
468
- * Returns the identity sigil label of this instance's constructor.
469
- *
470
- * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil-auto:ClassName:1:pnf11bgl').
471
- */
472
- getSigilLabel(): string;
473
- /**
474
- * Returns the human-readable sigil label of this instance's constructor.
475
- *
476
- * @returns The last passed label string (e.g. '@scope/pkg.ClassName').
477
- */
478
- getSigilEffectiveLabel(): string;
479
- /**
480
- * Returns a copy of the sigil type label lineage for this instance's constructor.
481
- *
482
- * @returns readonly array of labels representing the type lineage.
483
- */
484
- getSigilLabelLineage(): readonly string[];
485
- /**
486
- * Returns a copy of the sigil type label lineage set for this instance's constructor.
487
- *
488
- * @returns readonly array of labels representing the type lineage.
489
- */
490
- getSigilLabelSet(): Readonly<Set<string>>;
491
- /**
492
- * Compile-time nominal brand that encodes the class sigil labels object.
493
- */
494
- readonly [sigil]: {
495
- Sigil: true;
496
- } & { [K_1 in L]: true; } extends infer T ? { [K in keyof T]: T[K]; } : never;
497
- }) & {
498
- /**
499
- * Class-level identity label constant for this sigil constructor.
500
- */
501
- get SigilLabel(): L;
502
- /**
503
- * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
504
- */
505
- get SigilEffectiveLabel(): L;
506
- /**
507
- * Linearized sigil type label chain for the current constructor.
508
- *
509
- * Useful for debugging and performing strict lineage comparisons.
510
- *
511
- * @returns An array of labels representing parent → child type labels.
512
- */
513
- get SigilLabelLineage(): readonly string[];
514
- /**
515
- * Sigil type label set for the current constructor.
516
- * Useful for debugging.
517
- *
518
- * @returns A Readonly Set of labels that represent the type lineage.
519
- */
520
- get SigilLabelSet(): Readonly<Set<string>>;
521
- /**
522
- * Check whether `other` is (or inherits from) the instance represented by the
523
- * calling constructor.
524
- *
525
- * This replaces `instanceof` so that checks remain valid across bundles/realms
526
- * and when subclassing.
527
- *
528
- * @typeParam T - The specific sigil constructor (`this`).
529
- * @param this - The constructor performing the type check.
530
- * @param other - The object to test.
531
- * @returns A type guard asserting `other` is an instance of the constructor.
532
- */
533
- isOfType<T>(this: T, other: unknown): other is GetPrototype<T>;
534
- /**
535
- * Check whether `other` is exactly the same instance represented by the
536
- * calling constructor.
537
- *
538
- * @typeParam T - The specific sigil constructor (`this`).
539
- * @param this - The constructor performing the type check.
540
- * @param other - The object to test.
541
- * @returns A type guard asserting `other` is an instance of the constructor.
542
- */
543
- isExactType<T>(this: T, other: unknown): other is GetPrototype<T>;
544
- }) & B;
545
-
546
- export { DEFAULT_LABEL_REGEX, type ExtendSigil, type GetPrototype, type ISigil, type ISigilInstance, type ISigilStatic, RECOMMENDED_LABEL_REGEX, Sigil, SigilError, type SigilOf, type SigilOptions, Sigilify, SigilifyAbstract, WithSigil, getSigilLabels, isSigilCtor, isSigilInstance, sigil, updateSigilOptions, withSigil };