【发布时间】:2021-02-25 18:47:09
【问题描述】:
我正在尝试构建一个简单的抽象来列出对象的补丁。
type MyObject = {
attributeA: string;
attributeB: boolean;
attributeC: number;
};
type MyObjectKeys = keyof MyObject;
type Difference<Key extends MyObjectKeys = MyObjectKeys> = {
// The value of this attribute should determine
// the type of the old and new value.
key: Key;
oldValue: MyObject[Key];
newValue: MyObject[Key];
};
type Patch = {
patches: Difference[];
};
const patch: Patch = {
patches: [
{
key: 'attributeB',
// Should be inferred as boolean.
oldValue: '',
// Both should have the same inferred type.
newValue: 9,
},
],
};
我希望根据给定的key 输入oldValue 和newValue。
不幸的是,正如代码 cmets 中所述,它不起作用。
感谢您的任何建议!
【问题讨论】:
标签: typescript types type-inference