typescript - 如何解决错误:类型上不存在属性
问题描述
我对 TypeScript 很陌生,我想在 typescript 文件中使用 OpenCV js 库。我现在设置strict
为false
禁用类型检查,只是为了测试这是否可行,但所有 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;
}
}
解决方案
推荐阅读
- iis - MSdeploy.exe 如何在 setparam 值中使用特殊字符
- html - 单击 div 时,Bootstrap 弹出框一直保持不变
- python - 如何从帕累托前沿获得膝点解决方案
- angular - 如何在Angular中检查网页上特定组件的输出
- powerbi - 填充表格行的 Power BI 度量
- discord - TypeError:channel.send 不是函数(已弃用?)
- powerbi - 如何在 Power BI 中复制没有触摸板的触摸板缩放?
- mysql - 根据最后三个订单计算每位客户的退货率
- android - Android使用recyclerview在卡片内分组相同的视图类型
- ios - 使 iOS SwiftUI TextField 拒绝错误的数字输入