• [Typescript] 87. Medium DeepMutable


    Implement a generic DeepMutable which make every parameter of an object - and its sub-objects recursively - mutable.

    For example

    type X = {
      readonly a: () => 1
      readonly b: string
      readonly c: {
        readonly d: boolean
        readonly e: {
          readonly g: {
            readonly h: {
              readonly i: true
              readonly j: "s"
            }
            readonly k: "hello"
          }
        }
      }
    }
    
    type Expected = {
      a: () => 1
      b: string
      c: {
        d: boolean
        e: {
          g: {
            h: {
              i: true
              j: "s"
            }
            k: "hello"
          }
        }
      }
    }
    
    type Todo = DeepMutable<X> // should be same as `Expected`

    You can assume that we are only dealing with Objects in this challenge. Arrays, Functions, Classes and so on do not need to be taken into consideration. However, you can still challenge yourself by covering as many different cases as possible.

    /* _____________ Your Code Here _____________ */
    
    type DeepMutable<T extends Record<PropertyKey,any>> = T extends (...args: any[]) => any 
      ? T 
      : {
          - readonly [K in keyof T] : DeepMutable<T[K]>
        };
    
    type x = DeepMutable<Test2>
    /* _____________ Test Cases _____________ */
    import type { Equal, Expect } from '@type-challenges/utils'
    
    interface Test1 {
      readonly title: string
      readonly description: string
      readonly completed: boolean
      readonly meta: {
        readonly author: string
      }
    }
    type Test2 = {
      readonly a: () => 1
      readonly b: string
      readonly c: {
        readonly d: boolean
        readonly e: {
          readonly g: {
            readonly h: {
              readonly i: true
              readonly j: 's'
            }
            readonly k: 'hello'
          }
          readonly l: readonly [
            'hi',
            {
              readonly m: readonly ['hey']
            },
          ]
        }
      }
    }
    interface DeepMutableTest1 {
      title: string
      description: string
      completed: boolean
      meta: {
        author: string
      }
    }
    
    type DeepMutableTest2 = {
      a: () => 1
      b: string
      c: {
        d: boolean
        e: {
          g: {
            h: {
              i: true
              j: 's'
            }
            k: 'hello'
          }
          l: [
            'hi',
            {
              m: ['hey']
            },
          ]
        }
      }
    }
    
    type cases = [
      Expect<Equal<DeepMutable<Test1>, DeepMutableTest1>>,
      Expect<Equal<DeepMutable<Test2>, DeepMutableTest2>>,
    ]
    
    type errors = [
      // @ts-expect-error
      DeepMutable<'string'>,
      // @ts-expect-error
      DeepMutable<0>,
    ]
  • 相关阅读:
    Scrum的三个角色
    vim常用命令
    吴恩达深度学习第2课第3周编程作业 的坑(Tensorflow+Tutorial)
    tensorflow共享变量 the difference between tf.Variable() and get_variable()
    Jupyter notebook 输出含中文的pdf 方法
    find 命令查找文件,文件夹
    numpy.squeeze()是干啥的
    吴恩达深度学习第2课第2周编程作业 的坑(Optimization Methods)
    女儿一周前会爬了
    numpy.random中的shuffle和permutation以及mini-batch调整数据集(X, Y)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/16854536.html
Copyright © 2020-2023  润新知