domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
21 lines (20 loc) • 1.04 kB
TypeScript
import type { DomainObjectShape, Refable } from './Refable';
import type { RefByPrimary } from './RefByPrimary.type';
/**
* creates a reference to a domain object by its primary key
*
* extracts only the primary key properties from a domain object instance
*
* note
* - you may need to explicitly annotate the type for proper inference
* - e.g., `const ref = refByPrimary<typeof SeaTurtle>(turtle)`
* - automatic resolution of the relationship between instance and class.static properties is not yet possible in TypeScript
*
* @example
* ```ts
* const turtle = new SeaTurtle({ uuid: '1', seawaterSecurityNumber: '821', name: 'Crush' });
* const ref = refByPrimary<typeof SeaTurtle>(turtle);
* // ref = { uuid: '1' }
* ```
*/
export declare const refByPrimary: <TDobj extends Refable<TShape, TPrimary, TUnique>, TShape extends DomainObjectShape = any, TPrimary extends readonly string[] = any, TUnique extends readonly string[] = any>(instance: InstanceType<TDobj>) => RefByPrimary<TDobj, TShape, TPrimary, TUnique>;