Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用injectIntl()包装React组件后,属性类型检查丢失_Reactjs_Typescript_React Intl - Fatal编程技术网

Reactjs 使用injectIntl()包装React组件后,属性类型检查丢失

Reactjs 使用injectIntl()包装React组件后,属性类型检查丢失,reactjs,typescript,react-intl,Reactjs,Typescript,React Intl,假设我有一个组件卡,如下所示: import * as React from 'react'; interface Props { title: string; } export class Card extends React.Component<Props, null> { public render() { return ( <div> {this.props.title}

假设我有一个组件
,如下所示:

import * as React from 'react';

interface Props {
    title: string;
}

export class Card extends React.Component<Props, null> {
    public render() {
        return (
            <div>
                {this.props.title}
            </div>
        );
    }
}
Property 'title' is missing in type '{}'.
我需要使用
react intl
中的
injectIntl()
包装此组件,以便插入
intl
上下文。我是这样做的:

import * as React from 'react';
import {injectIntl} from 'react-intl';

interface Props {
    title: string;
}

class Component extends React.Component<Props, null> {
    // ..
}

export const Card = injectIntl(Component);
对于typescript编译器来说,这似乎很好。也没有运行时错误。我预计编译器会抛出如下错误:

import * as React from 'react';

interface Props {
    title: string;
}

export class Card extends React.Component<Props, null> {
    public render() {
        return (
            <div>
                {this.props.title}
            </div>
        );
    }
}
Property 'title' is missing in type '{}'.
这种行为从何而来?为什么是这样?如何解决这个问题


提前谢谢你

TL;DR:您需要安装软件包


react intl
包不使用类型注释,正如您在源代码中看到的那样。
injectIntl
返回的组件与
卡的组件完全不同。来自
@Types/react intl
包的类型添加缺少的类型注释。

谢谢@Tomáš!你的回答为我指明了正确的方向。实际上我没有安装类型,所以typescript在编译时不知道要验证什么。也许你可以用这个提示更新你的问题@罗宾回答更新。抢手货我忘了Typescript有一个类型注释的外部存储库。