import { IfNoDeepValue } from './helpers.js'; import { Or } from './logical.js'; import { IfEquals, IfNever } from './type-check.js'; /** * Marks given keys as readonly */ export type ReadonlySome = Readonly> & Omit; /** * Make all properties in T readonly deeply */ export type DeepReadonly = { readonly [K in keyof T as IfNever< Exclude, never, K >]: IfNoDeepValue> extends true // Do not deep process No-Deep values ? T[K] : // Deep process objects DeepReadonly>; }; /** * Make all properties in T readonly deeply */ export type DeeperReadonly = { readonly [K in keyof T as IfNever< Exclude, never, K >]: NonNullable extends (infer U)[] // Deep process arrays ? DeeperReadonly[] : // Do not deep process No-Deep values IfNoDeepValue> extends true ? T[K] : // Deep process objects DeeperReadonly>; }; /** * Returns readonly keys of an object */ export type ReadonlyKeys = keyof PickReadonly; /** * Pick all readonly properties in T */ export type PickReadonly = { [K in keyof T as Or< // Omit never keys IfNever>, // Omit required IfEquals<{ [Q in K]: T[K] }, { readonly [Q in K]: T[K] }, false, true> > extends true ? never : K]: T[K]; }; /** * Omit all readonly properties in T */ export type OmitReadonly = { [K in keyof T as Or< // Omit never keys IfNever>, // Omit required IfEquals<{ [Q in K]: T[K] }, { readonly [Q in K]: T[K] }> > extends true ? never : K]: T[K]; }; /** * Pick all readonly properties in T deeply */ export type DeepPickReadonly = { [K in keyof T as Or< // Omit never keys IfNever>, // Omit required IfEquals<{ [Q in K]: T[K] }, { readonly [Q in K]: T[K] }, false, true> > extends true ? never : K]: IfNoDeepValue> extends true // Do not deep process No-Deep values ? T[K] : // Deep process objects DeepPickReadonly>; }; /** * Pick all readonly properties in T deeply */ export type DeepOmitReadonly = { [K in keyof T as Or< // Omit never keys IfNever>, // Omit required IfEquals<{ [Q in K]: T[K] }, { readonly [Q in K]: T[K] }> > extends true ? never : K]: IfNoDeepValue> extends true // Do not deep process No-Deep values ? T[K] : // Deep process objects DeepOmitReadonly>; }; /** * Pick all readonly properties in T deeply including arrays */ export type DeeperPickReadonly = { [K in keyof T as Or< // Omit never keys IfNever>, // Omit required IfEquals<{ [Q in K]: T[K] }, { readonly [Q in K]: T[K] }, false, true> > extends true ? never : K]: NonNullable extends (infer U)[] // Deep process arrays ? DeeperPickReadonly[] : // Do not deep process No-Deep values IfNoDeepValue> extends true ? T[K] : // Deep process objects DeeperPickReadonly>; }; /** * Pick all readonly properties in T deeply including arrays */ export type DeeperOmitReadonly = { [K in keyof T as Or< // Omit never keys IfNever>, // Omit required IfEquals<{ [Q in K]: T[K] }, { readonly [Q in K]: T[K] }> > extends true ? never : K]: NonNullable extends (infer U)[] // Deep process arrays ? DeeperOmitReadonly[] : // Do not deep process No-Deep values IfNoDeepValue> extends true ? T[K] : // Deep process objects DeeperOmitReadonly>; };