We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接:DeepReadonly
实现类型 DeepReadonly ,将对象的每个子对象也变成只读。
DeepReadonly
import type { Equal, Expect } from "@type-challenges/utils"; type cases = [Expect<Equal<DeepReadonly<X>, Expected>>]; type X = { a: () => 22; b: string; aa: {}; 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 aa: {}; 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"] }]; }; }; };
如果是对象,需要递归调用,但函数不需要
type DeepReadonly<T> = T extends object & { call?: never } ? { readonly [key in keyof T]: DeepReadonly<T[key]> } : T;
{call?: never} 是用来排除函数的
{call?: never}
type DeepReadonly<T> = { readonly [key in keyof T]: T[key] extends Function ? T[key] : T[key] extends object ? DeepReadonly<T[key]> : T[key]; };
通过 T[key] extends Function 判断是不是函数,不是函数的话在判断是不是 object
T[key] extends Function
object
type DeepReadonly<T> = { readonly [key in keyof T]: keyof T[key] extends never ? T[key] : DeepReadonly<T[key]>; }; // 等价于 type DeepReadonly<T> = { readonly [key in keyof T]: keyof T[key] extends object ? T[key] : DeepReadonly<T[key]>; }; // 等价于 type DeepReadonly<T> = keyof T extends never ? T : { readonly [key in keyof T]: DeepReadonly<T[key]> }; // 等价于 type DeepReadonly<T> = keyof T extends object ? T : { readonly [key in keyof T]: DeepReadonly<T[key]> };
keyof T[key] extends never,如果 T[key] 为函数或者空对象, keyof T[key] 结果为 never
keyof T[key] extends never
T[key]
keyof T[key]
never
keyof () => void
keyof {}
同时 never extends object ⇒ true
never extends object
true
这样就把函数和空对象排除掉了
type DeepReadonly<T> = T extends Record<string, unknown> | Array<unknown> ? { readonly [key in keyof T]: DeepReadonly<T[key]> } : T;
const a: Record<string, unknown> = () => 1; // 报错 const c: Record<string, any> = () => 1; // 不报错
所以 Record<string, unknown> 不能用 any 代替 unknown
Record<string, unknown>
any
unknown
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
题目链接:DeepReadonly
实现类型
DeepReadonly
,将对象的每个子对象也变成只读。答案
如果是对象,需要递归调用,但函数不需要
方法一
知识点
{call?: never}
是用来排除函数的方法二
知识点
通过
T[key] extends Function
判断是不是函数,不是函数的话在判断是不是object
方法三
知识点
keyof T[key] extends never
,如果T[key]
为函数或者空对象,keyof T[key]
结果为never
keyof () => void
⇒never
keyof {}
⇒never
同时
never extends object
⇒true
这样就把函数和空对象排除掉了
方法四
知识点
所以
Record<string, unknown>
不能用any
代替unknown
The text was updated successfully, but these errors were encountered: