【问题标题】:Argument type not assignable to parameter type参数类型不可分配给参数类型
【发布时间】:2021-06-14 16:39:53
【问题描述】:

我有一个类型不匹配,我不确定它来自哪里: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>;

props 是这样定义的:

type ElementProps = {
    element: IElementUnion
}

函数签名:

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

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

【问题讨论】:

    标签: reactjs typescript react-native types


    【解决方案1】:

    关键当然是value: Array&lt;_IElement&gt;, in

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

    它必须声明为&lt;IElementUnion&gt;,而不是&lt;_IElement&gt;

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 2018-08-10
      • 2021-10-17
      • 1970-01-01
      • 2021-02-03
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多