[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>,
]
分类:
TypeScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-11-03 [CSS] z-index
2021-11-03 [Javascript] Safely Access a Property on a JavaScript Array with Optional Chaining
2020-11-03 [Javascript] Broadcaster + Operator + Listener pattern -- 13. Repeat When Done with a Repeat Operator
2020-11-03 [Javascript] Broadcaster + Operator + Listener pattern -- 12. Repeat a broadcaster that is DONE
2017-11-03 [Angular] How to get Store state in ngrx Effect
2014-11-03 [Angular-Scaled web] 2. Architecture sub-modules
2014-11-03 [Angular-Scaled web] 1. Architecture and file structure