TypeScript 属性“道具"不存在 [英] TypeScript Property 'props' does not exist

查看:61
本文介绍了TypeScript 属性“道具"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 .tsx 文件

import React, { Component } from 'react';导出类 SidebarItem 扩展组件 {构造函数(道具){超级(道具);}使成为 () {返回(<li>{this.props.children}</li>);}}

然而,TypeScript 会抛出这个错误:错误 TS2339:SidebarItem"类型上不存在属性props".

解决方案

解决方案是安装 React Types 定义

yarn add -DE @types/react

更多详情来自 typescript docs 和来自类型回购

顺便说一句,我必须重新启动 vscode 才能正确启动 linting.

I have this .tsx file

import React, { Component } from 'react';

export class SidebarItem extends Component {
    constructor (props) {
        super(props);
    }

    render () {
        return (<li>{this.props.children}</li>);
    }
}

However, TypeScript throws this error: error TS2339: Property 'props' does not exist on type 'SidebarItem'.

解决方案

The solution is to install the React Types defintions

yarn add -DE @types/react

More details from the typescript docs and from the types repo

On a side note I had to restart vscode for the linting to kick in properly.

这篇关于TypeScript 属性“道具"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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