【发布时间】: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