首页 > 其他分享 >Typescript类型体操 - Deep Readonly

Typescript类型体操 - Deep Readonly

时间:2022-09-03 00:00:45浏览次数:99  
标签:Typescript DeepReadonly readonly Deep hey Readonly hi Expected 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`

English

Implement a generic DeepReadonly<T> which make every parameter of an object - and its sub-objects recursively - readonly.

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.

For example:

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`

答案

type DeepReadonly<T extends {}> = { readonly [P in keyof T]: T[P] extends { [K: string]: {} } | any[]  ? DeepReadonly<T[P]> : T[P] };

在线演示

标签:Typescript,DeepReadonly,readonly,Deep,hey,Readonly,hi,Expected,type
From: https://www.cnblogs.com/laggage/p/type-challenge-deep-readonly.html

相关文章

  • Typescript类型体操 - First of Array
    题目中文实现一个通用First<T>,它接受一个数组T并返回它的第一个元素的类型。例如:typearr1=['a','b','c']typearr2=[3,2,1]typehead1=First<arr1>//e......
  • Typescript类型体操 - Length of Tuple
    题目中文创建一个通用的Length,接受一个readonly的数组,返回这个数组的长度。例如:typetesla=['tesla','model3','modelX','modelY']typespaceX=['FALCON9'......
  • Typescript类型体操 - Tuple To Object
    题目中文传入一个元组类型,将这个元组类型转换为对象类型,这个对象类型的键/值都是从元组中遍历出来。例如:consttuple=['tesla','model3','modelX','modelY']a......
  • [Typescript Challenges] 10. Medium - Include
    ImplementtheJavaScript Array.includes functioninthetypesystem.Atypetakesthetwoarguments.Theoutputshouldbeaboolean true or false.Forexa......
  • [Typescript Challenges] 7. Easy - Awaited
    IfwehaveatypewhichiswrappedtypelikePromise.Howwecangetatypewhichisinsidethewrappedtype?Forexample:ifwehave Promise<ExampleType> ho......
  • [Typescript Challenges] 4. Easy - First of Array
    Implementageneric First<T> thattakesanArray T andreturnsit'sfirstelement'stype.typearr1=['a','b','c']typearr2=[3,2,1]typehead1=F......
  • [Typescript Challenges] 5. Easy - Length of Tuple
    Forgivenatuple,youneedcreateageneric Length,pickthelengthofthetupleForexample:typetesla=['tesla','model3','modelX','modelY']typesp......
  • [Typescript Challenges] 6 Easy - Exclude
    Implementthebuilt-inExclude<T,U>Forexample:typeResult=MyExclude<'a'|'b'|'c','a'>//'b'|'c' /*_____________YourCodeHere_____________......
  • Typescript类型体操 - Readonly 2
    题目中文实现一个通用MyReadonly2<T,K>,它带有两种类型的参数T和K。K指定应设置为Readonly的T的属性集。如果未提供K,则应使所有属性都变为只读,就像普通的Readonly<T>一......
  • Typescript类型体操 - Pick
    题目要求实现TS内置的Pick<T,K>,但不可以使用它。从类型T中选择出属性K,构造成一个新的类型。例如:interfaceTodo{title:stringdescription:stringco......