Interface 具有重载的可调用接口

Interface 具有重载的可调用接口,interface,typescript,overloading,Interface,Typescript,Overloading,我想将(外部)函数与多个类型关联。我决定将它们放入一个幕后阵列,大致如下: var labelGenerators : ILabelGenerator[] = [] 其中,ILabelGenerator应该是类型可以具有的不同形状的通用接口,例如: interface ILabelGenerator{ (container : SomeContainerObject, delta: IDeltaValues): string; (container : SomeContainerObj

我想将(外部)函数与多个类型关联。我决定将它们放入一个幕后阵列,大致如下:

var labelGenerators : ILabelGenerator[] = []
其中,
ILabelGenerator
应该是类型可以具有的不同形状的通用接口,例如:

interface ILabelGenerator{
  (container : SomeContainerObject, delta: IDeltaValues): string;
  (container : SomeContainerObject, x: number, y: number): string;
  (container : SomeContainerObject, x: Date, y: number): string;
}
labelGenerators[0] = (container:SomeContainerObject, x:number, y: number) => {
   return "label 0"; //the real code uses the parameters
}
现在,我不知道如何将项添加到
labelGenerators
数组中,因为如果我执行以下操作:

interface ILabelGenerator{
  (container : SomeContainerObject, delta: IDeltaValues): string;
  (container : SomeContainerObject, x: number, y: number): string;
  (container : SomeContainerObject, x: Date, y: number): string;
}
labelGenerators[0] = (container:SomeContainerObject, x:number, y: number) => {
   return "label 0"; //the real code uses the parameters
}
我得到一个类型
(container:SomeContainerObject,x:number,y:number)=>字符串不可分配给类型ILabelGenerator
错误

如何解决这个问题?
(我使用的是TypeScript 1.3,但由于我有大约10种调用形状,1.4联合类型将非常笨拙)

因为第二个参数需要与所有签名兼容。使用x:any

,因为第二个参数需要与所有签名兼容。使用x:any

,因为第二个参数需要与所有签名兼容。使用x:any

,因为第二个参数需要与所有签名兼容。使用x:any

这可能会避免编译器错误,因为您明确告诉TS:

labelGenerators[0] = <ILabelGenerator>(
     (container: SomeContainerObject, x: number, y: number) => { 
         return "label 0"; //the real code uses the parameters 
     });
labelGenerators[0]=(
(容器:SomeContainerObject,x:number,y:number)=>{
return“label 0”;//实际代码使用参数
});

这可能会避免编译器错误,因为您明确告诉TS:

labelGenerators[0] = <ILabelGenerator>(
     (container: SomeContainerObject, x: number, y: number) => { 
         return "label 0"; //the real code uses the parameters 
     });
labelGenerators[0]=(
(容器:SomeContainerObject,x:number,y:number)=>{
return“label 0”;//实际代码使用参数
});

这可能会避免编译器错误,因为您明确告诉TS:

labelGenerators[0] = <ILabelGenerator>(
     (container: SomeContainerObject, x: number, y: number) => { 
         return "label 0"; //the real code uses the parameters 
     });
labelGenerators[0]=(
(容器:SomeContainerObject,x:number,y:number)=>{
return“label 0”;//实际代码使用参数
});

这可能会避免编译器错误,因为您明确告诉TS:

labelGenerators[0] = <ILabelGenerator>(
     (container: SomeContainerObject, x: number, y: number) => { 
         return "label 0"; //the real code uses the parameters 
     });
labelGenerators[0]=(
(容器:SomeContainerObject,x:number,y:number)=>{
return“label 0”;//实际代码使用参数
});

您的原始界面没有说“它可以是其中的任何一个”,而是说“它将与所有这些兼容”-以满足您必须使用加宽类型或实现所有适当的重载

你想要的界面是“它将是其中之一”。在这种情况下,您应该实际创建包含实际合同的接口。实现在契约中承诺的唯一一件事是第一个参数的类型为
SomeContainerObject
。就附加参数而言,所有的赌注都没有了

契约允许调用代码知道它可以依赖什么。因此,我将使用以下界面:

interface ILabelGenerator{
  (container: SomeContainerObject, ...additional: any[]): string;
}
使用此接口的原因是,这是关于
ILabelGenerator
实现的所有承诺

例如,当您键入以下内容时

labelGenerators[0](
你会得到真实的反映


您的原始接口优于承诺,因为它表明“您可以调用您的首选签名”。这个版本说“我不知道你第一次争论后应该提供什么”。i、 这是事实

您的原始接口没有说“它可以是其中的任何一个”,而是说“它将与所有这些兼容”-以满足您必须使用加宽的类型或实现所有适当的重载

你想要的界面是“它将是其中之一”。在这种情况下,您应该实际创建包含实际合同的接口。实现在契约中承诺的唯一一件事是第一个参数的类型为
SomeContainerObject
。就附加参数而言,所有的赌注都没有了

契约允许调用代码知道它可以依赖什么。因此,我将使用以下界面:

interface ILabelGenerator{
  (container: SomeContainerObject, ...additional: any[]): string;
}
使用此接口的原因是,这是关于
ILabelGenerator
实现的所有承诺

例如,当您键入以下内容时

labelGenerators[0](
你会得到真实的反映


您的原始接口优于承诺,因为它表明“您可以调用您的首选签名”。这个版本说“我不知道你第一次争论后应该提供什么”。i、 这是事实

您的原始接口没有说“它可以是其中的任何一个”,而是说“它将与所有这些兼容”-以满足您必须使用加宽的类型或实现所有适当的重载

你想要的界面是“它将是其中之一”。在这种情况下,您应该实际创建包含实际合同的接口。实现在契约中承诺的唯一一件事是第一个参数的类型为
SomeContainerObject
。就附加参数而言,所有的赌注都没有了

契约允许调用代码知道它可以依赖什么。因此,我将使用以下界面:

interface ILabelGenerator{
  (container: SomeContainerObject, ...additional: any[]): string;
}
使用此接口的原因是,这是关于
ILabelGenerator
实现的所有承诺

例如,当您键入以下内容时

labelGenerators[0](
你会得到真实的反映


您的原始接口优于承诺,因为它表明“您可以调用您的首选签名”。这个版本说“我不知道你第一次争论后应该提供什么”。i、 这是事实

您的原始接口没有说“它可以是其中的任何一个”,而是说“它将与所有这些兼容”-以满足您必须使用加宽的类型或实现所有适当的重载

你想要的界面是“它将是其中之一”。在这种情况下,您应该实际创建包含实际合同的接口。唯一值得一提的是