Priority of Typescript TypeRoots

After extending a class from an npm package with additional type definitions, I noticed that my custom definitions are taking lower priority than the ones coming from node_modules. Is there a way to adjust the TypeScript definition priority using the typeRoots property?

For instance, I extended the VueRouter class from the vue-router package in the following manner:

export class Router extends VueRouter {
  pushWithCheck (location: RawLocation): Promise<Route | void> {
    return this.push(location)
      .catch((error: Error) => {
        if (error && error.name !== 'NavigationDuplicated') {
          throw error
        } 
      })
  }
}

I then created a file at src/@types/vue.d.ts with the contents:

import Vue from 'vue'
import { Router } from '../router'

export {}

declare module 'vue/types/vue' {
  interface Vue {
    $router: Router
  }
}

Next, I updated the tsconfig.json file and added

typeRoots: ["./src/@types", "./node_modules/@types"]
(I also tried in reverse order).

In VS Code, this is what I observe:

https://i.sstatic.net/hYObh.png

Answer №1

In order for the changes to take effect, the interface should be exported within vue.d.ts:

declare module 'vue/types/vue' {
  export interface Vue {
    $router: Router
  }
}

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

Fetching locales asynchronously in nuxt.js using i18n and axios: A step-by-step guide

I am facing an issue with getting the location asynchronously. Whenever I try to implement my code, it results in a "Maximum call stack size exceeded" error. How can I resolve this issue? Previously, I attempted to retrieve the location by using the axios ...

The default value for prop objects did not set properly

I find myself in a rather peculiar situation: In my HomeComponent.vue <template> <div class="home"> <img alt="Vue logo" src="../assets/logo.png" /> <HelloWorld :msg="msg" @update=" ...

What is the role of the "prepare" function in AWS CDK constructs?

TL;DR: What is the role and purpose of the prepare(): void method in AWS CDK's Construct class? When and how should it be utilized or avoided? The information provided about prepare() states: prepare() function is called after child constructs have ...

Turn off Vuetify's v-touch functionality within a specific nested element

Currently, I am utilizing Vuetify and seeking a way to trigger certain code when the user swipes either left or right. Within my project, there is a container structured as follows: <div v-touch="{ left: () => performActionA(), right ...

TypeScript in Angular causing lodash tree shaking failure

I am currently working on a large project that involves TypeScript. Various attempts have been made to optimize the use of lodash in my project. Before making any conclusions, I believe it is important to review the outcomes of my efforts. The command I ...

Is it possible to utilize a Render Function in a .vue Component file?

I am interested in dynamically setting the HTML tags for components. Consider the following example: components: { test: { props: ['tag', 'attributes'], render(createElement) { return createElement(this.tag || ...

What is the reason behind capitalizing Angular CLI class file imports?

After creating a basic class in Angular using the CLI starter, I encountered an issue when trying to use the imported class. Instead of functioning as expected, it returned an empty object. Through troubleshooting, I discovered that simply changing the fil ...

Switching Boolean values is just like toggling class lists in Vue 2

This information is stored in my data: diceRoll: [{value: null, locked: false,},{value: null, locked: false},{value: null, locked: false}, {value: null, locked: false},{value: null, locked: false}], My goal is to toggle the 'locked' attribute o ...

How do I manage 'for' loops in TypeScript while using the 'import * as' syntax?

When working with TypeScript, I encountered an issue while trying to import and iterate over all modules from a file. The compiler throws an error at build time. Can anyone help me figure out the correct settings or syntax to resolve this? import * as depe ...

Chart of commitments and potential outcomes

I am in the early stages of learning about promises and I am struggling to understand how to write code correctly. Here is an overview of what the program should do: Retrieve a list of item types (obtained through a promise) Loop through each item type to ...

Issue with getStaticProps in Next.js component not functioning as expected

I have a component that I imported and used on a page, but I'm encountering the error - TypeError: Cannot read property 'labels' of undefined. The issue seems to be with how I pass the data and options to ChartCard because they are underline ...

Tips for modifying TypeScript class types based on a parent class object property?

As part of a project, I have created two classes: ParentClass childrenClass which extends the ParentClass class Displayed below is the code for my ParentClass: interface ConfSetup<T extends boolean> { enabled: T; permissions: bigint[]; locati ...

Assigning a value to a variable from a method in Vue: a step-by-step guide

I'm having trouble assigning values from a method to variables in HTML. Here's what I have in my code: <b-card-text>X position {{xpos}}</b-card-text> <b-card-text>Y position {{ypos}}</b-card-text> I would like to assign v ...

Refresh a Google chart without having to reload the entire page

I currently have a button that allows me to refresh the data on my page in case there is new data available through an API. Although this button successfully refreshes my datatable, it does not redraw the Google charts I have integrated into my project usi ...

What is the best way to incorporate an additional feature into vue.js?

Is it possible to add multiple get posts in Vue.js? I have successfully implemented a single post function, but I'm unsure how to incorporate multiple post functions. Here is my current code snippet: <script> new Vue({ el: '#app' ...

Encountering an issue post-upgrade with Angular 7 project

Currently, I am facing an issue with upgrading a project from Angular 6 to version 7. Despite following multiple online tutorials and successfully completing the upgrade process, I encountered an error when running the 'ng serve' command: ERROR ...

A guide on updating from the old version to the new version of Permission Handler

I have an old permission handler code that needs to be updated to work with the latest version of pubspec.yaml. Can anyone provide guidance on how to modify the code below for compatibility with the latest version? if (Platform.isAndroid) { Permissio ...

Whenever attempting to update an individual element within an associative array in Vue 3, all values within the array end up being updated simultaneously

In my Vue3 code snippet, I have the following: data: function() { return { testData:[], } }, mounted() { var testObj = { name: 'aniket', lastname: 'mahadik' ...

Uncovering the Mystery of Undefined Dom Ref Values in Vue 3 Templaterefs

I am currently experimenting with the beta version of Vue 3 and encountering an issue while trying to access a ref in the setup function. Here is my component: JS(Vue): const Child = createComponent({ setup () { let tabs = ref() console.log(t ...

Using the js-cookie library in a TypeScript-based project: A guide

Looking to incorporate the js-cookie library into my TypeScript project. After installing the library and typings with the command npm install js-cookie @types/js-cookie --save-dev in the location containing node_modules and package.json, my package.json ...