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 ...
随机推荐
- iNeuOS工业互联网操作系统,在线报表(Excel)开发工具
目 录 1. 概述... 2 2. 视频介绍... 2 3. 应用过程... 2 1. 概述 iNeuOS工业互联网操作系统在线报表(Excel)工具的开 ...
- VmWare安装Centos8注意事项
VmWare安装Centos8注意事项 1.需选择稍后安装操作系统 2.选择操作系统版本 3.修改虚拟机配置 4.配置完成点击开启虚拟机(注意要将鼠标放在屏幕中央,点击一下后才能使用上下键进行选择) ...
- [二进制漏洞]栈(Stack)溢出漏洞 Linux篇
目录 [二进制漏洞]栈(Stack)溢出漏洞 Linux篇 前言 堆栈 堆栈(Stack)概念 堆栈数据存储方式 函数调用 函数调用C语言代码 函数调用过程GDB调试 函数Call返回原理 函数栈帧 ...
- SAP Html viewer
1 *&---------------------------------------------------------------------* 2 *& Report RSDEM ...
- 程序员必备,一款让你提高工作效率N倍的神器uTools
下载地址:https://www.aliyundrive.com/s/f7PU7QxdxEz uTools 是什么? uTools = your tools(你的工具集) uTools 是一个极简.插 ...
- Linux文件的删除和软硬链接
文件的构成 由元数据(metadata)和数据(data)两部分组成 硬盘分区上一块空间存该分区上文件的元数据,一块空间存这些文件的数据 因为元数据和数据分离存放,所以需要通过指针地址来进行关联 元数 ...
- vue在Docker上运行
Dockerfile # 设置基础镜像 FROM nginx:latest # 定义作者 MAINTAINER test # 将dist文件中的内容复制到 /etc/nginx/html/ 这个目录下 ...
- Apache:dbutils 开源JDBC工具类库
commons-dbutils jar:下载 package com.jdbc.tools; import org.apache.commons.dbutils.QueryRunner; import ...
- Java 17 中的模式匹配与和类型
Java 17 中的模式匹配与和类型 从 Spring Security 获取用户谈起 使用 Spring Security做用户校验和权限控制时,常常使用和线程绑定的容器来获取当前登录用户. // ...
- XJSON 是如何实现四则运算的?
前言 在上一篇中介绍了 xjson 的功能特性以及使用查询语法快速方便的获取 JSON 中的值. 同时这次也更新了一个版本,主要是两个升级: 对转义字符的支持. 性能优化,大约提升了30%️. 转义字 ...