Type alias ReadOnly<Input>

ReadOnly<Input>: Input extends Map<infer Key, infer Value>
    ? ReadonlyMap<ReadOnly<Key>, ReadOnly<Value>>
    : Input extends Set<infer Item>
        ? ReadonlySet<ReadOnly<Item>>
        : Input extends [infer First, ...(infer Rest)]
            ? Rest extends never[]
                ? readonly [ReadOnly<First>]
                : readonly [ReadOnly<First>, ...ReadOnly<Rest>]
            : Input extends (infer Item)[]
                ? ReadonlyArray<ReadOnly<Item>>
                : Input extends Function
                    ? Input
                    : Input extends object
                        ? {
                            readonly [Property in keyof Input]: ReadOnly<Input[Property]>
                        }
                        : Input

Read-only deep any Input.

Remarks

This type makes any Input read-only recursively, including nested objects and arrays, Sets, Maps, and functions.

Example

const record: ReadOnly<Record<string, Array<number>>> = {
"🟢": [1, 2, 3],
"🟩": [4, 5, 6],
};
record["🟢"][0] = 7; // Error

Type Parameters

  • Input

    The type to make read-only.