17973-medium-deepmutable

Back

type DeepMutable<T> = T extends (...args: any[]) => any ? T : {
  -readonly [k in keyof T]: DeepMutable<T[k]>
}

Solution by wendao-liu #35110

type DeepMutable<T extends object> = {
  -readonly [K in keyof T]: T[K] extends Function ? T[K] : T[K] extends object ? DeepMutable<T[K]> : T[K]
}

Solution by ouzexi #34141

type DeepMutable<T extends object> = {
  -readonly [P in keyof T]: T[P] extends Function ? T[P] : T[P] extends object ? DeepMutable<T[P]> : T[P];
};

Solution by sunupupup #33476

// your answers
type DeepMutable<T> =  {
  -readonly [P in keyof T]: T[P] extends object ? T[P] extends Function ? T[P] : DeepMutable<T[P]> : T[P]
}

Solution by pea-sys #33395

type DeepMutable<T extends object> = T extends (...args: any) => any 
  ? T 
  : { - readonly [key in keyof T]: T[key] extends object ? DeepMutable<T[key]> : T[key] }

Solution by kanishev #33009

// your answers
type DeepMutable<T extends object> = {
  -readonly [P in keyof T]: T[P] extends object ? T[P] extends Function ? T[P] : DeepMutable<T[P]> : T[P]
}

Solution by Kakeru-Miyazaki #30898

type DeepMutable<T extends object> = 
  T extends (...args: any[]) => unknown 
    ? T : T extends {[propertyKey: string | number]: any} 
      ? {
        -readonly [K in keyof T]: DeepMutable<T[K]>
      } 
    : T

Solution by sv-98-maxin #30154

type DeepMutable<T> = {
  -readonly [key in keyof T]: T[key] extends object ? T[key] extends Function ? T[key] : DeepMutable<T[key]> : T[key]
}

Solution by tclxshunquan-wang #29844

type DeepMutable<T extends Record<keyof any, any>> = T extends (
  ...args: any[]
) => unknown
  ? T
  : {
      -readonly [P in keyof T]: T[P] extends Record<keyof any, any>
        ? DeepMutable<T[P]>
        : T[P];
    };

Solution by DoubleWoodLin #28891

这题一步步来即可

如果 value 是对象则需要继续递归处理,这里注意 Record<string, any> 的子类型还有 Function,需要处理一下

因为传入的值都是 readonly 的,需要去除一下,采用 - readonly 去除

type DeepMutable<T> = T extends Record<string, any>
  ? {
  - readonly [K in keyof T]: T[K] extends Record<string, any> 
    ? T[K] extends (...args: any[]) => any
      ? T[K]
      : DeepMutable<T[K]>
    : T[K]
} : T

Solution by linjunc #28340

type DeepMutable<T> = {
  -readonly [k in keyof T]: DeepMutable<T[k]>;
} & {};

Solution by ryuji-1to #27690

type DeepMutable<T> =
  T extends (...args: infer P) => infer R
  ? (...args: P) => R
  : T extends object
  ? { -readonly [K in keyof T]
    : DeepMutable<T[K]> }
  : T

Solution by smileboyi #27271

type DeepMutable<T> = {
  -readonly [K in keyof T]: T[K] extends ((...args: any[]) => any) ? T[K] : DeepMutable<T[K]>
}

Solution by XkSuperCool #26339

// your answers
type DeepMutable<T extends object> = {
  -readonly [K in keyof T]: T[K] extends Function ? T[K] : T[K] extends object ? DeepMutable<T[K]> : T[K]
}

Solution by studymachiney #24944

// your answers
type DeepMutable<T extends object> = {
  -readonly [K in keyof T]: T[K] extends Function ? T[K] : T[K] extends object ? DeepMutable<T[K]> : T[K]
}

Solution by studymachiney #24744

type DeepMutable<T extends object> = { -readonly [P in keyof T]:
  T[P] extends Function ? T[P] :
  T[P] extends object ? DeepMutable<T[P]> :
  T[P] };

// old way
// type DeepMutable<T extends { [x: PropertyKey]: any }> =
//   {
//     -readonly [P in keyof T]:
//     (T[P] extends Function ?
//       T[P] :  //函数直接返回
//       (T[P] extends { [x: PropertyKey]: any } ?
//         DeepMutable<T[P]> :   //结构体继续遍历
//         T[P])
//     )
//   };

Solution by E-uler #24557

type DeepMutable<T extends object> = {
  -readonly [Key in keyof T]: T[Key] extends Record<string, unknown> | readonly unknown[]
    ? DeepMutable<T[Key]>
    : T[Key]
}

Solution by NeylonR #24374

// your answers
type DeepMutable<T extends object> = {
  -readonly [K in keyof T]: T[K] extends object ? T[K] extends Function ? T[K] : DeepMutable<T[K]> : T[K]
}

Solution by snakeUni #23344

// 你的答案
type DeepMutable<T> = {
  -readonly [K in keyof T]: T[K] extends object ? T[K] extends Function ? T[K] : DeepMutable<T[K]> : T[K]
}

Solution by jxhhdx #22759

type DeepMutable<T extends object> = {
  -readonly [K in keyof T]: T[K] extends Function ? T[K] : T[K] extends object ? DeepMutable<T[K]> : T[K]
}

Playground

Solution by teamchong #22537

type DeepMutable<T extends Record<PropertyKey, any>> = {
  -readonly [Key in keyof T]: T[Key] extends (...args: any[]) => any ? T[Key] : DeepMutable<T[Key]>
}

Solution by drylint #22150

// 你的答案
type DeepMutable<T extends object>
    = T extends (...argv: infer P) => infer R ? (...argv: P) => R
    : T extends unknown[] ? (
        T extends [infer A, ... infer B extends unknown[]]
        ? [A extends object ? DeepMutable<A> : A, ...DeepMutable<B>]
        : T
    )
    : {
        -readonly [K in keyof T]
        : T[K] extends object
        ? DeepMutable<T[K]>
        : T[K] extends unknown[]
        ? DeepMutable<T[K]>
        : T[K]
    }

type test = DeepMutable<{
    readonly sdf: [[36, [3, {
        readonly a: '',
        readonly c: 33
    }, 4,]], {
        readonly b: 'b',
        readonly c: {
            readonly a: ''
            readonly b: ['']
        }
    }]
}>

Solution by goddnsgit #21809

type DeepMutable<T extends object> = {
  -readonly [K in keyof T]: T[K] extends object
    ? T[K] extends Function
      ? T[K]
      : DeepMutable<T[K]>
    : T[K]
}

Solution by rin-st #21164

type DeepMutable<T> = {
  -readonly [key in keyof T]: T[key] extends any[]
    ? DeepMutable<T[key]>
    : T[key] extends Function
    ? T[key]
    : T[key] extends object
    ? DeepMutable<T[key]>
    : T[key];
};

Solution by venusliang #20894

type DeepMutable<T  extends Record<string,any>> = {
  -readonly[P in keyof T]:T[P] extends Record<string,any> ?
  T[P] extends (...anys: any[]) => any ? T[P]:
   DeepMutable<T[P]>:
  T[P]
}

Solution by so11y #20841

// your answers
type DeepMutable<T extends object> = {
  -readonly[K in keyof T] : T[K] extends object ? T[K] extends (...anys: any[]) => any ? T[K] : DeepMutable<T[K]> : T[K]
}

Solution by YqxLzx #20252

// your answers
type DeepMutable<T> = {
  -readonly [Key in keyof T]: T[Key] extends object
    ? DeepMutable<T[Key]>
    : T[Key];
};

Solution by fengjinlong #20110

type DeepMutable<T extends Record<PropertyKey, any>> = {
  -readonly [P in keyof T]: 
    T[P] extends Record<PropertyKey, any>
      ? T[P] extends Function
        ? T[P]
        : DeepMutable<T[P]> 
      : T[P]
}

Solution by zhaoyao91 #19902

type DeepMutable<T extends object> = {
  -readonly [K in keyof T]: T[K] extends object ? (T[K] extends (...anys: any[]) => any ? T[K] : DeepMutable<T[K]>) : T[K];
};

Solution by CaoXueLiang #18923

type DeepMutable<T extends Record<keyof any,any>> =
  T extends (...args:any[])=>any?
    T:
    {
      - readonly [K in keyof T]:DeepMutable<T[K]>
    }

Solution by jiangshanmeta #18259