TS2740 类型缺少 ReadOnly<MyInterface> 中的以下属性;带有 TypeScript 应用程序的 React Native 中的错误 [英] TS2740 Type is missing the following properties from ReadOnly&lt;MyInterface&gt; error in React Native with TypeScript app

查看:157
本文介绍了TS2740 类型缺少 ReadOnly<MyInterface> 中的以下属性;带有 TypeScript 应用程序的 React Native 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 StatelessComponent 已被弃用,我正在尝试将所有组件转换为类.

Since StatelessComponent is deprecated, I am trying to turn all the components into classes.

我有一个接口,例如:

interface MyInterface{
    prop1: number;
    prop2: boolean;
}

我在课堂上使用它:

class MyComponent extends React.Component<MyInterface> {
   ...
   public render(){...}
}

当我尝试像这样使用 MyComponent 时:

And when I try to use MyComponent like this:

<MyComponent 
    prop1={3}
    prop2={false}
/>

我收到此错误:

TS2740:类型 {prop1: 3, prop2: false} 缺少 ReadOnly 类型中的以下属性:render、context、setState、forceUpdate 等.

TS2740: Type {prop1: 3, prop2: false} is missing the following properties from type ReadOnly: render, context, setState, forceUpdate, and 3 more.

有什么解决办法吗?

推荐答案

我最终通过将类的声明更改为 class MyComponent extends React.Component 解决了问题.

I finally solved the problem by changing the declaration of the class to class MyComponent extends React.Component<any, MyInterface>.

这篇关于TS2740 类型缺少 ReadOnly<MyInterface> 中的以下属性;带有 TypeScript 应用程序的 React Native 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆