My project involves creating a custom voice recognition library, and I have decided to utilize 3rd party package types called @types/dom-speech-recognition
. However, upon attempting to integrate these types into my service, the compiler raised errors indicating that it was unable to locate these external types:
Error: ../angular-voice-recognition/src/lib/voice-recognition.service.ts:9:23 - error TS2304: Cannot find name 'SpeechRecognition'.
9 private speechAPI!: SpeechRecognition;
~~~~~~~~~~~~~~~~~
Error: ../angular-voice-recognition/src/lib/voice-recognition.service.ts:40:28 - error TS2304: Cannot find name 'webkitSpeechRecognition'.
40 this.speechAPI = new webkitSpeechRecognition();
~~~~~~~~~~~~~~~~~~~~~~~
To address this issue, I installed the necessary types using
npm install @types/dom-speech-recognition --save
tsconfig.lib.json:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": [
"dom-speech-recognition"
]
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
voice-recognition.service.ts:
import { Injectable } from '@angular/core';
import {VoiceParams} from "./model/voice-params.model";
import {Observable} from "rxjs";
import {VoiceResult} from "./model/voice-result";
import {VoiceEvent} from "./model/voice-event";
@Injectable()
export class VoiceRecognitionService {
private speechAPI!: SpeechRecognition;
...
private initialize(params: VoiceParams) {
if ('webkitSpeechRecognition' in window) {
...
}
}
}
These are the key areas where modifications were made following the installation of @types/dom-speech-recognition
in my library. Could there be a need to export or import the type elsewhere as well?