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有一个类型注释的外部存储库。