对象属性只读(递归)DeepReadonly

·125 字 · 约 1 分钟#前端#TypeScript#type

实现一个泛型 DeepReadonly<T>,它将对象的每个参数及其子对象递归地设为只读。

您可以假设在此挑战中我们仅处理对象。不考虑数组、函数、类等。但是,您仍然可以通过覆盖尽可能多的不同案例来挑战自己。

例如

type X = { 
    x: { 
        a: 1
        b: 'hi'
    }
    y: 'hey'
}

type Expected = { 
    readonly x: { 
        readonly a: 1
        readonly b: 'hi'
    }
    readonly y: 'hey' 
}

type Todo = DeepReadonly<X> // should be same as `Expected`

解析#

  1. 属于基础类型则直接返回
  2. 数组、Map、Set 需要拆分其内容属性
type Primitive = undefined | string | null | boolean | number | Function;


type DeepReadonly<T> = T extends Primitive
    ? T
    : T extends Array<infer U>
    ? ReadonlyArray<DeepReadonly<T>>
    : T extends Map<infer K, infer V>
    ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
    : T extends Set<infer U>
    ? ReadonlySet<DeepReadonly<U>>
    : { readonly [K in keyof T]: DeepReadonly<T[K]> };

本文由 note.batype.com 导入。

相关文章

  • 获取只读属性 GetReadonlyKeys

    实现泛型 GetReadonlyKeys<T , GetReadonlyKeys<T 返回由对象 T 所有只读属性的键组成的联合类型。

  • 实现 Omit

    不使用 Omit 实现 TypeScript 的 Omit<T, K 泛型。

  • 实现 Pick

    不使用 Pick<T, K ,实现 TS 内置的 Pick<T, K 的功能