Bringing in vue TypeScript type definitions

In my main file entry.ts, I am trying to make reference to the typescript definitions for Vue.

The 'entry.ts' file is located at src/js/entry.ts, while the type definition for Vue can be found at src/js/lib/bower/vue/types/index.d.ts

/// <reference path='./lib/typings/jquery.d.ts' />
/// <reference path='./lib/bower/vue/types/index.d.ts' />

let data: Object = {},
    app: Vue = new Vue({
        data: data,
        el: '#app'
    });
console.log(app);

class Test {
    private id: string;
    constructor(id: string) {
        this.id = id;
    }
    public getElement(): any {
        return $(this.id);
    }
}
console.log(new Test('asdf').getElement());

Upon compilation of this file with TypeScript targeting es6 and es6 modules, the following output is generated:

[steventheevil@Steven-PC webdev-starter]$ tsc
src/js/entry.ts(5,10): error TS2304: Cannot find name 'Vue'.
src/js/entry.ts(5,20): error TS2304: Cannot find name 'Vue'.

To address this issue, I replaced the reference to the Vue type definition with an import statement as shown below:

/// <reference path='./lib/typings/jquery.d.ts' />
import * as Vue from './lib/bower/vue/types/index';

let data: Object = {},
    app: Vue = new Vue({
        data: data,
        el: '#app'
    });
console.log(app);

class Test {
    private id: string;
    constructor(id: string) {
        this.id = id;
    }
    public getElement(): any {
        return $(this.id);
    }
}
console.log(new Test('asdf').getElement());

The updated code compiles successfully without errors. Here is the output:

import * as Vue from '../../src/js/lib/bower/vue/types/index';
let data = {}, app = new Vue({
    data: data,
    el: '#app'
});
console.log(app);
class Test {
    constructor(id) {
        this.id = id;
    }
    getElement() {
        return $(this.id);
    }
}
console.log(new Test('asdf').getElement());

However, a problem arises where the import statement for the type definition remains, causing issues when using Rollup with Babel. How can I instruct the TypeScript compiler to eliminate imports for type definitions (.d.ts files)?

Below is the type definition for Vue (src/jslib/bower/vue/types/index.d.ts)

import * as V from "./vue";
import * as Options from "./options";
import * as Plugin from "./plugin";
import * as VNode from "./vnode";

// Namespace containing various available types
declare namespace Vue {
  // Types exported via this namespace
}

// Class declaration for Vue class
declare class Vue extends V.Vue {}

export = Vue;

Your assistance on this matter would be greatly appreciated. Thank you.

Answer №1

While delving into my TypeScript code, I stumbled upon a question regarding how to properly import VNode.

Should someone else encounter this issue, I hope my solution can be of assistance.

To access the necessary interfaces or classes, refer to the type definitions provided at vue/types.

For instance, when importing VNode:

// main.ts
import Vue from 'vue';
import { VNode } from 'vue/types';
import App from './App.vue';

new Vue({
  render: (h): VNode => h(App),
}).$mount('#app');

The version of Vue utilized in this scenario is 2.6.10.

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

Exporting ExpressJS from a TypeScript wrapper in NodeJS

I've developed a custom ExpressJS wrapper on a private npm repository and I'm looking to export both my library and ExpressJS itself. Here's an example: index.ts export { myExpress } from './my-express'; // my custom express wrap ...

Ways to modify an object similar to manipulating an array collection

Hey there, I've heard that iterating and editing objects (not arrays) is not the best practice. Is there a more efficient way to do it as easily as it can be done with an array of objects? Check out this link for reference new Vue({ el: '#app ...

Replace string values with an object array for a particular property

When faced with the task of replacing specific string values defined by the user in an array of objects, but only applying the rules to certain properties, there is a need for a more efficient approach. For instance, consider the array below: [ {Name: &apo ...

Testing Slack's Web API with Jest for mock purposes

Currently, I am working with two files. One file is where I set up a slack web API client to post a message and test it with a mocked value: main.ts: import { WebClient } from '@slack/web-api'; const slack = new WebClient(process.env.SLACK_API_K ...

Error encountered while rendering page in vue 2 due to undefined getters

Here is my global state and shared component where I set and define getters: export default { getters: { loading(state) { return state.loading; }, statusMessage(state) { return state.statusMessage; }, }, } The store expor ...

Convert information into an Observable

I have a scenario where I am fetching a list of items from an Observable in my Angular service. Each item contains an array of subjects, and for each subject, I need to make a separate API call to retrieve its details such as name, description, etc. Data ...

Vue displays error logs within Karma, however, the test failure is not being reflected in the Karma results

Currently, I am in the process of writing unit tests for Vue components within our new project. For testing, I am utilizing Karma with Mocha + Chai, and PhantomJS as the browser. The test command being used is cross-env BABEL_ENV=test karma start client/ ...

Give the Row ID as a parameter to a personalized component within MUI Datagrid Pro

I am currently exploring the idea of incorporating an intermediate state to row checkboxes based on the selection status of other checkboxes within a detailed panel. My approach involves crafting a custom checkbox component and implementing some logical ...

Node.js: Managing multiple occurrences of the same event name for testing purposes

When it comes to unit testing using mocha, I am looking for a way to set up an asynchronous queue for handling events. Previously, I used a once() Promise to wait for events like this: import EventEmitter from 'events' import { once } from ' ...

Having trouble retrieving image information within the Asp.net core controller

I am facing an issue trying to store image details in the database through Angular and ASP.NET Core. I am unable to retrieve the image data sent from Angular in the controller. Although I am able to obtain the image information using the [FromForm] attribu ...

Typescript error: the argument passed as type a cannot be assigned to the parameter of type b

In my programming interface, I have defined shapes as follows: type Shape = | Triangle | Rectangle; interface Triangle {...} interface Rectangle {...} function operateFunc(func: (shape: Shape) => void) {...} function testFunction() { const rectFun ...

Simulating MatSnackBar in Angular 8 using Jasmine Testing

My Angular 8 application utilizes the Angular Material MatSnackBar, and I am currently in the process of testing whether the open() method of this class is triggered. The invocation of open() happens within an NgRx store selector as shown below: ngOnInit( ...

Secondary Electron window not properly receiving IPC messages

While developing my TypeScript code that is linked to the HTML being executed by my application, I encountered an issue with creating a new window for my settings. It seems that the preloaded script is loaded onto the new window upon opening, but the windo ...

What are the steps to integrate vue-i18n with vue 3?

After setting up vue-i18n with Vue 3, I encountered an error; Here is my code snippet from the app.js file: import { createApp } from 'vue'; // ...... import en from './translate/en.json' import ar from './translate/ar.json' ...

What could be the reason behind the absence of this.props.onLayout in my React Native component?

In my React Native app, I have the below class implemented with Typescript: class Component1 extends React.Component<IntroProps, {}> { constructor(props){ super(props) console.log(props.onLayout) } ... } The documentation for the View ...

Implementing attribute binding in Vue 3

As a newcomer to Vue, I am currently exploring the concept of dynamically changing the source link for a video or image by passing in data through a prop. I have designed a component with a specific template structure and now aim to insert the source from ...

Finding the number of elements in a FirebaseListObservable involves accessing the `length` property

One of the tasks in my Angular 2 application involves retrieving data from a Firebase database and storing it in a FirebaseListObservable. I have a method called getStatus that is supposed to determine the number of elements in this FirebaseListObservable. ...

Checkbox from Ant Design placed inside a button

As a beginner in React, I am trying to transform an Ant Design Checkbox into a button while maintaining the checkbox functionality. Here is my initial code for the checkbox: import { Checkbox } from "antd"; <div className="modal-body d-flex flex- ...

Error message: Uncaught ReferenceError: require is not defined in Typescript and KnockoutJs combination

Presently, I am exploring the ts-ko demo provided by TypeScript. When I directly reference Ko like this: /// <reference path="./node_modules/@types/knockout/index.d.ts" /> No errors occur. However, if I add a reference in this manner: import * as ...

Integrating a public key and file into a constant data object within a Vue.js application

I am looking to create a 'data' structure that includes 'publicKey' and 'file' within it, and I want to separate them in the store as publicKey and file, respectively. How can I define this structure in save.vue as demonstrat ...