entity-id.ts raw
1 /**
2 * Base class for strongly-typed entity IDs.
3 * Prevents mixing up different ID types (e.g., IdentityId vs PermissionId).
4 */
5 export abstract class EntityId<T extends string = string> {
6 protected constructor(protected readonly _value: string) {
7 if (!_value || _value.trim() === '') {
8 throw new Error(`${this.constructor.name} cannot be empty`);
9 }
10 }
11
12 get value(): string {
13 return this._value;
14 }
15
16 equals(other: EntityId<T>): boolean {
17 if (!(other instanceof this.constructor)) {
18 return false;
19 }
20 return this._value === other._value;
21 }
22
23 toString(): string {
24 return this._value;
25 }
26
27 toJSON(): string {
28 return this._value;
29 }
30 }
31