使用以下代码;TypeScript鸭式打字:只需要一个属性
interface ParamTest extends Object {
ids?: any;
model?: any;
params?: any;
pageContext?: PageContext;
}
class Foo {
public callMeWithParamsPlease(params: ParamTest): void{
}
public nope() {
this.callMeWithParamsPlease("hi");
}
}
我不知道我将作为参数提供什么。但我知道至少有一个的字段必须填满。如果我使用:
export interface ApiServiceOpts extends Object {
ids?: any;
model?: any;
params?: any;
pageContext: PageContext;
}
然后,它不再与兼容的字符串,如预期 - 因为字符串没有pageContext
。但是我不确定是否会提供pageContext - 但我知道至少需要提供一个字段。是否有任何神奇的打字稿语法允许这个?
也许类似于;
interface PageContext {}
export interface ParamTestId {
ids: any;
}
export interface ParamTestModel {
model: any;
}
export interface ParamTestParams {
params: any;
}
export interface ParamTestPageContext {
pageContext: PageContext;
}
class Foo {
public callMeWithParamsPlease(params: ParamTestId & ParamTestModel & ParamTestParams & ParamTestPageContext): void {
params.ids.a = "";
params.model.b = {}
}
public nope() {
this.callMeWithParamsPlease("hi")
}
}
目前还不清楚你在问什么。 '“它不再与字符串兼容” - 它什么时候与一个字符串兼容,这个“它”是什么?没有你的代码中有一个字符串参数。另外,为什么要扩展Object? –