Typescript definition for react-native-tesseract-ocr

677 views Asked by At

Trying to figure out the correct typescript definition for react-native-tesseract-ocr (https://github.com/jonathanpalma/react-native-tesseract-ocr)

This is the quick example of it in action:

import RNTesseractOcr from 'react-native-tesseract-ocr';

RNTesseractOcr.startOcr(response.path, "LANG_ENGLISH")
  .then((result) => {
        this.setState({ ocrResult: result });
        console.log("OCR Result: ", result);
  })      
  .catch((err) => {
        console.log("OCR Error: ", err);
  })

What I've tried is (doesn't work).

declare module "react-native-tesseract-ocr" {
    export default class RNTesseractOcr {
        startOcr(image: String, lang: String) :Promise<any>;
    }
}

Looking to put this into a declarations.d.ts file, not sure if that changes the syntax.

1

There are 1 answers

0
Ryan Cavanaugh On BEST ANSWER

You've declared that the default export is a class constructor, in other words that the intended usage is

const c = new RNTesseractOcr();
c.startOcr(/*....*/);

Instead, try this:

declare module "react-native-tesseract-ocr" {
    namespace RNTesseractOcr {
        function startOcr(image: String, lang: String) :Promise<any>;
    }
    export default RNTesseractOcr;
}