React组件的Typescript定义文件疑难解答


我目前使用的是React 16.4.1和Typescript 2.9.2。我正在尝试使用reaptcha库:

https://github.com/sarneeh/reaptcha

我像这样导入:

import * as Reaptcha from 'reaptcha';

由于没有提供类型定义,构建当前给出了这个(预期的)错误:

TS7016: Could not find a declaration file for module 'reaptcha'. '...' implicitly has an 'any' type.

尽管有这个错误,代码仍然“工作”,但我已经尝试了很长一段时间来构建一个足够的定义文件来抑制这个错误。根据库的导出方式,似乎有很多不同的方法可以做到这一点,而我似乎没有找到正确的组合。对于此定义:

declare module "reaptcha" {
  import * as React from "react";
  export default class Reaptcha extends React.Component<any, any> {
  }
}

这并没有解决问题,而是呈现了一个不同的错误。

TS2604: JSX element type 'Reaptcha' does not have any construct or call signatures.

在后一个错误中,我很难理解。任何帮助我们度过这个难关的人都很感谢。

使用组件的最小情况是:

  public render() {
    const Fragment = React.Fragment;
    return (
      <Fragment>
        <Reaptcha
          ref={(e: any) => (this.captcha = e)}
          sitekey="MY KEY"
          onVerify={this.onVerify}
        />
      </Fragment>
    );
  }

转载请注明出处:http://www.gxainuo.com/article/20230526/1336246.html