首页 > 解决方案 > 如何解决错误:类型上不存在属性

问题描述

我对 TypeScript 很陌生,我想在 typescript 文件中使用 OpenCV js 库。我现在设置strictfalse禁用类型检查,只是为了测试这是否可行,但所有 OpenCV 函数都返回错误:

Property Mat does not exist on type '(cv: any, ...args: any[]) => any'

我的代码示例:

import cv from "../OpenCV/opencv.js";

export class FaceRecognizer {
    
    private sourceMat: any;

    private grayMat: any

    private faceVector: any;

    private faceMat: any;

    private videoSource: HTMLVideoElement;
    
    private videoWidth: number;

    private videoHeight: number;


    constructor(videoSource: HTMLVideoElement) {
        this.sourceMat = new cv.Mat(this.videoHeight, this.videoWidth, cv.CV_8UC4);
        this.grayMat = new cv.Mat(this.videoHeight, this.videoWidth, cv.CV_8UC1);
        this.faceVector = new cv.RectVector();
        this.faceMat = new cv.Mat(0, 0, 0);
    }
}

编辑: 这是我定义类型的方式。

我的文件夹结构:

src
  tsconfig.json
  @types
      OpenCV
          main.d.ts
          package.json
  OpenCV
    opencv.js

包.json

{
    "name": "types",
    "version": "1.0.0",
    "typings": "main.d.ts"
}

tsconfig.js

...
"declaration": false,
"typeRoots": [
   "node_modules/@types",
   "./src/@types"
]

main.d.ts


declare namespace cv {
    export function Mat(...args: any): any;
    export function rotatedRectPoints(...args: any): any;
    export function rotatedRectBoundingRect(...args: any): any;
    export function rotatedRectBoundingRect2f(...args: any): any;
    export function exceptionFromPtr(...args: any): any;
    export function minEnclosingCircle(...args: any): any;

      export class MatVector {
      push_back: any;
      resize: any;
      size: any;
      get: any;
      set: any;
    }
    export class RectVector {
      push_back: any;
      resize: any;
      size: any;
      get: any;
      set: any;
    }
}

标签: typescripttypescript-typings

解决方案


推荐阅读