我走过TS Handbook,来到了映射类型。有一个代码段可将对象属性包装到代理中。
type Proxy<T> = {
get(): T;
set(value: T): void;
}
type Proxify<T> = {
[P in keyof T]: Proxy<T[P]>;
}
function proxify<T>(o: T): Proxify<T> {
// ... wrap proxies ...
}
let proxyProps = proxify(props);
我正在尝试用它的实现来弥补 proxify 函数中的空白,我得到这样的东西:
function proxify<T>(t: T): Proxify<T> {
let result = <Proxify<T>>{};
for (const k in t) {
result[k] = { //(*) from that moment I lose strong typing
get: () => t[k],
set: (value) => t[k] = value
}
}
return result;
}
我无法控制循环内的类型,那里的所有内容都必须是 any 类型。假设我的实现完全正确,该如何解决?
答案 0 :(得分:2)
实现似乎还可以。在创建对象时,会失去一些安全性,但您并不能完全放松。您仍然可以得到的打字量实际上可能会让您感到惊讶
function proxify<T>(t: T): Proxify<T> {
let result = <Proxify<T>>{};
for (const k in t) { // k is of type Extract<keyof T, string> so it must be a key of T
// result[k] and t[k] both work because k is a key of both T and Proxify<T> but result['random'] would be invalid
result[k] = { // get/set fields are checked, so _get would be an error
// the return of get must be T[Extract<keyof T, string>] so ()=> 0 would be an error
get: () => t[k],
// value and t[k] must be T[Extract<keyof T, string>] so t[k] = '' would also be an error
set: (value) => t[k] = value
}
}
return result;
}
答案 1 :(得分:0)
我的实现版本如下。我找不到在get和set中强制类型安全的方法。如果可以通过某种方式编写诸如get():PropType,set(value:PropType)这样的属性,则可能会更加优雅。
function proxify<T>(o: T): Proxify<T> {
const proxifyObj: Proxify<T> = <Proxify<T>>{};
Object.keys(o).map((key) => {
proxifyObj[key] = {
get() {
return o[key];
},
set(value) {
o[key] = value;
}
};
});
return proxifyObj;
}