import { RuntypeBase } from '../runtype'; import { Array as Arr, ReadonlyArray } from './array'; import { InternalRecord, RecordFields } from './Object'; import { Record, KeyRuntypeBase, ReadonlyRecord } from './Record'; import { Tuple, ReadonlyTuple } from './tuple'; export type Mutable = T extends InternalRecord< infer TFields, infer TPartial, true > ? InternalRecord : T extends ReadonlyArray ? Arr : T extends ReadonlyTuple ? Tuple : T extends ReadonlyRecord ? Record : unknown; export function Mutable( input: InternalRecord, ): InternalRecord; export function Mutable( input: ReadonlyArray, ): Arr; export function Mutable[] = RuntypeBase[]>( input: ReadonlyTuple, ): Tuple; export function Mutable>( record: ReadonlyRecord, ): Record; export function Mutable(input: any): any { const result = { ...input }; result.isReadonly = false; for (const m of [`asPartial`, `pick`, `omit`]) { if (typeof input[m] === 'function') { result[m] = (...args: any[]) => Mutable(input[m](...args)); } } return result; }