【发布时间】:2026-01-19 11:30:02
【问题描述】:
我对 ContainerComponent 和 ContainerProps 有疑问,我认为问题在于“li”是字符串类型,我需要明确将其定义为
React.ElementType
<ListItem
ContainerComponent="li"
ContainerProps={{ ref: provided.innerRef }}
{...provided.draggableProps}
{...provided.dragHandleProps}
style={getItemStyle(
snapshot.isDragging,
provided.draggableProps.style
)}
>
我得到的错误:
没有重载匹配这个调用。
最后一个重载给出了以下错误。
类型 '"li"' 不可分配给类型 'ElementType
【问题讨论】:
-
我对组件不熟悉,但如果它想要 ElemenType,请尝试给它 type -
ContainerComponent={(<li/>).type} -
"我认为问题在于 "li" 是字符串类型" 不是这样。它被推断为字符串文字,这很好。
React.ElementType<React.HTMLAttributes<HTMLDivElement>>类型解析为大约 70 个字符串文字元素名称的联合。我猜"li"不是其中之一吗?我看不到整个列表。 -
Type '"li"' is not assignable to type '"object" | "abbr" | "address" | "article" | "aside" | "b" | "bdi" | "bdo" | "big" | "blockquote" | "caption" | "cite" | "code" | "col" | "colgroup" | "dd" | "del" | "details" | "dfn" | ... 61 more ... | undefined' -
@LindaPaiste 不确定发生了什么,但
'li'是ContainerComponent如果not provided 的默认值 -
完美,感谢分享!
标签: reactjs typescript types material-ui react-props