type SomeData = {
prop1: string;
prop2: number;
}
type WithChange<T> = { [P in keyof T & string as `${P}Change`]: T[P] };
type SomeMoreData = WithChange<SomeData>; // { prop1Change: string, prop2Change: number }
Here is what you can do.
type SomeMoreDataMapping = {
prop1: "prop1Change"
prop2: "prop2Change"
}
type ValueOf<T> = T[keyof T]
type KeyValueTupleToObject<T extends [keyof any, any]> = {
[K in T[0]]: Extract<T, [K, any]>[1]
}
type MapKeys<T, M extends Record<string, string>> =
KeyValueTupleToObject<ValueOf<{
[K in RequiredKeys<T>]-?: [K extends keyof M ? M[K] : K, T[K]]
}>> & Partial<KeyValueTupleToObject<ValueOf<{
[K in OptionalKeys<T>]-?: [K extends keyof M ? M[K] : K, T[K]]
}>>> extends infer O ? { [K in keyof O]: O[K] } : never;
type RequiredKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? never : K }[keyof T];
type OptionalKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? K : never }[keyof T];
interface SomeData {
prop1?: string;
prop2: number;
prop3?: string;
prop4: number;
}
type SomeMoreData = MapKeys<SomeData, SomeMoreDataMapping>;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…