如何解决此错误TS2322:类型“元素”不能分配给类型“ HTMLLIElement”

时间:2019-03-06 04:41:17

标签: typescript-typings

我想确保将独占<li/>的列表传递给组件,以使它们将显示在由该组件呈现的<ul>元素内。

这是我的代码:

  const menuItems: HTMLLIElement[] = [
    <li>Item 1</li>,
    <li>Item 2</li>,
    <li>Item 3</li>,
  ];

对于数组内<li>的每一行,都会出现此错误:

 error TS2322: Type 'Element' is not assignable to type 'HTMLLIElement'.

我尝试了另一种创建li元素数组的方法

  const menuItems = [
    React.createElement(HTMLLIElement, {value: 'Item 1' }),
    React.createElement(HTMLLIElement, {value: 'Item 2' }),
    React.createElement(HTMLLIElement, {value: 'Item 3' }),
  ]

但是它给出了一个不同的错误:

error TS2345: Argument of type '{ new (): HTMLLIElement; prototype: HTMLLIElement; }' is not assignable to parameter of type 'string | FunctionComponent<{ value: string; }> | ComponentClass<{ value: string; }, any>'.
  Type '{ new (): HTMLLIElement; prototype: HTMLLIElement; }' is not assignable to type 'ComponentClass<{ value: string; }, any>'.
    Type 'HTMLLIElement' is missing the following properties from type 'Component<{ value: string; }, any, any>': context, setState, forceUpdate, render, and 3 more.

我该如何解决这个问题?

0 个答案:

没有答案