本质上,我想确保对象参数包含所有必需的属性,但可以包含它想要的任何其他属性。例如:
function foo(bar: { baz: number }) : number {
return bar.baz;
}
foo({ baz: 1, other: 2 });
但这会导致:
Object literal may only specify known properties, and 'other' does not exist in type '{ baz: number; }'.
是的你可以。尝试这个:
interface IBaz {
baz: number;
[key: string]: any;
}
function foo(bar: IBaz) : number {
return bar.baz;
}
foo({ baz: 1, other: 2 });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)