需要一些帮助来完成通用的object.map函数,我目前的实现在编译器上极其缓慢,直到它中断(但可以正常工作),并且目前我还没有想到推断ReturnType的好方法
type DeepKeyOf<T extends object> = {
[K in keyof T]: T[K] extends object ? DeepKeyOf<T[K]> | { key: K; value: T[K] } : { key: K; value: T[K] }
}[keyof T];
function objectMap<T extends object, B>(obj: T, mapper: (value: DeepKeyOf<T>) => any): T {
return Object.keys(obj).reduce((newObj, key) => {
return { ...newObj, [key]: mapper(obj[key]) };
}, {}) as any;
}
这将返回{key,value}对象文字对的已区分的并集,以便您可以使用key ===“ foo”来检查键,并获得该键的类型保护值,这正是我想要的。 / p>
2个问题。
如果有人说他们可以用更高种类的类型解决问题,那么可以给我提供带有HKT的scala语法的伪代码,我可以写出一个应该完成的实现。
编辑:
type DeepKeyOf<T extends object> = {
[K in keyof T]: T[K] extends object ? DeepKeyOf<T[K]> | { key: K; value: T[K] } : { key: K; value: T[K] }
}[keyof T];
interface IPerson {
name: "susan";
children: {
billy: string;
sally: Date;
};
}
会回馈
{key: "name", value: "susan"} | {key: "children", value: {billy: string, sally: Date} | {key: "billy", value: string} | {key: "sally", value: Date}
编辑:这是距离iv'e很近的距离,但是只有当您删除了“ if key ==“ dob”“单独处理所有键时,它才起作用,ReturnType并不是我想要的,因为Dob是其他类型的并集。
//我也只是将其做成浅的object.map,以使其在编译器和我自己上都更容易。
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void)
? I
: never;
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type ShallowKeyOf<T extends object> = { [K in keyof T]: { key: K; value: T[K] } }[keyof T];
interface IPerson {
name: "susan";
children: {
billy: number;
sally: Date;
};
dob: Date;
}
type GenerateRecord<Tuple extends [any, any]> = Tuple extends infer SELF
? SELF extends [infer K, infer V] ? (K extends string ? Record<K, V> : never) : never
: never;
type ChangeValue<T extends object, Tuple extends [keyof T, any]> = Omit<T, Tuple[0]> extends infer SELF
? SELF & UnionToIntersection<GenerateRecord<Tuple>>
: never;
function objectMap<T extends object, TupleReturn extends [keyof T & string, any]>(
obj: T,
mapper: (value: ShallowKeyOf<T>) => TupleReturn
): ChangeValue<T, TupleReturn> {
return Object.keys(obj).reduce((newObj, key) => {
return { ...newObj, [key]: mapper((obj as any)[key]) };
}, {}) as any;
}
const test = objectMap(("" as any) as IPerson, ({ key, value }) => {
if (key === "name") {
return [key, new Date()];
}
if (key === "dob") {
return [key, "NOW STRING"]
}
return [key, new Date()];
});
答案 0 :(得分:0)
这是我分解对象眼键和非对象眼键的一种方法。我认为它的类型空间比extend object
大得多,并且通常会给我带来麻烦,因为它包括日期之类的东西。
type NonObject = Date | RegExp | string | number;
type ObjectValuedKeys<T> = {
[K in keyof T]: T[K] extends NonObject ?
never :
{ o: K }
}[keyof T]['o'];
type NonObjectValuedKeys<T> = {
[K in keyof T]: T[K] extends NonObject ?
{ o: K } :
never
}[keyof T]['o'];
type NonObjectKeyValues<T> = {
[K in NonObjectValuedKeys<T>]: {
key: K;
value: T[K];
}
}[NonObjectValuedKeys<T>];
type ObjectKeyValues<T> = {
[K in ObjectValuedKeys<T>]: MapKeys<T[K]>
}[ObjectValuedKeys<T>];
type MapKeys<T> =
{
v: NonObjectKeyValues<T>,
k: ObjectKeyValues<T>;
}['v' | 'k'];
interface IPerson {
name: 'susan';
children: {
billy: string;
sally: Date;
deeper: {
norma: RegExp,
}
};
}
MapKeys<IPerson>
给了我
{
key: "name";
value: "susan";
} | {
key: "billy";
value: string;
} | {
key: "sally";
value: Date;
} | {
key: "norma";
value: RegExp;
}