【发布时间】:2019-04-05 09:09:24
【问题描述】:
interface A { a?: number };
interface B { a?: string };
function copy<
Source extends object,
Destination extends { [destinationKey in keyof Source]?: (1) }
>(
source: Source,
key: keyof Source,
destination: Destination,
transformer: (value: (2)) => (3)
) {
if (source[key] !== undefined) {
destination[key] = transformer ? transformer(source[key]) : source[key];
}
}
const a: A = { a: 123 };
const b: B = {};
copy(a, "a", b, (value) => value.toString());
在上面的例子中,我可以为以下占位符使用什么:
-
(1) -
Destination中的值类型与Source中的相应键相关联。 -
(2) -
Source中的值类型,与参数key指定的键相关联。 -
(3) -
Destination中的值类型,与参数key指定的键相关联。
【问题讨论】:
标签: typescript generics types typescript3.0