简体   繁体   English

通用 function 用于 Typescript 中的两种不同类型的接口

[英]Generic function for two different types of interfaces in Typescript

I am writing a React application (in TypeScript), in which I've two useState objects that will tell if an addon/accessory have been removed from a product, for visual purposes.我正在编写一个 React 应用程序(在 TypeScript 中),其中我有两个 useState 对象,用于判断是否从产品中删除了插件/附件。 Products can have both accessories and addons.产品可以有附件和插件。

First things first.第一件事。 I have two different interfaces for addons and accessories:我有两个不同的插件和附件界面:

interface IAddon {
    id: string;
    // some other values
}

interface IAccessory {
    id: string;
    // some other values
}

Both of my above interfaces are contained within a parent interface known as a product:我上面的两个接口都包含在一个称为产品的父接口中:

interface IProduct {
    rowId: string;
    accessories: IAccessory[];
    addons: IAddon[];
    // other values
}

I have created two useStates that can contain a list of inactive accessories and addons sorted by the key of the product, as I have a list of products for which users can add or remove either accessories or addons.我创建了两个 useStates,它们可以包含按产品键排序的非活动附件和插件列表,因为我有一个产品列表,用户可以为其添加或删除附件或插件。 Once a product is inactive it's visuals are supposed to change.一旦产品处于非活动状态,它的视觉效果应该会发生变化。

interface IAddonsWithId {
    [rowId: string]: IAddon[]
}

interface IAccessoriesWithId {
    [rowId: string]: IAccessory[]
}

I am trying to create a key pair function that work for both above interfaces and can add or remove from either of them.我正在尝试创建一个适用于上述两个接口的密钥对 function 并且可以从它们中的任何一个中添加或删除。 I have the below mentioned useStates.我有下面提到的useStates。

const [inactiveAddons, setInactiveAddons] = useState<IAddonsWithId>({});
const [inactiveAccessories, setInactiveAccessories] = useState<IAccessoriesWithId>({});

and I have created this tiny interface to handle in the following function:我创建了这个小接口来处理以下 function:

interface IKeyPair {
    rowId: string;
    objectId: string;
    object: IAddonsWithId | IAccessoriesWithId;
}

So with that I'm trying to create this function:因此,我正在尝试创建这个 function:

const sortOutObjectFromList = ({rowId, objectId, object}: IKeyPair) => {
    return {
        ...object,
        [rowId]: object[rowId].filter( // <--- This line troubles me (TS:2349)
            (item: IAddon | IAccessory) => item.id !== objectId,
        ),
    };
};

The idea of the function is to be able to run through both my addon and accessory lists and be able to sort out the item that should have removed it's inactive state. function 的想法是能够遍历我的插件和附件列表,并能够整理出应该删除它的非活动 state 的项目。 I will have to create a similar function that will add items to the inactive list, but first I need to figure out how to satisfy TSLint.我将不得不创建一个类似的 function 将项目添加到非活动列表,但首先我需要弄清楚如何满足 TSLint。

I am getting the following error on the line marked above:我在上面标记的行上收到以下错误:

TS2349: This expression is not callable. Each member of the union type '{ <S extends IAddon>(callbackfn: (value: IAddon, index: number, array: IAddon[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: IAddon, index: number, array: IAddon[]) => unknown, thisArg?: any): IAddon[]; } | {...; }' has signatures, but none of those signatures are compatible with each other.

My code should be compileable, as is, but I'd like to avoid the TSLint error as I have set up pre-commit hooks to check for that.我的代码应该是可编译的,但我想避免 TSLint 错误,因为我已经设置了预提交挂钩来检查它。 I've tried searching google and here on stackoverflow, but have been unable to figure out completely how to fix this error.我试过在stackoverflow上搜索google和here,但一直无法完全弄清楚如何解决这个错误。

I believe the issue is the mix of the two interfaces IAddon and IAccessory , as they do not share many of the same properties, except for the id .我认为问题在于IAddonIAccessory这两个接口的混合,因为它们不共享许多相同的属性,除了id which is the only one I try to match on.这是我唯一尝试匹配的。

How do I remove this TSLint error?如何删除此 TSLint 错误?

Try this, I think object[rowId] is coming as undefined.试试这个,我认为 object[rowId] 是未定义的。

const sortOutObjectFromList = ({rowId, objectId, object}: IKeyPair) => {
    return {
        ...object,
        [rowId]: 
         object[rowId]?.filter((item: IAddon | IAccessory) => item.id !== objectId) ?? []
    };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React TypeScript 组件具有两个不同的 prop 接口 - React TypeScript component with two different prop interfaces 将具有两种泛型类型的泛型接口组合为仅具有一种泛型类型的一种类型 - Combine generic interfaces with two generic types into one type with only one generic type Typescript使用通用参数/返回类型来响应无状态函数 - Typescript React stateless function with generic parameter/return types 带有通用类型 TypeScript 的自定义钩子 - Custom Hook with Generic Types TypeScript 以类型为参数的通用 TypeScript API - Generic TypeScript API with types as parameters 如何将我的 nextjs typescript(接口/类型)与 nodejs 服务器 typescript 模型(接口/类型)一起保存? - How I can keep my nextjs typescript (interfaces / types) with nodejs server typescript models (interfaces / types)? 通过 typescript 中函数的可选参数返回不同的泛型类型 - return different generic type via function's optional argument in typescript 在 TypeScript 中接受动态属性名称的不同 function 返回类型 - Accepting different function return types for dynamic property names in TypeScript 使用 Typescript 在 React 中将 function 作为具有不同参数类型的道具传递 - Passing a function as a prop with different parameter types in React with Typescript Typescript 两个接口相互引用,可能吗? - Typescript two interfaces referencing each other, is it possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM