utilitypes-ts
    Preparing search index...

    Type Alias MakeReadonly<T, K>

    MakeReadonly: T extends T
        ? Omit<T, K> & Readonly<Pick<T, Extract<K, keyof T>>>
        : never

    Makes specified keys K readonly in each member of a union T.

    This distributes over unions, ensuring each member is processed individually. Only keys that exist in each member are affected.

    Type Parameters

    • T

      The target type (can be a union)

    • K extends UnionKey<T> = UnionKey<T>

      Keys to make readonly (defaults to all union keys)

    type Result = MakeReadonly<{ a: string; b: number }>;
    // ^?
    // { readonly a: string; readonly b: number }
    type Result = MakeReadonly<{ a: string } | { b: number }>;
    // ^?
    // { readonly a: string } | { readonly b: number }
    type Result = MakeReadonly<{ a: string } | { b: number }, "a">;
    // ^?
    // { readonly a: string } | { b: number }