如何从react-intl和typescript一起使用injectIntl​​?

时间:2019-03-07 11:29:09

标签: reactjs typescript react-intl

我正在尝试将injectIntl中的react-intl与这样的打字稿一起使用(这与我在其他问题的答案中发现的一致):

import { injectIntl, InjectedIntlProps } from "react-intl";

interface Props {
    certificate?: Certificate;
    onCancelClick(event: any): void;
    onDeleteClick(event: any): void;
    onSubmit(certificate: CertificateWritable): Promise<any>;
}

class CertificateForm extends Component<Props & InjectedIntlProps> {
// ... removed for simplicity
}

export default injectIntl<Props>(CertificateForm);

但是我遇到以下错误:

Error:(174, 34) TS2345: Argument of type 'typeof CertificateForm' is not assignable to parameter of type 'ComponentType<Props & InjectedIntlProps>'.
  Type 'typeof CertificateForm' is not assignable to type 'ComponentClass<Props & InjectedIntlProps, any>'.
    Types of property 'propTypes' are incompatible.
      Type '{ certificate: Requireable<InferProps<{ [x: string]: Requireable<string> | Validator<ReactText> | Requireable<boolean>; }>>; onCancelClick: Validator<(...args: any[]) => any>; onDeleteClick: Requireable<...>; onSubmit: Validator<...>; }' is not assignable to type 'WeakValidationMap<Props & InjectedIntlProps>'.
        Types of property 'certificate' are incompatible.
          Type 'Requireable<InferProps<{ [x: string]: Requireable<string> | Validator<ReactText> | Requireable<boolean>; }>>' is not assignable to type 'Validator<Certificate | null | undefined>'.
            Types of property '[nominalTypeHack]' are incompatible.
              Type 'InferProps<{ [x: string]: Requireable<string> | Validator<ReactText> | Requireable<boolean>; }> | null | undefined' is not assignable to type 'Certificate | null | undefined'.
                Type 'InferProps<{ [x: string]: Requireable<string> | Validator<ReactText> | Requireable<boolean>; }>' is not assignable to type 'Certificate | null | undefined'.
                  Type 'InferProps<{ [x: string]: Requireable<string> | Validator<ReactText> | Requireable<boolean>; }>' is missing the following properties from type 'Certificate': id, caCertificate, caCertificateKey, domain, and 2 more.

我不明白我在做什么,以及如何使其与TS一起使用?

这里没有建议的解决方案:React-intl, use api with Typescript

我已经安装了@types/react-intl 2.3.17版。

更新: 这是我从certificate中删除Props时收到的错误消息:

Error:(175, 27) TS2345: Argument of type 'typeof CertificateForm' is not assignable to parameter of type 'ComponentType<Props & InjectedIntlProps>'.
  Type 'typeof CertificateForm' is not assignable to type 'ComponentClass<Props & InjectedIntlProps, any>'.
    Types of property 'propTypes' are incompatible.
      Type '{ certificate: Requireable<InferProps<{ [x: string]: Requireable<string> | Validator<ReactText> | Requireable<boolean>; }>>; onCancelClick: Validator<(...args: any[]) => any>; onDeleteClick: Requireable<...>; onSubmit: Validator<...>; }' is not assignable to type 'WeakValidationMap<Props & InjectedIntlProps>'.
        Types of property 'onDeleteClick' are incompatible.
          Type 'Requireable<(...args: any[]) => any>' is not assignable to type 'Validator<(event: any) => void>'.
            Types of property '[nominalTypeHack]' are incompatible.
              Type '((...args: any[]) => any) | null | undefined' is not assignable to type '((event: any) => void) | undefined'.
                Type 'null' is not assignable to type '((event: any) => void) | undefined'.

TypeScript版本为3.3.3333

1 个答案:

答案 0 :(得分:0)

似乎,问题不在于react-intlcertificate属性类型不正确的问题。看一下Certificate类型。

只需从道具中移除certificate,然后检查问题是否消失。