1

I'm currently using React 16.4.1 and Typescript 2.9.2. I'm trying to utilise the reaptcha library:

https://github.com/sarneeh/reaptcha

Which I import like this:

import * as Reaptcha from 'reaptcha';

As there are no type definitions shipped, building currently gives me this (expected) error:

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

Despite this error, the code "works", but I've been trying for a long time to build just enough of a definition file to suppress the error. There seems to be a lot of different ways to do this based on how the library is export and I don't seem to have hit the right combination. For this definition:

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

Rather than solving the issue, this just presents a different error.

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

And this latter error, I'm quite stuck on. Any assistance getting past this appreciated.

The minimal case of where the component is used is:

  public render() {
    const Fragment = React.Fragment;
    return (
      <Fragment>
        <Reaptcha
          ref={(e: any) => (this.captcha = e)}
          sitekey="MY KEY"
          onVerify={this.onVerify}
        />
      </Fragment>
    );
  }
2
  • To understand why what you tried didn't work, I'd need to see the code that actually uses Reaptcha after importing it. Commented Jul 30, 2018 at 12:00
  • Thanks Matt. I've edited the question to include this code. Commented Jul 30, 2018 at 21:54

2 Answers 2

1

The Reaptcha readme shows the import as:

import Reaptcha from 'reaptcha';

I tried this style of import with your original declaration and TypeScript was happy but the import didn't work at runtime. When I enabled the --esModuleInterop TypeScript option, then everything worked. Try using this option if you aren't already.

Sign up to request clarification or add additional context in comments.

1 Comment

Moving to the import * as... seems to be the widely documented approach, such as in every React doc. Your alternate approach here totally fixes it, thanks a heap for the help.
1

You could just use:

declare module "reaptcha";

1 Comment

Thanks Matt. I'd really like to work in a direction for a more complete definition - I wrote types for all the props and functions but pulled them back to demonstrate this minimal case. I'll accept this is we don't get there though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.