typescript 属性“值”在只读类型上不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48869369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:14:09  来源:igfitidea点击:

Property "value" does not exist on type Readonly

reactjstypescript

提问by Kuzan

I am learning how to develop applications using React as front-end and spring as back-end. While developing a sample application, I encountered an error as follows:

我正在学习如何使用 React 作为前端和 Spring 作为后端开发应用程序。在开发示例应用程序时,我遇到了如下错误:

       `(26,28): Property "value" does not exist on type 'Readonly<{}>`

This error is generated from my App.tsx file which contains the React Components definition in JSX. The class component with the state "value" is defined as follows.

这个错误是由我的 App.tsx 文件产生的,该文件包含 JSX 中的 React Components 定义。状态为“value”的类组件定义如下。

class App extends React.component{
        constructor(props:any) {
             super(props);
             this.state = {
             value:[],
             isLoading:false
          };
       }
        ...
25      render(){
26        const value = this.state.value;
27        const isLoading = this.state.isLoading;
          ... 
    }
   }//End of Class*

I am not understanding what's wrong. Can someone please help me look at it in a different perspective so that this problem can be approached in a different manner?

我不明白出了什么问题。有人可以帮我从不同的角度看待它,以便可以以不同的方式解决这个问题吗?

回答by Anthony N

Did you declare an interface for your state?

你为你的 声明了一个接口state吗?

Take a look at Hello React and TypeScriptwhich shows an example of using interfaces with React Components.

看一看Hello React 和 TypeScript,它显示了使用 React 组件接口的示例。

I suspect you're missing something like this: interface IMyComponentProps { someDefaultValue: string }

我怀疑你缺少这样的东西: interface IMyComponentProps { someDefaultValue: string }

interface IMyComponentState {
    someValue: string
}

class App extends React.Component<IMyComponentProps, IMyComponentState> {
  // ...
}

回答by israr

    export default class ReactTodoWebpart extends React.Component<IReactTodoWebpartProps,any> {
  /**
   *
   */
  constructor(props:IReactTodoWebpartProps) {
    super(props);
    this.state={
      todoItems:[]
    };
    this.props.todoClient.getItems().then(resolvedItems=>{
      this.setState({
        todoItems: resolvedItems,

      })
    });

  }
// I also face this issue and fixed it by using any in the second argument

回答by Kelum Sampath Edirisinghe

could you please try this as follow.

你可以试试这个吗?

class App extends React.component<{},any>{ //your code}