Issue with using third-party package types in Angular library creation

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?

Answer №1

The error message that is currently being displayed suggests that the definitions for SpeechRecognition and webkitSpeechRecognition are not accessible, despite being installed.

To resolve this issue, consider modifying tsconfig.ts:

{"compilerOptions": {"types": []}}

This adjustment will allow TypeScript to locate and include the necessary types from the installed packages.

Additionally, remember to include the appropriate import statement in voice-recognition.service.ts

import { SpeechRecognition } from 'dom-speech-recognition';

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I update the version of v8 in NodeJS to ensure I have the latest Javascript enhancements?

Is it possible to upgrade the JavaScript engine version from 3.x to 6.x without updating the Node.js version? The current Node version is 0.12.x. ...

Issue with data propagation in Angular 2 RC5 when using ngStyle in parent component

Here is a basic Angular 2 application setup I am working with: import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: ` <h1>Test</h1> <test-component [Height] = "30 ...

Suggestions for npm package methods missing in VS Code

Hey, I'm new to node.js and npm. I recently downloaded an npm package, but I'm facing an issue where VS code is not providing me with method name recommendations for the downloaded package. This feature would be very helpful for me in the future. ...

Is there a method to limit exports within a package in Node.js?

Is there a specific method or structure in NodeJS that allows for the sharing of functions within modules of a package exclusively, without permitting them to be shared with other packages? For example, if Package A consists of file1.js, file2.js, and ind ...

Leveraging React Hooks to display a dynamic pie chart by fetching and mapping data from an API

I have a task where I need to fetch data from an API that returns an object containing two numbers and a list structured like this... {2, 1 , []} The project I'm currently working on utilizes 'use-global-hook' for managing state in Redux. T ...

How to pull data from an HTTP source without relying on promises

Here is a simplified version of the service code I am using: Load(Channel: any) { // let URL = Globals.AppSiteRoot + Channel.URL return this.LoadData(URL) } Load_Default() { let URL = Globals.AppSiteRoot + "di ...

Error message: The object is not visible due to the removal of .shading in THREE.MeshPhongMaterial by three-mtl-loader

Yesterday I posted a question on StackOverflow about an issue with my code (Uncaught TypeError: THREE.MTLLoader is not a constructor 2.0). Initially, I thought I had solved the problem but now new questions have surfaced: Even though I have installed &apo ...

How to Remove onFocus Warning in React TypeScript with Clear Input Type="number" and Start without a Default Value

Is there a way to either clear an HTML input field of a previous set number when onFocus is triggered or start with an empty field? When salary: null is set in the constructor, a warning appears on page load: Warning: The value prop on input should not ...

What is the best way to globally incorporate tether or any other feature in my Meteor 1.3 TypeScript project?

I've been working hard to get my ng2-prototype up and running in a meteor1.3 environment. Previously, I was using webpack to build the prototype and utilized a provide plugin to make jQuery and Tether available during module building: plugins: [ ne ...

What is the best way to vertically align items within a <div> using CSS in a React application?

The layout of the page looks like this: https://i.sstatic.net/NGCNP.png I am trying to center the Redbox (containing '1') within the "PlayerOneDiv". Similarly, I want to center the yellow box within "PlayerTwoDiv". return ( ...

Unlocking the Potential of Vue Class Components: Exploring Advanced Customization Options

Currently, I am working on a project using Vue 2 with Typescript. However, I am facing an issue where I cannot add options to the component. <script lang="ts"> import { Component, Vue } from "vue-property-decorator"; import HelloW ...

When delving into an object to filter it in Angular 11, results may vary as sometimes it functions correctly while other times

Currently, I am working on implementing a friend logic within my codebase. For instance, two users should be able to become friends with each other. User 1 sends a friend request to User 2 and once accepted, User 2 is notified that someone has added them a ...

HTML: Mark the chosen hyperlink or tag

In my HTML page, I am looking to keep the link selected when it is clicked on. Here is the initial HTML code: <table class="main-dev"> <tr> <td> <a class='titleForm' style="cursor:pointer"> ...

The Angular component router-outlet is not recognized as a known element

I have encountered an error message: 'router-outlet' is not a known element: 1. If 'router-outlet' is an Angular component, then verify that it is part of this module. 2. If 'router-outlet' is a Web Component then add ...

Is it possible for an Angular App to function as an authenticated user for a real-time database?

Just a question, no code included. I want to restrict access to reading from RTDB only to authenticated users. However, I don't want every user to have to sign up individually. Instead, I would like to have one login tied to the angular app that auto ...

Dispatch a websocket communication from a synchronous function and retrieve the information within the function itself

I am currently working on an Angular project and need guidance on the most effective approach to implement the following. The requirement is: To retrieve an image from the cache if available, otherwise fetch it from a web socket server. I have managed ...

Utilizing Leaflet-geotiff in an Angular 6 Environment

I'm currently facing an issue where I am unable to display any .tif image on my map using the leaflet-geotiff plugin. I downloaded a file from gis-lab.info (you can download it from this link) and attempted to add it to my map, but I keep encountering ...

The pathway specified is not recognized as a valid package: C:Program Files odejs pm

I recently started using node and npm and I'm attempting to upgrade npm from version 1.4.3 to the latest one. My current process involves navigating to C:\Program Files (x86)\nodejs with cmd.exe and then running the installation without -g: ...

What is the best approach for managing optional object input parameters while also verifying the presence and accuracy of that specific property?

What is the best approach to handling a situation where a method has optional object member properties for the options object, but you still want to ensure the presence of that property with a default value in the resulting instance? Is creating a separate ...

Attempting to map an object, however it is showing an error stating that the property 'title' does not exist on type 'never'

While attempting to retrieve data from the Bloomberg API, I encountered an issue when trying to extract the title from the response object. The error message received was: Property 'title' does not exist on type 'never'. Below is the co ...