UNPKG

rich-domain

Version:

This package provide utils file and interfaces to assistant build a complex application with domain driving design

51 lines 2.92 kB
/** * A utility type to recursively make all properties of a type `T` deeply readonly. * - Handles built-in types, objects, arrays, tuples, sets, and maps. * - Special cases include skipping class constructors and functions. * * @template T The type to be made deeply readonly. */ export type ReadonlyDeep<T> = T extends BuiltIns ? T : T extends new (...arguments_: any[]) => unknown ? T : T extends (...arguments_: any[]) => unknown ? {} extends ReadonlyObjectDeep<T> ? T : HasMultipleCallSignatures<T> extends true ? T : ((...arguments_: Parameters<T>) => ReturnType<T>) & ReadonlyObjectDeep<T> : T extends Readonly<ReadonlyMap<infer KeyType, infer ValueType>> ? ReadonlyMapDeep<KeyType, ValueType> : T extends Readonly<ReadonlySet<infer ItemType>> ? ReadonlySetDeep<ItemType> : T extends readonly [] | readonly [...never[]] ? readonly [] : T extends readonly [infer U, ...infer V] ? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>] : T extends readonly [...infer U, infer V] ? readonly [...ReadonlyDeep<U>, ReadonlyDeep<V>] : T extends ReadonlyArray<infer ItemType> ? ReadonlyArray<ReadonlyDeep<ItemType>> : T extends object ? ReadonlyObjectDeep<T> : unknown; /** * Makes all key-value pairs of a `ReadonlyMap` deeply readonly. * @template KeyType Type of the keys in the map. * @template ValueType Type of the values in the map. */ type ReadonlyMapDeep<KeyType, ValueType> = {} & Readonly<ReadonlyMap<ReadonlyDeep<KeyType>, ReadonlyDeep<ValueType>>>; /** * Makes all elements of a `ReadonlySet` deeply readonly. * @template ItemType Type of the items in the set. */ type ReadonlySetDeep<ItemType> = {} & Readonly<ReadonlySet<ReadonlyDeep<ItemType>>>; /** * Makes all properties of an object type deeply readonly. * @template ObjectType The type of the object to be made deeply readonly. */ type ReadonlyObjectDeep<ObjectType extends object> = { readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>; }; /** * Primitive types that are not recursively transformed. */ export type Primitive = null | undefined | string | number | boolean | symbol | bigint; /** * Built-in types that are excluded from recursive transformations. */ export type BuiltIns = Primitive | void | Date | RegExp; /** * Utility type to determine if a function type has multiple call signatures. * - Used to differentiate simple functions from overloaded functions. * * @template T The function type to analyze. */ type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> = T extends { (...arguments_: infer A): unknown; (...arguments_: infer B): unknown; } ? B extends A ? A extends B ? false : true : true : false; export {}; /** * @description Utility for deeply readonly transformations. * Adapted from TypeScript type utility libraries. * @link https://github.com/sindresorhus/type-fest/blob/main/source/readonly-deep.d.ts */ //# sourceMappingURL=types-util.d.ts.map