简体   繁体   English

TS2339:移植到Typescript时,类型'typeof React'上不存在属性'PropTypes'

[英]TS2339: Property 'PropTypes' does not exist on type 'typeof React' when porting to Typescript

I'm currently converting a javascript/react project to a typescript/react/redux project. 我目前正在将一个javascript / react项目转换为typescript / react / redux项目。

I'm currently having a problem with this file: 我目前遇到此文件的问题:

import React from 'react';
import GoldenLayout from 'golden-layout';
import {Provider} from 'react-redux';
import Default from '../modules/m-Default/Default';

interface GoldenLayoutWrapperProps {}

interface GoldenLayoutWrapperState {}

class GoldenLayoutWrapper extends React.Component <GoldenLayoutWrapperProps, GoldenLayoutWrapperState> {

    public layout;
    static contextTypes = {
        store: React.PropTypes.object.isRequired
    };

    private wrapComponent(Component, store) {
        class Wrapped extends React.Component {
            render () {
                return (
                    <Provider store={store}>
                        <Component {...this.props}/>
                    </Provider>
                )
            }
        }
        return Wrapped;
    }

    componentDidMount() {
        var layout = new GoldenLayout(this.layoutConfig, this.layout);

        layout.registerComponent('Default', this.wrapComponent(Default, this.context.store));
    }

    render() {
        return (
            <div className="golden-layout-wrapper" ref={input => this.layout = input}/>
        )
    }

    private layoutConfig = {}
}

   export default GoldenLayoutWrapper;

When I attempt to compile, I get the following error: 当我尝试编译时,我收到以下错误:

ERROR in [at-loader] ./src/main/GoldenLayoutWrapper.tsx:18:22 
TS2339: Property 'PropTypes' does not exist on type 'typeof React'.

After some searcing, some people suggested to give contextTypes a static modifier, which I attempted, but the problem still persists. 在一些搜索之后,有些人建议给contextTypes一个静态修饰符,我尝试过,但问题仍然存在。

What could be the problem? 可能是什么问题呢?

As you are using React v16.0.0 , You need to make use of prop-types as it is treated a separate library since React v15.5 当您使用React v16.0.0时,您需要使用prop-types因为它自React v15.5起被视为一个单独的库

You can install it via npm as a project dependency 您可以通过npm作为项目依赖项安装它

npm install --save prop-types

And then inside your component where you want to use it : 然后在您想要使用它的组件内:

import PropTypes from 'prop-types'; // ES6

Instead of React.PropTypes you can directly use PropTypes 取而代之的React.PropTypes你可以直接使用PropTypes

You can read more about prop-types Here 您可以在此处阅读有关道具类型的更多信息

Check my answer here for full details: Cannot read property 'string' of undefined | 在这里查看我的答案以获取完整的详细信息: 无法读取未定义的属性'string' React.PropTypes | React.PropTypes | LayoutPropTypes.js LayoutPropTypes.js

In short, as of React v15.5 React.PropTypes is deprecated. 简而言之,从React v15.5 React.PropTypes ,不推荐使用React.PropTypes You'll need to install the prop-types package from npm. 您需要从npm安装prop-types包。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React typescript,类型“typeof TodoFilterItem”上不存在属性“propTypes”。 TS2339 - React typescript, Property 'propTypes' does not exist on type 'typeof TodoFilterItem'. TS2339 TS2339:类型“ {}”上不存在属性“焦点”。 使用React打字稿 - TS2339: Property 'focus' does not exist on type '{}'. with React typescript Typescript TS2339:在Typescript + React + Redux应用程序中,类型&#39;IntrinsicAttributes&#39;上不存在属性&#39;queryType&#39;吗? - Typescript TS2339: Property 'queryType' does not exist on type 'IntrinsicAttributes' in Typescript + React + Redux app? 反应,Typescript,钩子 - 从钩子中解构数据,TS2339:类型上不存在属性“数据” - React, Typescript, Hooks - destructuring data from hook, TS2339: Property 'data' does not exist on type 类型“X[]”上不存在属性“customProperty”。 打字稿反应。 TS2339 - Property 'customProperty' does not exist on type 'X[]'. Typescript React. TS2339 children.toArray() - TS2339:“ReactChild | 类型”上不存在属性“键” 反应片段 | ReactPortal'... - 反应 Typescript - children.toArray() - TS2339: Property 'key' does not exist on type 'ReactChild | ReactFragment | ReactPortal'... - React Typescript 类型“HTMLInputElement”上不存在属性“目标”。 TypeScript 反应。 TS2339 - Property 'target' does not exist on type 'HTMLInputElement'. TypeScript React. TS2339 TS2339(TS)属性“状态”在类型上不存在 - TS2339 (TS) Property 'state' does not exist on type 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339 Typescript 2.6-&gt;使用模块扩展来扩展外部库的类型定义-&gt; TS2339:类型”上不存在属性” - Typescript 2.6 -> Extend type definition for external library with module augmentation -> TS2339: Property '' does not exist on type ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM