之前遇到数组对象变量提示不准确
const apis = {
list: ["/list", "获取商品列表", { id: 0 }],
time: ["/time", "获取最新时间"],
};
// 不能提示出id
apis.list[2].
使用 as const 获取准确提示
const apis = {
list: ["/list", "获取商品列表", { id: 0 }],
// ...
} as const;
// 有提示了✨
apis.list[2].id
之前遇到这个场景没有使用这个方法,因为as const会使所有属性变成readonly,没有找到去掉readonly的方法,后面看见泛型函数Required源码
type Required<T> = {
[P in keyof T]-?: T[P];
};
非必填 -> 必填 -?
readonly -> 可写 -readonly
type CanWrite<T> = {
-readonly [K in keyof T]: T[K]
}
数据嵌套
type CanWrite<T> = {
-readonly [K in keyof T]: T[K] extends Record<any, any> ? CanWrite<T[K]> : T[K]
}