简体   繁体   English

从接口扩展以添加新属性会导致“X 型不是通用的”

[英]Extending from an interface to add a new property results in "Type X is not generic"

export interface IOptionsPropsExtra extends IOptionsProps<IOptionListBelowProps> { delete: any}

export const SelectedChoiceItem: (optionProps: IOptionsPropsExtra<IOptionListBelowProps>) => React.ReactElement<any> =
    (optionProps: IOptionsPropsExtra<IOptionListBelowProps>) => {
        return (
            <Label className={styles.item}>
                {optionProps.item.name}
                <IconButton iconProps={{ iconName: 'Trash' }} onClick={optionProps.delete} />
            </Label>
        );
    };

I wrote the interface IOptionsPropsExtra to be able to pass an extra argument which is called delete and is a function, but in doing so I am getting:我编写了接口 IOptionsPropsExtra 以便能够传递一个额外的参数,该参数称为 delete 并且是一个函数,但这样做我得到:

Type "IOptionsPropsExtra" is not generic.类型“IOptionsPropsExtra”不是通用的。

Is there something I am misunderstanding about interfaces and how to extends them?我对接口以及如何扩展它们有什么误解吗? I thought extending is basic inheritance which allows you to inherit all of the properties of the interface so that you can add any new one on top of it, which is literally all I am doing.我认为扩展是基本的继承,它允许你继承接口的所有属性,这样你就可以在它上面添加任何新的属性,这就是我所做的。 Why am I getting an error about it not being generic?为什么我收到关于它不是通用的错误? Should it even be a generic type?它甚至应该是泛型类型吗? It's clearly should not be given what I am trying to do plus isn't a generic something like any, which goes against what I am trying to do here?显然不应该给出我正在尝试做的事情,而且不是任何通用的东西,这与我在这里尝试做的事情背道而驰?

The key problem is what do you want IOptionsPropsExtra to be:关键问题是您希望IOptionsPropsExtra是什么:

  1. A non-generic intereface that is an extension of a specific IOptionsProps what always has IOptionListBelowProps as the value of its generic variable, ie the extension of IOptionsProps<IOptionListBelowProps>一个非通用接口,它是特定IOptionsProps的扩展,它总是将IOptionListBelowProps作为其通用变量的值,即IOptionsProps<IOptionListBelowProps>的扩展

  2. A generic interface that extends IOptionsProps and uses the same generic variable as that interface扩展IOptionsProps并使用与该接口相同的通用变量的通用接口

The problem is you wrote the definition for 1, but used it as 2.问题是您为 1 编写了定义,但将其用作 2。

For 1 , you use your interface definition as a regular interface:对于 1 ,您将接口定义用作常规接口:

export const SelectedChoiceItem: (optionProps: IOptionsPropsExtra) => React.ReactElement<any> =
    (optionProps: IOptionsPropsExtra) => {
//...

For 2 , what I suppose you want to do , you leave the const as it is but define the interface as a generic one:对于 2我想你想做什么,你保持常量不变,但将接口定义为通用接口:

export interface IOptionsPropsExtra<T> extends IOptionsProps<T> { delete: any}

To be clear, a "generic" type is one that uses another type as a variable.需要明确的是,“通用”类型是使用另一种类型作为变量的类型。

IOptionsPropsExtra is not a generic type because it can be completely resolved without providing any other type. IOptionsPropsExtra不是泛型类型,因为它可以在不提供任何其他类型的情况下完全解析。

let IOptionsPropsExtra | null = null // works fine.

IOptionsProps is a generic type because it requires another type to be passed to it. IOptionsProps是一种泛型类型,因为它需要将另一种类型传递给它。 You are doing that when you say IOptionsProps<IOptionListBelowProps> .当您说IOptionsProps<IOptionListBelowProps>时,您正在这样做。

So you are locking your IOptionsPropsExtra type to be of type IOptionsProps with generic parameter type of IOptionListBelowProps .那么,你是你锁定IOptionsPropsExtra类型为类型IOptionsProps与泛型参数类型的IOptionListBelowProps

What I think you intend is one of two things.我认为你的意图是两件事之一。

  1. You want the IOptionsPropsExtra to always extend IOptionsProps<IOptionListBelowProps> and no other type should ever be passed to IOptionsProps when you use your custom type.您希望IOptionsPropsExtra始终扩展IOptionsProps<IOptionListBelowProps>并且在使用自定义类型时不应将其他类型传递给IOptionsProps If this what you want then you have declared the type properly and you just use it like any other type without the <ParameterType> syntax.如果这是您想要的,那么您已经正确声明了类型,并且您可以像使用任何其他类型一样使用它,而无需<ParameterType>语法。
(optionProps: IOptionsPropsExtra) => {
  1. Or you want the IOptionsPropsExtra to be generic, just like the type it extends.或者您希望IOptionsPropsExtra是通用的,就像它扩展的类型一样。 In that case, you need to declare it like as a generic, and forward the generic parameter to the type that it extends:在这种情况下,您需要将其声明为泛型,并将泛型参数转发给它扩展的类型:
export interface IOptionsPropsExtra<T> extends IOptionsProps<T> {
    delete: any
}

And use it like this:并像这样使用它:

(optionProps: IOptionsPropsExtra<IOptionListBelowProps>) => {

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM