我需要使用 TypeScript 重载一个方法。
FooModel有 6 个参数,但 2 个字符串参数是唯一的强制参数。因此,我不想每次想使用myMethod时都创建FooModel,而是想重载myMethod并在其中创建一次FooModel,然后在返回之前创建其余逻辑。
我已经根据迄今为止在网上找到的内容尝试过此操作,但出现以下错误:
TS2394: This overload signature is not compatible with its implementation signature.
Run Code Online (Sandbox Code Playgroud)
该错误的解决方案与我的方法不兼容
static async myMethod(model: FooModel): Promise<BarResult>
static async myMethod(inputText: string, outputText: string): Promise<BarResult>{
//implementation;
return new BarResult(); //Different content based on the inputs
}
Run Code Online (Sandbox Code Playgroud)