huihuilang53 / ts-challenges

ts类型体操刷题总结

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deepreadonly

huihuilang53 opened this issue · comments

commented

/*
9 - 深度 Readonly

by Anthony Fu (@antfu) #中等 #readonly #object-keys #deep

题目

由谷歌自动翻译,欢迎 PR 改进翻译质量。

实现一个通用的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`

在 Github 上查看:https://tsch.js.org/9/zh-CN
*/

/* _____________ 你的代码 _____________ */

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

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
Expect<Equal<DeepReadonly, Expected>>,
]

type X = {
a: () => 22
b: string
c: {
d: boolean
e: {
g: {
h: {
i: true
j: 'string'
}
k: 'hello'
}
l: [
'hi',
{
m: ['hey']
},
]
}
}
}

type Expected = {
readonly a: () => 22
readonly b: string
readonly c: {
readonly d: boolean
readonly e: {
readonly g: {
readonly h: {
readonly i: true
readonly j: 'string'
}
readonly k: 'hello'
}
readonly l: readonly [
'hi',
{
readonly m: readonly ['hey']
},
]
}
}
}

/* _____________ 下一步 _____________ /
/

分享你的解答:https://tsch.js.org/9/answer/zh-CN
查看解答:https://tsch.js.org/9/solutions
更多题目:https://tsch.js.org/zh-CN
*/