You need to enable JavaScript to run this app. TypeScript使用OpenCV.js出现属性不存在类型报错的解决方法
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript使用OpenCV.js出现属性不存在类型报错的解决方法

TypeScript引入OpenCV.js报Property Mat does not exist on type问题解决

问题现象

作为TypeScript开发新手,为了快速验证OpenCV.js功能,已将strict配置关闭,仍在调用OpenCV API时抛出类型错误:
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
  • OpenCV类型目录下的package.json配置
{
    "name": "types",
    "version": "1.0.0",
    "typings": "main.d.ts"
}
  • tsconfig相关配置片段
"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;
    }
}

报错核心原因

  • 当前导入的opencv.js是UMD格式的OpenCV官方构建产物,TS默认将其模块类型识别为初始化回调函数,而非带Mat、RectVector等静态属性的对象,因此读取cv.Mat时会触发属性不存在的类型报错。
  • 现有main.d.ts中声明的declare namespace cv是游离的全局命名空间,没有和导入的../OpenCV/opencv.js模块做类型绑定,声明内容和实际导入的模块完全没有关联。
  • 自定义类型目录下的package.json命名不符合TS类型包的扫描规则,结合错误的全局命名空间声明,typeRoots配置并没有实际生效,TS根本没有加载写好的类型定义。

可落地的解决方法

方案1:最快跑通功能的兜底写法(不需要写自定义d.ts)

如果当前只是想先验证功能、完全绕开类型检查,不需要维护复杂的自定义类型,直接在导入位置加类型断言即可:

// 把导入的模块直接断言为any类型,彻底消除所有类型相关报错
import cvWasmModule from "../OpenCV/opencv.js";
const cv = cvWasmModule as any;

这种写法不需要修改tsconfig、不需要调整目录结构,修改完导入代码后所有cv.xxx的调用都不会再触发类型检查报错。

注意:原示例代码的构造函数存在逻辑bug,没有把传入的videoSource参数赋值给类属性,也没有给videoWidth、videoHeight赋值,就算类型问题解决,运行时也会因为传入undefined创建Mat对象失败。需要补充对应赋值逻辑:

constructor(videoSource: HTMLVideoElement) {
    // 补充缺失的属性赋值
    this.videoSource = videoSource;
    this.videoWidth = videoSource.videoWidth;
    this.videoHeight = videoSource.videoHeight;
    // 等OpenCV.js异步初始化完成后再调用API
    cv.onRuntimeInitialized = () => {
        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);
    }
}

方案2:修正自定义类型声明(适合后续要逐步补全类型的场景)

如果还是想保留自定义类型声明文件,需要把原来的全局命名空间声明改成和导入路径绑定的模块声明,修改main.d.ts内容为:

// 绑定实际导入的opencv.js路径
declare module "../OpenCV/opencv.js" {
    // 定义cv对象的结构
    export const cv: {
        Mat: new (...args: any[]) => any;
        RectVector: new (...args: any[]) => any;
        CV_8UC4: number;
        CV_8UC1: number;
        // 兜底所有未声明的属性,避免临时缺定义报错
        [key: string]: any;
    }
    // 匹配默认导出规则
    export default cv;
}

同时在tsconfig中补充"allowJs": true配置,允许TS加载js格式的OpenCV构建文件,模块解析就能正常匹配上写的类型定义。

方案3:调整类型目录的扫描规则

如果要让typeRoots配置生效,需要把@types/OpenCV目录下的package.json的name字段改成符合TS类型包规范的名称,同时把原来的全局命名空间声明改成符合模块导出的格式,否则TS在扫描类型目录时会因为包名不规范跳过写的类型定义。

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.13 04:54:04