File declaring Vue3 Typescript types

I'm working with a Vue3 plugin in JavaScript, here's a snippet of the code:

const myPlugin = {
    install(Vue, config) {
           // do something
    }
export default myPlugin;

This code is located in index.js and will be called by app.use. For TypeScript projects, I need to create a type declaration. I discovered the vue/types/plugin interface in the vue package with definitions for the object and the install method. However, I'm unsure of how to apply this to my plugin. What should I include in the d.ts file so that the TypeScript compiler recognizes that myPlugin should be of type PluginObject?

Answer №1

Version 3.1.1 of Vue now allows the App.use() function to accept any type of argument, which appears to prevent enhancing its type declaration. In other words, attempting to augment the module does not overwrite the original App.use() function (potentially because the use of any supersedes MyPluginOptions):

declare module '@vue/runtime-core' {
  interface App {
    use(plugin: MyPlugin, ...options: MyPluginOptions[]): this;
  }
}

The only current solution seems to be directly modifying

node_modules/@vue/runtime-core/dist/runtime-core.d.ts
file by adding a generic to Plugin_2 and PluginInstallFunction to allow typing the options argument:

declare type Plugin_2<Option = any> = PluginInstallFunction<Option> & {
    install?: PluginInstallFunction<Option>;
} | {
    install: PluginInstallFunction<Option>;
};
export { Plugin_2 as Plugin }

declare type PluginInstallFunction<Option> = (app: App, ...options: Option[]) => any;

After that, replace the current App.use() with:

export declare interface App<HostElement = any> {
    use<Option>(plugin: Plugin_2<Option>, ...options: Option[]): this;
}

demo

I have also submitted a pull request to vue-next. If it is accepted, module augmentation will no longer be necessary, as the type of options will be inferred automatically.

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

When building websites, pages, or applications with React, how do you determine the best choice between JavaScript, TypeScript, or JavaScriptXML?

After completing my portfolio and an eCommerce website design using Figma, I started learning React and Tailwind with Vite. I'm comfortable with basic JavaScript but now I want to understand the differences between .js, .jsx, and .ts files when workin ...

Are multiple click events needed for identical buttons?

In my component, there is an HTML structure like this: <div id="catalogo" class="container"> <div class="row"> <div *ngFor="let artista of artistas" class="col-sm" style="mar ...

Problem with Typescript: The type '{ x;y }' is required to have a '[Symbol.iterator]()' method

Just starting out with Typescript and tackling the task of converting a React project from JavaScript to TypeScript. I've been diving into various posts for guidance, but I feel like I'm going in circles. Any assistance would be greatly appreci ...

Troubleshooting with Vuetify: Exploring methods to adjust the width of individual components within a banner

Seeking to incorporate a search form into a banner, utilizing vuetify. <v-banner app v-model="show" two-line transition="slide-y-transition"> <v-form> <v-text-field label="textfield" /> <v-select :items="select_1" label="sel ...

Adjusting the IntelliSense Functionality in Monaco Editor

Currently, I am testing out the CompletionItemProvider feature for my project. I have implemented two separate CompletionItemProviders. One is set to trigger when any alphabet letter is typed and the other triggers when the user enters a single quote chara ...

Is there a way to access a child component's method from the parent component using ref in Vue3?

I am encountering an issue while attempting to call the child method from the parent component in vue3 using the ref method. Unfortunately, an error is being thrown. Uncaught TypeError: addNewPaper.value?.savePaper is not a function Displayed below is m ...

Using the appropriate parameters is key: TS2345: The argument of type 'T' cannot be assigned to a parameter of type 'new() => any'

Struggling with Typescript 2, I am attempting to create a universal parser-method for database objects. Utilizing TypedJSON, I am encountering difficulties in correctly organizing the parameters. Here is part of my code: private static parseToInstance< ...

Combining arrays of objects sharing a common key yet varying in structure

Currently, I am facing a challenge while working on this problem using Typescript. It has been quite some time since I started working on it and I am hoping that the helpful community at StackOverflow could provide assistance :) The scenario involves two ...

What is the importance of using getters for functions involving Moment.js in vueJS and typescript?

weekOfMonth() calculates the current month and week within that month. <template> <h3>{{ weekOfMonth }}</h3> </template> <script lang="ts"> export default class HomeView extends Vue { const moment = require(& ...

Converting Getters into JSON

I am working with a sequelize model named User that has a getter field: public get isExternalUser(): boolean { return this.externalLogins.length > 0; } After fetching the User from the database, I noticed in the debugger that the isExternalUser prop ...

Adding missing values to two corresponding arrays in JavaScript

I am working with two arrays: xAxisData: ["0006", "0007", "0009", "0011", "0301"] yAxisData: [6.31412, 42.4245, 533.2234, 2345.5413, 3215.24] My goal is to standardize the length of the xAxis array to a maximum value, for example, DATAPOINT_LENGTH_STAND ...

"Exploring the World of Websockets Using Ionic 3 and Angular

How can I successfully implement a Websocket in Ionic 3 and Angular 4? I attempted to use the socket.io-client package, but when I try to connect the websocket using the following code: this.socket = io(this.urls.websocket, {transports: ['websocket& ...

Combining properties from one array with another in typescript: A step-by-step guide

My goal is to iterate through an array and add specific properties to another array. Here is the initial array: const data = [ { "id":"001", "name":"John Doe", "city":"New York&quo ...

Failure to update Vue localforage

Why is the value of this.emails not getting properly updated outside of localforage when using localforage? The console does not display any errors. localforage.getItem("emails").then(function (value) { this.emails = value ...

The property functions normally outside the promise, but is undefined when within the promise context

I am currently working on filtering an array based on another array of different objects but with the same key field. Although I have made some progress, I keep encountering errors that I am unable to resolve. @Component({ selector: 'equipment&ap ...

After transitioning to TypeScript, the CRA app is encountering issues loading CSS files compiled by SASS

My React application was originally created using create-react-app and I had been successfully loading scss files with the node-sass package. However, after deciding to switch to TypeScript following the steps outlined in , my css files are no longer being ...

Is it possible to transfer items from one list within a component to another list inside a different component with Vue draggable?

I am having trouble implementing Vue Draggable to move items between lists within a component setup. The issue I'm facing is that the items can be moved within a list, but not from one list to another. The structure involves a Board component that ho ...

Exploring Next JS: Effectively altering SVG attributes and incorporating new elements

I have integrated SVGR to load an SVG as a component in my latest Next.js 13 application: import CvSvg from './../../public/image.svg' export default function Home() { return ( <div className="flex flex-col min-h-screen" ...

Next.js typescript tutorial on controlling values with increment and decrement buttons

I'm just starting to learn typescript and I'm looking to implement increment and decrement buttons in a next.js project that's using typescript. export default function Home() { return ( <div className={styles.container}> ...

Constructor polymorphism in TypeScript allows for the creation of multiple constructor signatures

Consider this straightforward hierarchy of classes : class Vehicle {} class Car extends Vehicle { wheels: Number constructor(wheels: Number) { super() this.wheels = wheels } } I am looking to store a constructor type that ext ...