小编Mr.*_*r.B的帖子

Typescript方法重载不同类型的参数但相同的响应

我需要使用 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)

methods overloading typescript

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

methods ×1

overloading ×1

typescript ×1