type-fns
Version:
A set of types, type checks, and type guards for simpler, safer, and easier to read code.
56 lines (55 loc) • 2.12 kB
TypeScript
/**
* these are the keys most commonly used metadata keys
*/
declare const typicalMetadataKeys: readonly ["id", "uuid", "createdAt", "updatedAt", "deletedAt", "effectiveAt"];
/**
* these are the keys most commonly used metadata keys
*/
type TypicalMetadataKeys = typeof typicalMetadataKeys[number];
/**
* asserts that any optional metadata keys of the object will now be required
*
* by default, the following keys are considered to be metadata:
* - `id`
* - `uuid`
* - `createdAt`
* - `updatedAt`
* - `deletedAt`
* - `effectiveAt`
*
* note:
* - this type does not make any other optional, non-metadata keys required
* - i.e., any optional types on the object that are not specified as metadata keys will not be made required
* - this type does not _add_ any metadata properties to the type definition
* - i.e., if a metadata key was not already present on the object's type - it will not be added
*/
export type HasMetadata<T extends Record<string, any>, MetadataKeys = TypicalMetadataKeys> = T & Required<Pick<T, keyof T & // that are on the object
MetadataKeys>>;
/**
* asserts that any optional metadata keys of the object will now be omitted
*
* by default, the following keys are considered to be metadata:
* - `id`
* - `uuid`
* - `createdAt`
* - `updatedAt`
* - `deletedAt`
* - `effectiveAt`
*/
export type OmitMetadata<T extends Record<string, any>, MetadataKeys = TypicalMetadataKeys> = Omit<T, keyof T & // that are on the object
MetadataKeys>;
/**
* for an object of type T, asserts that the object HasMetadata<T, K> for all listed metadata keys K
*
* by default, the following keys are considered to be metadata:
* - `id`
* - `uuid`
* - `createdAt`
* - `updatedAt`
* - `deletedAt`
* - `effectiveAt`
*
* see more information about the `HasMetadata` type in its jsdoc on-hover documentation
*/
export declare const hasMetadata: <MetadataKeys extends string = "id" | "uuid" | "createdAt" | "updatedAt" | "deletedAt" | "effectiveAt", T extends Record<string, any> = Record<string, any>>(obj: T, keys?: MetadataKeys[]) => obj is HasMetadata<T, MetadataKeys>;
export {};