[英]How to define an type such that it only needs to implement one property of another interface
如果我有一个界面:
interface ITest {
a: number;
b: string;
}
我想实现一个接口,这样 function 需要接受一个 object ,其中只包含接口属性的一些定义子集,例如对于上面的例子,它可能只需要接受a
. 我尝试了以下方法:
type WithOnly<T, K extends keyof T> = {
[K in keyof T]: T[K];
}
export const f = (x: WithOnly<ITest, 'a'>) => settings.a * 2;
但是编译器似乎不喜欢这样; 它希望x
也有b
。 有没有办法实现这个?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.