1

I want to have intellisense for opencv.js inside VS Code when I call it from a JavaScript file, but when I use import "path/opencv.js" or import cv from "path/opencv.js", I can't have intellisense for cv.

In the OpenCV documentation, there are following description and a example:

Once opencv.js is ready, you can access OpenCV objects and functions through cv object. The promise-typed cv object should be unwrap with await operator. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await .

imgElement.onload = await function() {
  cv = (cv instanceof Promise) ? await cv : cv;
  let mat = cv.imread(imgElement);`
}

but the intellisense doesn't even show a promise-typed cv.

It is worth mentioning that my environment is browser.

Edit:

When I use import cv from "path/opencv.js" VS code detect 'cv' type as:

(alias) (local var) cv: (cv: any, ...args: any[]) => any

The way opencv.js exports its module is very complicated, it is not the modern way of exporting module:

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(function () {
      return (root.cv = factory());
    });
  } else if (typeof module === 'object' && module.exports) {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like environments that support module.exports,
    // like Node.
    module.exports = factory();
  } else {
    // Browser globals
    root.cv = factory();
  }
}(this, function () {
  var cv = function(cv) {
  cv = cv || {};
  var Module = cv;
  ...
7
  • Have you enabled checkJS in config, does your jsconfig.json includes the folder with opencv.js? Commented Sep 2 at 8:08
  • I test it, but it don't solve the problem. Also when I enable it (checkJS), import cv from "path/opencv.js" raises error. Commented Sep 2 at 10:05
  • ok so I think its probably packed as old amd module (using old require func), try something like import pkg from 'path/opencv.js'; const cv = pkg.exportedObjectName;. It works for me when using old amd modules. Commented Sep 2 at 10:26
  • It doesn't work for me, I think module is exported in a dynamic way and VS Code can't detect it. I think it is better to give intellisense up for it! Commented Sep 2 at 12:36
  • It may be better to build opencv.js myself. Commented Sep 2 at 12:41

0

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.