Error in declaring type even after including .d.ts file

I have encountered a situation where the npm package update-immutable does not come with a built-in typescript definition or a definition in @types. To resolve this issue, I created a type definition file within my project. As a result, VS Code is now able to detect the correct types for this package.

However, when the noImplicitAny option is set to true, Angular's compiler fails to recognize this type definition file. This leads to an error message stating

Could not find a declaration file for module 'update-immutable'. '.../node_modules/update-immutable/dist/update.js' implicitly has an 'any' type.

How can I make sure that Angular's compiler locates the .d.ts type definition file that I have created for the npm package being used?

After enabling the traceResolution feature, I discovered that placing the file update-immutable.d.ts directly under the project root allows it to be detected. However, I prefer to maintain organization and avoid cluttering the project root with type files if possible.

Project structure:

projectroot
+ src
| + app
| | + states
| |   + core
| |     + core.reducer.ts (where update-immutable is imported)
| + types
|   + update-immutable
|     + index.d.ts (custom type file)
+ node_modules
| + update-immutable
|   + ...
+ tsconfig.json

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "downlevelIteration": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "jszip": [
        "node_modules/jszip/dist/jszip.min.js"
      ]
    },
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

index.d.ts type definition:

declare module 'update-immutable' {
  export default function update<T>(view: T, upd: object): T;
}

Answer №1

I encountered a similar issue in a project not using Angular.

In order to resolve the problem, I had to specify both paths and typeRoots in my tsconfig.json file. Here's an example that helped me from a repository by andyperlitch/typeroots-fail:

// tsconfig.json
{
  "baseUrl": ".",
  "typeRoots": ["./node_modules/@types", "./custom-types"],
  "paths": {
    "*": ["*", "./custom-types/*"]
  }
}

For more insights, check out this discussion on GitHub.

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

Error: Reference to an undeclared variable cannot be accessed. TypeScript, Cordova, iOS platforms

Can anyone offer some advice on what might be the issue? I'm encountering an error while building my Ionic app on the IOS platform, but everything is running smoothly on Android. ReferenceError: Cannot access uninitialized variable. service.ts:31 O ...

Guide to employing Axios types in project declaration files

When working on my project's type declaration file, I encountered a dilemma with using Axios types as part of my own types. The issue lies in the fact that all declarations for Axios are exported from their official repository. I specifically need to ...

The Angular 2 routerLink doesn't update the component after the initial click, even though the URL changes in the browser

When using Angular 2, I encountered an issue where clicking a routerLink in the App module successfully navigates to a parameterised route (e.g. /events/2) and loads the correct component (event-details) on the initial click. However, subsequent clicks on ...

The toggle-button's group property does not function properly when paired with ngIf

The group property is not functioning as expected when using ngIf in the toggle-group. Here is the code snippet: <mat-button-toggle-group *ngIf="query.noOfQuestions == 05" (change)="toggleChangeQuestion($event)" name="selectQue ...

Ionic, Angular - A component with two out of three inputs displaying undefined values

I am facing an issue with the @input() in my code. Two out of three inputs have undefined values when I try to use them, although they can be displayed using interpolation in the template file. .ts : export class NoteCanvasPageComponent { @Input() note ...

Angular 2: Utilizing Http Subscribe Method with "this" Context Pointer

Question: http.request('js/app/config/config.json').subscribe(data => { this.url = data.json().url; }); It seems that "this" is pointing to Subscriber instead of the parent class. I was under the impression that the fat- ...

By default, Angular 2's routerLinkActive is set to active for dynamic routes

Within my ngOnInit function, I am dynamically obtaining two links and URLs. <li><a [routerLink]="welcomeUrl" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Getting Started</a></li> <li><a [routerLin ...

Using Typescript with Styled-Components and Styled-System: Unable to find a matching overload for this function call

Encountering the infamous "No overload matches this call" error when using a combination of Typescript, Styled-Components, and Styled-System. I've come across solutions that suggest passing a generic type/interface to the styled component, like the o ...

No routes found that match. URL Segment 'calendar' does not correspond to any routes available

Currently interning, I've been tasked with building my own Angular 5 web application. However, I've hit a roadblock with an issue that's had me stuck for hours now. Every time I try to access the calendar, it gives me an error saying it can& ...

Accessing the 8100 ionic application from any port or device other than the original one is not possible

I have successfully developed an Ionic application that runs smoothly on the browser using port 8100. However, I am encountering issues with logging in from simulator or any other port. I have implemented httpClient to fetch headers. Can someone please pro ...

Showcasing a single object in an IONIC/Angular application using the ngIF directive

I am in need of assistance as I have encountered an issue. Essentially, I am fetching an object from an external API (Strapi) using the GET method, but when attempting to display it on Views with ngIF, it fails to show up. Below is the code snippet: word ...

Internet Explorer is throwing unexpected routing errors in Angular 2

I have a spring-boot/angular 2 application that is working perfectly fine on Chrome, Safari, Opera, and Edge. However, when accessed through Internet Explorer, the app directly routes to the PageNotFound component. I have tried adding shims for IE in the i ...

Running end-to-end test in Angular application based on specified configuration

Currently, I am immersed in a project centered around Angular7 where e2e tests are crafted using protractor. I have recently implemented a new feature in the project that I aim to control through a functional switch. When the switch is turned ON, the feat ...

Unit testing Firebase function with Jest for mocking

Currently, I am in the process of developing unit tests and facing challenges with mocking Firebase functions while specifying the return type upon calling them. The code snippet below illustrates what I intend to mock (account.service.ts) and provides ins ...

Set the component variable to hold the output of an asynchronous method within a service

As I work on developing an application, I aim to keep my component code concise and devoid of unnecessary clutter. To achieve this, I plan to offload complex logic into a service which will then be injected into the component. Suppose my component includes ...

Executing Angular2 application within Docker container

I have created a Docker file to run my ng2 app in a container: FROM ubuntu:latest RUN apt-get update #Install curl & git RUN apt-get -qq -y install curl RUN apt-get install -yqq git #Download and install nodejs-6 RUN curl -sL https://deb.nodesourc ...

Filtering server-side components in Next.js to create a customized list

Having been accustomed to the previous architecture of Next.js, I embarked on a new project where I am exploring the use of server and client components in the latest architecture. Specifically, I have a page dedicated to displaying race results in a tabl ...

Trigger a new Action upon successful completion of another action

I am a newcomer to the world of Angular and Redux. I have a common question for which I can't seem to find the right answer or maybe just need a helpful tip. I am utilizing ngrx in my application and I need to update some basic user data and then refr ...

Issue with Vue plugin syntax causing component not to load

I'm facing an issue with a Vue plugin that I have. The code for the plugin is as follows: import _Vue from "vue"; import particles from "./Particles.vue"; const VueParticles = (Vue: typeof _Vue, options: unknown) => { _Vue. ...

What is the recommended data type to assign to the `CardElement` when using the `@stripe/react-stripe-js` package in TypeScript?

I'm struggling to determine the correct type to use for this import: import { CardElement } from '@stripe/react-stripe-js'; I have successfully used the types Stripe, StripeElements, and CreateTokenCardData for the stripe and elements props ...