0

我有一个用打字稿编写的反应组件 PageLayout:

export interface IPageLayoutProps {...}
export interface IPageLayoutActions {...}
export interface IPageLayoutLocalState {...}

export default
class PageLayout<P extends IPageLayoutProps & IPageLayoutActions, S extends Object>
extends React.Component<P, S>
{
    render() {
        return (...)
    }
}

然后我尝试像这样扩展这个组件:

import PageLayout, { IPageLayoutProps, IPageLayoutActions, IPageLayoutLocalState } from './PageLayout'

export interface IPageLayoutSidebarProps {...}
export interface IPageLayoutSidebarActions {...}
interface IPageLayoutSidebarState {sidebarVisible: boolean}

export default
class PageLayoutSidebar<P extends IPageLayoutSidebarProps & IPageLayoutSidebarActions & IPageLayoutProps & IPageLayoutActions, S extends IPageLayoutSidebarState & IPageLayoutLocalState>
extends PageLayout<P, S>
{
    render() {
       return (...)
    }

    toggleSidebar(visible:boolean) {
        this.setState({
            ...this.state,
            sidebarVisible: visible
        })
    }
}

问题是我在尝试调用 this.setState 时遇到错误,它告诉我“传播类型只能从对象类型创建”。任何人都知道状态可能有什么问题以及为什么我不能在其上使用扩展运算符?

4

1 回答 1

0

尝试仅使用:

this.setState({sidebarVisible: visible})
于 2017-02-01T15:34:47.657 回答