正文从这开始~

总览

当我们没有为函数组件或者类组件的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. 获得MySQL数据库存放位置

    更新记录 2022年6月13日 发布. 2022年6月11日 开始. 通过查看MySQL与存储目录相关的参数 show variables like '%dir%'; 通过查询后datadir参数的值 ...

  2. 【Spring】事务的执行原理(二)

    前置知识 事务的执行步骤如下: 获取事务管理器 创建事务 执行目标方法 捕捉异常,如果出现异常进行回滚 提交事务 public abstract class TransactionAspectSupp ...

  3. BPC 脚本

    // //税款计算(现金流) // //2018年5月11日修改,去除之前的送数逻辑,新增BADI计算相关科目 // *SELECT(%010804ZH%, "ID", " ...

  4. 在VMware Workstation 16上安装Windows7虚拟机以及VMware tools安装失败解决方法

    安装VMware Workstation 16 搜素"VMware Workstation下载" 下载 VMware Workstation Pro 下载Windows7系统镜像 ...

  5. VirtualBox虚拟机安装Ubuntu系统后,增加内存空间和处理器核心数

    对于Linux爱好者而言,初次使用虚拟机时,一般都会使用默认的设置,例如硬盘空间.内存空间等等. 而往往在熟悉之后,安装了某些必要的软件,或者熟悉了实际的开发场景后,却发现原本给虚拟机分配的物理资源是 ...

  6. 超 Nice 的表格响应式布局小技巧

    今天,遇到了一个很有意思的问题,一名群友问我,仅仅使用 CSS,能否实现这样一种响应式的布局效果: 简单解析一下效果: 在屏幕视口较为宽时,表现为一个整体 Table 的样式 而当屏幕视口宽度较小时, ...

  7. 【摸鱼神器】UI库秒变低代码工具——表单篇(一)设计

    前面说了列表的低代码化的方法,本篇介绍一下表单的低代码化. 内容摘要 需求分析. 定义 interface. 定义表单控件的 props. 定义 json 文件. 基于 el-form 封装,实现依赖 ...

  8. NC200211 装备合成

    NC200211 装备合成 题目 题目描述 牛牛有 \({x}\) 件材料 \({a}\) 和 \({y}\) 件材料 \({b}\) ,用 \({2}\) 件材料 \({a}\) 和 \({3}\) ...

  9. Python: 列表、数组及迭代器切片的区别及联系

    1. 对列表和数组进行切片 1.1 切片索引 众所周知,Python中的列表和numpy数组都支持用begin: end语法来表示[begin, end)区间的的切片索引: import numpy ...

  10. 树莓派实战:微信机器人(itchat实现)

    背景 楼主有一台树莓派4B开发板(8G内存版),是目前的顶配机型.这一年来的业余时间,除了写Java.架构方面的文章,也陆续折腾了不少树莓派上的好玩小项目,在此新开一个树莓派实战的文章系列,分享给粉丝 ...