【问题标题】:How to use injectIntl from react-intl and typescript together?如何一起使用 react-intl 和 typescript 中的 injectIntl​​?
【发布时间】:2023-10-12 19:16:02
【问题描述】:

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

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。

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

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

【问题讨论】:

  • @cwtuan 感谢您的提示。
  • 快速更新:我最终使用 *.d.ts 文件覆盖了这些类型。在我看来,确定类型的定义是错误的。
  • 你是如何使用 *.d.ts 文件覆盖类型的,我在 Typescript 3.5.x 上遇到了同样的问题
  • @JunQ 你可以这样做:medium.com/@tomsoir/…

标签: reactjs typescript react-intl


【解决方案1】:

看来,问题不在于react-intlcertificate 道具类型不正确的问题。看看Certificate 类型。

只是,从道具中删除certificate并检查问题是否消失。

【讨论】:

  • 这没有帮助 - 如果我从 Props 中删除证书,然后打字稿抱怨下一个道具,我已将错误消息添加到我的问题中。
  • 请添加TS版本
  • 已添加到问题中
  • 不确定,但试试:``` Component ```
  • @HonzaKalfus 我建议你打个电话。
最近更新 更多