• [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>,
    ]
  • 相关阅读:
    图像中的傅立叶变换(二)
    图像中的傅立叶变换(一)
    最大似然估计
    论文笔记:Batch Normalization
    TensorFlow学习笔记:共享变量
    postman管理收藏夹,批量执行接口
    postman设置token关联参数,其他接口直接读取token变量
    appium+python+unittest+HTMLRunner编写UI自动化测试集
    appium自动化环境搭建(python语言开发)
    Fiddler抓取https数据包
  • 原文地址:https://www.cnblogs.com/Answer1215/p/16854536.html
Copyright © 2020-2023  润新知