domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
36 lines (35 loc) • 1.47 kB
TypeScript
import { type AssessWithAssure } from 'type-fns';
import type { DomainObject } from '../instantiation/DomainObject';
import type { ConstructorOf, HasReadonly } from './HasReadonly.type';
/**
* runtime type guard that asserts that all readonly keys (metadata + explicit readonly)
* of a domain object have defined values
*
* note: this function requires that `metadata` is explicitly declared on the class.
* if metadata is not explicitly declared, this function will throw an error to
* prevent ambiguity about which keys are being checked.
*
* this is useful for narrowing types after persistence operations where readonly
* attributes are expected to be populated
*
* for example:
* ```ts
* const cluster = new DeclaredAwsRdsCluster({ name: 'testdb' });
* // cluster.arn is string | undefined
* // cluster.port is number | undefined
*
* const persisted = await persist(cluster);
* if (hasReadonly({ of: DeclaredAwsRdsCluster })(persisted)) {
* // persisted.arn is string (required)
* // persisted.port is number (required)
* }
*
* // or use .assure to throw if readonly keys are missing
* const assured = hasReadonly({ of: DeclaredAwsRdsCluster }).assure(persisted);
* // assured.arn is string (required)
* // assured.port is number (required)
* ```
*/
export declare const hasReadonly: <TDobj extends ConstructorOf<DomainObject<any>>>({ of: dobj, }: {
of: TDobj;
}) => AssessWithAssure<InstanceType<TDobj>, HasReadonly<TDobj>>;