重载不能仅因返回类型而异 [英] Overloads cannot differ only by return type

查看:52
本文介绍了重载不能仅因返回类型而异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重载一个函数,以便在其他地方使用该函数时,它将正确显示结果,即项目数组、空值或单个项目:

I'm trying to overload a function so when using the function somewhere else, it will show the result properly that is either an array of items, void, or a single item:

getSelected(type): void;
getSelected(type): IDataItem;
getSelected(type): IDataItem[];
getSelected(type:string, one:boolean = true):any {
   if (!type) {
       return;
   }
   if (one) {
      return _.reduce<IDataItem, IDataItem>(sections[type], (current: IDataItem, p: IDataItem) => {
         return p.selected === true ? p : current;
      }, void 0);
   }

   return _.filter<IDataItem>(sections[type], (p: IDataItem):boolean => {
     return p.selected === true && p.enabled === true;
   });
}

它给了我错误错误 TS2175:重载不能仅因返回类型而异.".那么,我如何才能表明返回类型的多种可能性?

It's giving me the error "error TS2175: Overloads cannot differ only by return type.". How can I just signal the many possibilities for the return types then?

推荐答案

关于你的原始代码有几点值得注意...

It is worth noting a couple of points about your original code...

这些是重载签名" - 您可以调用它们.

These are "overload signatures" - you can call these.

getSelected(type): void;
getSelected(type): IDataItem;
getSelected(type): IDataItem[];

下一行是实现签名"——你不能直接调用它.

The next line is the "implementation signature" - you cannot call it directly.

getSelected(type:string, one:boolean = true):any {

这意味着就目前而言,您永远不能传递 one 参数.

This means that as it stands, you cannot ever pass the one argument.

您的重载没有为 type 参数指定类型,这些将具有 any 类型 - 但我认为您可能希望将其限制为字符串.每个签名都需要注释...

Your overloads don't specify a type for the type parameter, these will have the any type - but I think you probably want to restrict this to strings. Each signature needs the annotation...

type: string

因布尔参数值而异

您的签名表明如果您在 one 参数中传递 true,您将得到一个结果.如果您要传递 false,则会得到多个结果.我们现在可以使用它,因为 TypeScript 已经变得更棒.见下文...

Differ based on boolean argument value

Your signature that states that if you pass true in the one argument, you'll get a single result. If you were to pass false, you'd get multiple results. We can now work with that, because TypeScript has been made even more awesome. See below...

根据所有这些信息,您可以使用:

Given all this information, you can use:

getSelected(type: string): void;
getSelected(type: string, one: true): IDataItem;
getSelected(type: string, one: false): IDataItem[];
getSelected(type: string, one: boolean = true): any {
    // ... code!
}

当您调用它时,将根据传递的参数推断类型.这是它的形状,从实际方法中删除了您的代码...

When you call this, the types will be inferred based on the arguments passed. This is the shape of it, with your code removed from the actual method...

interface IDataItem {
    name: string;
}

class Example {
    getSelected(type: string): void;
    getSelected(type: string, one: true): IDataItem;
    getSelected(type: string, one: false): IDataItem[];
    getSelected(type: string, one: boolean = true): void | IDataItem | IDataItem[] {
        // ... code!
    }
}

const example = new Example();

// void return
example.getSelected('just type');

// IDataItem
const a = example.getSelected('x', true);

// IDataItem[]
const b = example.getSelected('x', false);

这篇关于重载不能仅因返回类型而异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆