23 lines
552 B
TypeScript
23 lines
552 B
TypeScript
export interface EntityProps<Id = string> {
|
|
readonly id: Id;
|
|
}
|
|
|
|
export abstract class Entity<Id> implements EntityProps<Id> {
|
|
protected constructor(readonly id: Id) {
|
|
// Make the id property truly immutable at runtime
|
|
Object.defineProperty(this, 'id', {
|
|
value: id,
|
|
writable: false,
|
|
enumerable: true,
|
|
configurable: false
|
|
});
|
|
}
|
|
|
|
equals(other?: Entity<Id>): boolean {
|
|
return !!other && this.id === other.id;
|
|
}
|
|
}
|
|
|
|
// Alias for backward compatibility
|
|
export type EntityAlias<Id = string> = EntityProps<Id>;
|