我正在Typescript中尝试函数重载,但出现以下错误:
错误:(2,5)TS2394:重载签名与函数实现不兼容。
我不知道如何解决此错误。任何帮助将不胜感激。
create(key: string | TextKeys, reKey: string | TextKeys, params?: ObjectLiteral<string>, subkeys?: { [key: string]: string }, selector?: Selector): Either<EError, (AOutput | ROutput)[]>;
create(key: string | TextKeys, params?: ObjectLiteral<string>, subkeys?: { [key: string]: string }, selector?: Selector): Either<EError, AOutput>;
create(key: string | TextKeys, reKey?: string | TextKeys, params?: ObjectLiteral<string>, subkeys?: { [key: string]: string }, selector: Selector = selectRandom) {
const output: Either<EError, AOutput> = this.createAOutputs(key, params, subkeys).map(selector);
if (reKey !== undefined) {
return List.fromArray([output, this.createROutputs(reKey, params, subkeys)]).sequenceEither<EError, AOutput | ROutput>().map(arr => _.flatten(arr.toArray())) as Either<EError, (AOutput | ROutput)[]>;
} else {
return output;
}
}
答案 0 :(得分:1)
我不知道如何解决此错误。任何帮助将不胜感激。
您的最后一个函数签名必须对每个可能的重载都起作用。一个快速的技巧来说明什么可行:
create(key: string | TextKeys, reKey: string | TextKeys, params?: ObjectLiteral<string>, subkeys?: { [key: string]: string }, selector?: Selector): Either<EError, (AOutput | ROutput)[]>;
create(key: string | TextKeys, params?: ObjectLiteral<string>, subkeys?: { [key: string]: string }, selector?: Selector): Either<EError, AOutput>;
create(...iWorkWithAllOptions:any[]) { // NOTICE
// implement
}