import type {KeysOfUnion} from './keys-of-union.d.ts'; import type {CallSignatureOf, SelectedKeys, SimplifyObject, UnwrapPathBrand} from './internal/index.d.ts'; import type {IsNever} from './is-never.d.ts'; import type {IsAny} from './is-any.d.ts'; import type {UnknownArray} from './unknown-array.d.ts'; /** Create a type that makes the given keys readonly, while keeping the remaining keys as is. Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are readonly. @example ``` import type {SetReadonly} from 'type-fest'; type Foo = { a: number; readonly b: string; c: boolean; }; type SomeReadonly = SetReadonly; //=> {a: number; readonly b: string; readonly c: boolean} ``` @category Object */ export type SetReadonly> = // The call signature is rebuilt per member, so a union of callables does not mix their signatures. BaseType extends unknown ? CallSignatureOf & _SetReadonly> : never; export type _SetReadonly = BaseType extends UnknownArray // To distribute `BaseType` when it's a union type. // `readonly` applies to a whole tuple, not to individual elements, so the tuple becomes readonly when any key is given. // `any` takes both branches of the array check, so its array branch is dropped to leave just the object branch. ? IsAny extends true ? never : IsNever extends true ? BaseType : Readonly : SetObjectReadonly>; type SetObjectReadonly = SimplifyObject< {[Key in keyof BaseType as Key extends Keys ? never : Key]: BaseType[Key]} & {readonly [Key in keyof BaseType as Key extends Keys ? Key : never]: BaseType[Key]} >; export {};