网站搜索

reactjs - 无法在 React 组件构造函数中分配状态,错误 “不可分配给类型 'Readonly'”


我想为WAP和Web创建一个通用的搜索组件

当我尝试在构造函数中分配状态时,错误:'不可分配给类型 'Readonly''

export interface SearchProps {
    searchValue: string;
    keyword: string;
    words: string[];
    suggestList: Array<{name: string, value: string}>;
    searchHistories: SearchHistory[];
}

export interface SearchState  {
    searchValue: string;
    keyword: string;
    words: string[];
    suggestList: Array<{name: string, value: string}>;
    searchHistories: SearchHistory[];
}

class CommonSearch<P extends SearchProps, S extends SearchState> extends React.Component<P, S> {
    constructor(props: P) {
        super(props);
        // err 'is not assignable to type 'Readonly'.
        this.state = {
            searchValue: "",
            keyword: "",
            words: [],
            suggestList: [],
            searchHistories: []
        }
    }