You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Panel组件样式报错TS2322:类型不兼容如何修复?

修复Panel组件的TypeScript样式类型错误

你遇到的这个TS类型错误,是因为Panel组件的styles属性结构不符合IPanelStyles的定义。

错误原因

你把closeButton的样式嵌套在了root对象里,但在Fluent UI的IPanelStyles接口中,closeButton是和root同级的独立样式属性,并非root的子属性,所以类型检查不通过。

修复代码

将styles的结构调整为正确的层级:

<Panel
    isOpen={this.state.showPanel}
    onDismiss={this.onPanelClose}
    type={PanelType.medium}
    hasCloseButton={true}
    closeButtonAriaLabel="Close Submission Details"
    headerText={escape(strings.ListSubmissionsStatusPanelHeader)}
    isFooterAtBottom={true}
    onRenderFooterContent={this.renderFooter}
    styles={{ closeButton: { margin: '2px' } }}
>

如果需要同时设置root的样式,可以这样写:

styles={{
    root: { /* 这里写root的样式 */ },
    closeButton: { margin: '2px' }
}}

调整后,样式结构就符合IStyleFunctionOrObject<IPanelStyleProps, IPanelStyles>的类型要求,TS错误会消失。

内容的提问来源于stack exchange,提问作者user989988

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 04:50:24