正文从这开始~

总览

当我们没有为函数组件或者类组件的props声明类型,或忘记为React安装类型声明文件时,会产生"Parameter 'props' implicitly has an 'any' type"错误。为了解决这个错误,在你的组件中明确地为props对象设置一个类型。

安装类型文件

你首先要确定的是你已经安装了React类型声明文件。在项目的根目录下打开终端,并运行以下命令。

# ️ with NPM
npm install --save-dev @types/react @types/react-dom # ---------------------------------------------- # ️ with YARN
yarn add @types/react @types/react-dom --dev

尝试重启你的IDE和开发服务。

声明类型

如果这没有帮助,你有可能忘记明确地为函数组件或类组件的props声明类型。

// App.tsx

// ️ Parameter 'props' implicitly has an 'any' type.ts(7006)
function Person(props) {
return (
<div>
<h2>{props.name}</h2>
<h2>{props.age}</h2>
<h2>{props.country}</h2>
</div>
);
} function App() {
return (
<div>
<Person name="James" age={30} country="Australia" />
</div>
);
} export default App;

上述代码片段的问题在于,我们没有为函数组件的props设置类型。

为了解决该错误,我们必须显示地props参数类型。

// App.tsx

interface PersonProps {
name: string;
age: number;
country: string;
children?: React.ReactNode; // ️ for demo purposes
} function Person(props: PersonProps) {
return (
<div>
<h2>{props.name}</h2>
<h2>{props.age}</h2>
<h2>{props.country}</h2>
</div>
);
} function App() {
return (
<div>
<Person name="James" age={30} country="Australia" />
</div>
);
} export default App;

我们为组件的props显示地声明了一个接口,这就可以解决错误。

我们不需要设置children属性,但如果你向你的组件传递children,你就必须这样做。

如果你不想为你的组件明确地声明props对象类型,那么你可以使用any类型。

// App.tsx

// ️ set type to any
function Person(props: any) {
return (
<div>
<h2>{props.name}</h2>
<h2>{props.age}</h2>
<h2>{props.country}</h2>
</div>
);
} function App() {
return (
<div>
<Person name="James" age={30} country="Australia" />
</div>
);
} export default App;

any类型有效地关闭了类型检查,所以我们能够向组件传递props,并访问对象上的属性,而不会得到任何类型检查错误。

泛型

如果你有一个类组件,可以使用泛型来为其propsstate声明类型。

// App.tsx

import React from 'react';

interface PersonProps {
name: string;
age: number;
country: string;
children?: React.ReactNode;
} interface PersonState {
value: string;
} // ️ React.Component<PropsType, StateType>
class Person extends React.Component<PersonProps, PersonState> {
render() {
return (
<div>
<h2>{this.props.name}</h2>
<h2>{this.props.age}</h2>
<h2>{this.props.country}</h2>
</div>
);
}
} export default function App() {
return (
<div>
<Person name="James" age={30} country="Australia" />
</div>
);
}

我们明确地为组件的propsstate提供了类型,这就解决了这个错误。

如果你不想明确地为你的组件的propsstate提供类型,你可以使用any类型。

// App.tsx

import React from 'react';

// ️ type checking disabled for props and state
class App extends React.Component<any, any> {
constructor(props: any) {
super(props); this.state = {value: ''};
} handleChange = (event: any) => {
this.setState({value: event.target.value});
}; render() {
return (
<div>
<form>
<input
onChange={this.handleChange}
type="text"
value={this.state.value}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
} export default App;

我们在输入propsstate对象时使用了any类型,这有效地关闭了类型检查。

现在你将能够访问this.propsthis.state对象上的任何属性而不会得到类型检查错误。

重新安装

如果错误没有解决,尝试删除你的node_modulespackage-lock.json(不是package.json)文件,重新运行npm install并重新启动你的IDE。

# ️ delete node_modules and package-lock.json
rm -rf node_modules
rm -f package-lock.json # ️ clean npm cache
npm cache clean --force npm install

如果错误仍然存在,请确保重新启动你的IDE和开发服务。VSCode经常出现故障,重启有时会解决问题。

React报错之Parameter 'props' implicitly has an 'any' type的更多相关文章

  1. React报错之Parameter 'event' implicitly has an 'any' type

    正文从这开始~ 总览 当我们不在事件处理函数中为事件声明类型时,会产生"Parameter 'event' implicitly has an 'any' type"错误.为了解决 ...

  2. React报错之Property 'value' does not exist on type EventTarget

    正文从这开始~ 总览 当event参数的类型不正确时,会产生"Property 'value' does not exist on type EventTarget"错误.为了解决 ...

  3. React报错之Property 'X' does not exist on type 'HTMLElement'

    正文从这开始~ 总览 在React中,当我们试图访问类型为HTMLElement 的元素上不存在的属性时,就会发生Property 'X' does not exist on type 'HTMLEl ...

  4. React报错之Property 'value' does not exist on type 'HTMLElement'

    正文从这开始~ 总览 当我们试图访问一个类型为HTMLElement的元素上的value属性时,会产生"Property 'value' does not exist on type 'HT ...

  5. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  6. notification 报错the method build() is undefined for the type Notificatin.Builder

    notification 报错the method build() is undefined for the type Notificatin.Builder 这事api版本号太低导致的 Notifi ...

  7. react报错 TypeError: Cannot read property 'setState' of undefined

    代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...

  8. react报错this.setState is not a function

    当报错这个的时候就要看函数是否在行内绑定this,或者在constructor中绑定this. 我这里犯的错误的是虽然我在constructor中绑定了this,但是语法写的不正确. 错误示范: co ...

  9. MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]

    问题背景: 在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下: com.chenzhou.base.mybati ...

随机推荐

  1. TypeScript(4)接口

    介绍 TypeScript 的核心原则之一是对值所具有的结构进行类型检查.我们使用接口(Interfaces)来定义对象的类型.接口是对象的状态(属性)和行为(方法)的抽象(描述) 接口初探 声明接口 ...

  2. .Net Core 企业微信更新模版卡片消息

    1.搭建回调服务器 可参考:https://www.cnblogs.com/zspwf/p/16381643.html进行搭建 2.编写代码 2.1接口定义 应用可以发送模板卡片消息,发送之后可再通过 ...

  3. Mybatis+SpringBoot 项目All elements are null

    xml文件内容如下 查出来的集合长度是有的,但是会出现All elements are null 解决方案: 注意我的xml文件全部是这样的,并且我调用的sql返回值是  resultType=&qu ...

  4. CVPR 2017:See the Forest for the Trees: Joint Spatial and Temporal Recurrent Neural Networks for Video-based Person Re-identification

    [1] Z. Zhou, Y. Huang, W. Wang, L. Wang, T. Tan, Ieee, See the Forest for the Trees: Joint Spatial a ...

  5. 螣龙安科:威胁研究——与MAZE勒索软件事件相关的策略,技术和程序

    至少从2019年5月开始,恶意行为者就一直在积极部署MAZE勒索软件.勒索软件最初是通过垃圾邮件和漏洞利用工具包分发的,后来又转移到妥协后进行部署.根据我们在地下论坛中对涉嫌用户的观察以及整个Mand ...

  6. 如何学习Vim

    如果你是Linux用户,学习Vim会有很大的好处. 如果你是windows用户,个人建议还是使用vscode. 准备大约40min的学习时间,打开终端,输入下面命令开启自带教程 vimtutor 按操 ...

  7. 网格动物UVA1602

    题目大意 输入n,w,h(1<=n<=10,1<=w,h<=n).求能放在w*h网格里的不同的n连块的个数(平移,旋转,翻转算一种) 首先,方法上有两个,一是打表,dfs构造连 ...

  8. grafana监控配置

    一.配置开启smtp服务 1.编辑grafana配置文件grafana.ini [smtp] enabled = true host = smtp.163.com:25 user = 157xxxx3 ...

  9. Graph Theory の brief introduction

    一. 图的概念   1.定义   某类具体事物(顶点)和这些事物之间的联系(边),由顶点(vertex)和边(edge)组成, 顶点的集合V,边的集合E,图记为G = (V,E) 2.分类     1 ...

  10. YII学习总结1

    YII 安装(2.0 advanced) 以下借鉴别人的博文  http://www.jb51.net/article/54055.htm 今天终于搞明白怎么安装Yii2了.对于我这种小白来说真是费尽 ...