React报错之Parameter 'props' implicitly has an 'any' type
正文从这开始~
总览
当我们没有为函数组件或者类组件的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
,并访问对象上的属性,而不会得到任何类型检查错误。
泛型
如果你有一个类组件,可以使用泛型来为其props
和state
声明类型。
// 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>
);
}
我们明确地为组件的props
和state
提供了类型,这就解决了这个错误。
如果你不想明确地为你的组件的props
和state
提供类型,你可以使用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;
我们在输入props
和state
对象时使用了any
类型,这有效地关闭了类型检查。
现在你将能够访问this.props
和this.state
对象上的任何属性而不会得到类型检查错误。
重新安装
如果错误没有解决,尝试删除你的node_modules
和package-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的更多相关文章
- React报错之Parameter 'event' implicitly has an 'any' type
正文从这开始~ 总览 当我们不在事件处理函数中为事件声明类型时,会产生"Parameter 'event' implicitly has an 'any' type"错误.为了解决 ...
- React报错之Property 'value' does not exist on type EventTarget
正文从这开始~ 总览 当event参数的类型不正确时,会产生"Property 'value' does not exist on type EventTarget"错误.为了解决 ...
- React报错之Property 'X' does not exist on type 'HTMLElement'
正文从这开始~ 总览 在React中,当我们试图访问类型为HTMLElement 的元素上不存在的属性时,就会发生Property 'X' does not exist on type 'HTMLEl ...
- React报错之Property 'value' does not exist on type 'HTMLElement'
正文从这开始~ 总览 当我们试图访问一个类型为HTMLElement的元素上的value属性时,会产生"Property 'value' does not exist on type 'HT ...
- react 报错的堆栈处理
react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...
- notification 报错the method build() is undefined for the type Notificatin.Builder
notification 报错the method build() is undefined for the type Notificatin.Builder 这事api版本号太低导致的 Notifi ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
- react报错this.setState is not a function
当报错这个的时候就要看函数是否在行内绑定this,或者在constructor中绑定this. 我这里犯的错误的是虽然我在constructor中绑定了this,但是语法写的不正确. 错误示范: co ...
- MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
问题背景: 在Dao中使用MyBatis进行查询操作,参数是传的一个List:studentNameList,但是在执行查询的时候报错,具体日志如下: com.chenzhou.base.mybati ...
随机推荐
- [自制操作系统] 第04回 完善MBR
目录 一.前景回顾 二.改写MBR 三.实现loader 一.前景回顾 在之前我们说到,MBR的作用便是加载操作系统内核到指定位置.而MBR需要通过读取硬盘来获得操作系统内核.在上一回我们已经讲解了硬 ...
- 10.5 详解Android Studio项目结构
Android项目的结构很复杂,并不像HTML项目,最简单的直接一个HTML文件就行了,相信学完上一节的同学就明白,哪怕是一个HelloWorld这样一个项目的文件可能都有几十个,所以我们需要搞清楚, ...
- NC14380 位数差
NC14380 位数差 题目 题目描述 给一个数组 \({a}\) ,定义 \(h(a,b)\) 为在十进制下 \(a + b\) 与 \(a\) 的位数差,求 \(\displaystyle\sum ...
- wsl2安装百度apollo及其基本配置
一. wsl2的开启 首先 WSL2 gui 需要Windows 11 Build 22000版本以上才支持 利用管理员权限打开PowerShell 执行 dism.exe /online /enab ...
- 【python】M3U8下载器脚本
[python]M3U8下载器脚本 脚本目标: 1. 输入M3U8文件的链接,得到视频 2.使用异步操作,这样可以快很多,不加锁,因为懒得写,而且影响不大 已知条件: 1.m3u8文件其实就是一个记录 ...
- DNS 系列(二):DNS 记录及工作方式,你了解吗?
在上一篇<DNS 系列(一):为什么更新了 DNS 记录不生效?>中,我们主要讲解了 DNS 和 DNS 传播,知道了网络通信主要通过 IP 地址来进行,而域名系统(DNS)则是保证用户在 ...
- linux 文件名乱码的文件无法删除
1.通过ls -i命令获得文件的节点号 2.通过节点号删除 find -inum 节点号 -delete 这样就可以删除文件名乱码的文件
- 聊聊 C++ 中几类特殊成员函数
一:背景 在 C# 中要说类默认给我们定义的特殊成员函数,莫过于 构造函数,但在 C++ 中这样的特殊函数高达 6 种,有必要整合一下聊一聊. 二:特殊成员函数 1. 默认构造函数 和 C# 一样,很 ...
- 零基础学Java(8)数组
数组 数组存储相同类型值的序列. 声明数组 数组是一种数据结构,用来存储同一类型值的集合.通过一个整型下标(index,或称索引)可以访问数组中的每一个值.例如,如果a是一个整型数组,a[i]就是数组 ...
- Re:用webpack从零开始的vue-cli搭建'生活'
有了vue-cli的帮助,我们创建vue的项目非常的方便,使用vue create然后选择些需要的配置项就能自动帮我们创建配置好的webpack项目脚手架了,实在是'居家旅行'必备良药.这次借着学习w ...