You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular开发自定义语音识别库:无法使用第三方类型@types/dom-speech-recognition

解决SpeechRecognition类型找不到的问题
  • 先确认项目实际使用的tsconfig文件:如果你的项目主配置是根目录的tsconfig.json,仅修改tsconfig.lib.json不会生效,需要在主配置的compilerOptions.types中添加dom-speech-recognition,或者让主配置继承tsconfig.lib.json。同时检查include字段要覆盖你使用这些类型的代码文件,比如:

    "include": ["src/**/*"]
    
  • 手动引入类型:不需要额外导出,但可以在使用SpeechRecognition的文件顶部添加:

    import '@types/dom-speech-recognition';
    

    或者在项目的全局类型声明文件(比如src/global.d.ts)中添加:

    /// <reference types="dom-speech-recognition" />
    
  • 检查版本兼容性:确认@types/dom-speech-recognition版本和你的TypeScript版本匹配,尝试更新类型包:

    npm update @types/dom-speech-recognition --save
    
  • 全局声明兜底:如果以上方法都无效,可以在使用文件中临时声明全局类型:

    declare global {
      interface Window {
        SpeechRecognition: typeof SpeechRecognition;
        webkitSpeechRecognition: typeof SpeechRecognition;
      }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 04:59:53