@ganbarodigital/ts-lib-value-objects
Version:
Helps you create value objects and refined types for safer software
51 lines • 1.6 kB
TypeScript
import { Entity } from "./Entity";
/**
* Entity<ID, T> is the base class for defining your Entity hierarchies.
*
* Every Entity:
*
* - has an identity
* - that you can access by an `id` property
* - has a stored value
* - that you can get the valueOf()
*
* Your child classes should implement readonly `get` accessors for
* any fields of type `T` that you'd like to access without having
* to call `valueOf()` all the time.
*/
export declare abstract class EntityObject<ID, T> implements Entity<ID, T> {
/**
* this is the data that we wrap
*
* child classes are welcome to access it directly (to avoid the cost
* of a call to `valueOf()`), but should never modify the data at all
*/
protected readonly value: T;
/**
* this constructor does no contract / specification enforcement at all
* do that in your constructor, before calling super()
*
* if you don't need to enforce a contract, your class can safely
* create a public constructor
*/
protected constructor(input: T);
/**
* returns the ID of this entity
*/
abstract idOf(): ID;
/**
* returns the wrapped value
*
* for types passed by reference, we do NOT return a clone of any kind.
* You have to be careful not to accidentally change this value.
*/
valueOf(): T;
/**
* a type-guard. It proves that an object is a wrapper around type `T`
* that has ID `ID`.
*
* added mostly for completeness
*/
isEntity(): this is Entity<ID, T>;
}
//# sourceMappingURL=EntityObject.d.ts.map