我有一个 TypeScript 中的 React 应用程序,其代码包含
export interface State {
expandedRows: any,
}
class MyComponent extends React.Component<Props, State> {
state = {
expandedRows: [],
};
isExpanded = (rowId: number) => {
return this.state.expandedRows.includes(rowId);
}
并且它会发出错误 TypeScript 错误:“number”类型的参数不能分配给“never”类型的参数。 TS2345
当我将 rowId
变量输入 includes
方法时。
我尝试将 ExpandRows 的类型更改为 Array,但出现相同的错误。我尝试从 State 接口(interface)中删除 ExpandedRows,但出现了相同的错误。我觉得也许我的 State 接口(interface)现在以某种方式被分配给 this.state
。或者,includes
方法可能没有为其输入指定类型。我该如何解决这个问题?
最佳答案
除了其他好的答案之外 - 您可以指定状态
的类型
state: State = {
expandedRows: [],
};
关于reactjs - 在 React 中使用 state 和 include 会给出 Argument of type 'number' is not assignable to parameter of type 'never' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59398557/