参数类型不可分配给参数类型

时间:2021-03-17 10:43:01

标签: reactjs typescript react-native types

我有一个类型不匹配,我不确定它来自哪里:Error:(35, 68) TS2345: Argument of type '(subElement: IElementUnion, i: number) => JSX.Element' is not assignable to parameter of type '(value: _IElement, index: number, array: _IElement[]) => Element'. Types of parameters 'subElement' and 'value' are incompatible. Type '_IElement' is not assignable to type 'IElementUnion'. Type '_IElement' is missing the following properties from type 'IControlledNumber': value, type

接口定义如下:

// SHEET ELEMENTS
interface _IElement {
    name: string
}

export interface ISection extends _IElement{
    value: Array<_IElement>,
    type: elementType.SECTION,
}

...

export type IElementUnion = ISection | IText | IControlledNumber

该函数在 React 组件中调用:

export const Section: FunctionComponent<ElementProps>  = ({element}) =>
    <ColumnView>
        <Line/>
        <Text75>{element.name}</Text75>
        {element.type === elementType.SECTION && element.value.map((subElement: IElementUnion, i: number) =>
            transformToJsx(subElement, i))}
    </ColumnView>;

道具定义如下:

type ElementProps = {
    element: IElementUnion
}

函数签名:

export function transformToJsx(element: IElementUnion, i: number)

现在我在调用和函数定义中都不使用 _IElement 类型 - 那么类型不兼容从何而来?

1 个答案:

答案 0 :(得分:0)

当然关键是value: Array<_IElement>, in

export interface ISection extends _IElement{
    value: Array<_IElement>,
    type: elementType.SECTION,
}

它必须声明为 <IElementUnion>,而不是 <_IElement>