TypeScript语言规范的第6.3节讨论了函数重载,并给出了如何实现它的具体示例.但是,如果我尝试这样的事情:
export class LayerFactory {
constructor (public styleFactory: Symbology.StyleFactory) { }
createFeatureLayer (userContext : Model.UserContext, mapWrapperObj : MapWrapperBase) : any {
throw "not implemented";
}
createFeatureLayer(layerName : string, style : any) : any {
throw "not implemented";
}
}
Run Code Online (Sandbox Code Playgroud)
即使函数参数属于不同类型,我也会收到指示重复标识符的编译器错误.即使我在第二个createFeatureLayer函数中添加了一个额外的参数,我仍然会遇到编译器错误.想法,请.
我有一个打字稿类:
class ContactModel {
public getUsage(type: string): restangular.IElement {
return this.getBase().one('usages', type);
}
public getUsage(customerId: number, type: string): restangular.IElement {
return this.ModelFactory.createRequestMapper(ContactModel.options)
.one('customers', customerId).all('contacts/usages', type);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
这会导致编译器抛出以下错误:
>> app/modules/common/model/ContactModel.ts(27,12): error TS2393: Duplicate function implementation.
>> app/modules/common/model/ContactModel.ts(31,12): error TS2393: Duplicate function implementation.
Run Code Online (Sandbox Code Playgroud)
我在这个例子和TypeScript手册之间看到的唯一区别是他们的例子有不同的返回类型,并且我有相同的返回类型(两种情况都有不同的输入参数).
问题是:我做错了什么 - 或者打字稿类方法是否需要使用不同的方法参数类型来允许重载?这看起来很愚蠢,因为.Net和Java都支持使用相同的返回类型和不同的输入类型进行重载.