Error code TS2749 is indicating that the identifier 'XXX' is being interpreted as a value instead of a type. Perhaps you intended to use 'typeof XXX' instead

I've encountered a strange issue while running my npm run dev command in my project built with Nuxt.js, which includes Vue.js components. While launching the application, I'm encountering errors related to TypeScript like

TS2749: 'About' refers to a value, but is being used as a type here. Did you mean 'typeof About'?
, even though there are no issues shown when running npm run test.

The problematic line in my spec.ts file is as follows:

import { shallowMount, Wrapper } from "@vue/test-utils";    
import About from "@/pages/about.vue";

describe("About", () => {
  const wrapper: Wrapper<About> = shallowMount(About); // <-- Issue
  ...
}

Prior to setting typing, the type seems correct as displayed below:

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

Attempting the solution provided by using

const wrapper: Wrapper<typeof About> = shallowMount(About);
results in another TypeScript error, preventing the test from compiling. This new error states:
TS2344: Type 'ExtendedVue<Vue, unknown, unknown, { setLocation: any; }, unknown>' does not satisfy the constraint 'Vue'. Type 'VueConstructor<{ setLocation: any; } & Vue>' is missing several properties.

I am puzzled by the fact that the test command remains quiet, while TypeScript raises complaints about the tests themselves during local app execution. Despite all tests passing and successful app compilation, it appears to be an issue with typings in @vue/test-utils.

Answer №1

Wrapper<About> poses a challenge - this is a TypeScript type definition and About is not actually a TypeScript type... it represents a value (specifically, a Vue component definition object).

To address this issue, consider using

Wrapper<InstanceType<typeof About>>

In cases where you need to explicitly define types from external libraries (complete with typings) and bypass TypeScript's type inference system, it can feel like quite a bit of unnecessary effort. To achieve this, one must delve into and comprehend the details of the typings

...which, truth be told, I haven't fully mastered myself ...so the code snippet provided might just be incorrect :)

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

Struggling to implement a singleton service in Angular as per the documentation provided

I have implemented a service in Angular that I want to be a singleton. Following the guidelines provided in the official documentation, I have set the providedIn property to "root" as shown below: @Injectable({ providedIn: "root" }) export class SecuritySe ...

Contrasting characteristics of class members in JavaScript versus TypeScript

Typescript, a superset of Javascript, requires that Javascript code must function in Typescript. However, when attempting to create class members in a typescript file using the same approach as Javascript, an error is encountered. CODE :- script.ts (types ...

Why isn't my axios login get request functioning as expected?

I am currently working on a login-page for a school project. I am using vue.js and have been attempting to make a get-request using axios. However, I am facing an issue where I cannot even enter the .then() block as the last thing displayed is alert(TEST1) ...

The Vue.js app is functioning properly in the development environment, but it is encountering issues when trying to load the template in the production environment with Rails 5.2.0 and Webpacker

I am currently utilizing Rails 5.2.0 in conjunction with the Webpacker gem for deploying a Vue application. Within the show.html.erb file, the code is very straightforward: <div data-behavior="vue-app"><MyComponent></MyComponent></di ...

Convert all key types into arrays of that key type using a TypeScript utility type

My interface (type) is currently defined as: interface User { name: string, id: string, age: number, town: string } I have a function now that will search for Users based on specific fields. I prefer not to manually declare an additi ...

Extending State Interface in ReactJs and Typescript: A Step-by-Step Guide

I have the following: interface EditViewState<T> { entity?: T; } abstract class EditView<T, P, S> extends React.Component<P, EditViewState<T> & S> { constructor(props: P, ctx: any) { super(props, ctx); this. ...

How can a Vue.js toggle button dynamically change based on the combined state of two checkboxes?

My current project involves implementing a toggle feature that allows users to enable a day and then select between AM or PM using checkboxes. One issue I'm facing is how to de-toggle the button if a user unchecks both AM and PM options. Check out t ...

How can we leverage mapped types in TypeScript to eliminate properties and promisify methods?

Below is the provided code snippet: class A { x = 0; y = 0; visible = false; render() { return 1; } } type RemoveProperties<T> = { readonly [P in keyof T]: T[P] extends Function ? T[P] : never//; }; type JustMethodKe ...

Learn how to effortlessly integrate and utilize a bpmn file in a Vue component by leveraging the raw-loader

As a newcomer to bpmn-js, I am facing the challenge of importing and utilizing a .bpmn file within a vue.js component (BPMNViewer.vue). Despite my efforts in researching, I could only find recommendations to use the raw-loader. Consequently, I am currently ...

Problem with retrieving information from an array of objects - Vue.js / accessing API / utilizing axios / implementing proxy

Currently, my project involves using Vue.js to connect to an API and retrieve data using axios with a proxy. I am facing difficulty accessing the property of an object nested within multiple arrays. Below are more details: View Global details Property I& ...

How to open a print preview in a new tab using Angular 4

Currently, I am attempting to implement print functionality in Angular 4. My goal is to have the print preview automatically open in a new tab along with the print popup window. I'm struggling to find a way to pass data from the parent window to the c ...

Tips on retrieving the image URL from Aws s3 and saving it in mongodb

Can anyone help me with retrieving the URL location of an image from AWS s3 and storing it in MongoDB using mongoose? Currently, when I try to console log the req.file.location value, it shows up as undefined. I have also attempted to console log uploadFil ...

Discovering the right place to establish global data in Nuxt JS

Exploring the world of NuxtJS today, I found myself pondering the optimal method for setting and retrieving global data. For instance, how should a frequently used phone number be handled throughout a website? Would utilizing AsyncData be the most effecti ...

Sending Information to Routes - Error: No Routes Found to Match

I recently tried implementing a solution from an article () to pass an ID between modules in order to use it as a search filter. However, I encountered the "cannot match any routes" error and have been struggling for some time since I'm new to Angular ...

Having trouble accessing undefined properties? Facing issues with the latest Angular version?

Why am I encountering an error and what steps can be taken to resolve it? Currently using the latest version of Angular. ERROR TypeError: Cannot read properties of undefined (reading 'id') Here is the JSON data: { "settings": [ { ...

Creating a basic accordion feature in Vue by harnessing the power of radio buttons

Currently, I'm exploring the use of radio buttons to create a basic accordion in Vue. Everything is functioning correctly except for one issue - I can't seem to figure out how to display only one view at a time within the accordion. After expand ...

Ways to reset a dropdown selection when a switch is turned off

Hey there! I'm facing a bit of a challenge while working on my project using React, TypeScript, Ant Design, and Refine Framework. In my edit.tsx page component, I need to modify a record based on the value of catHasParent fetched from the database. Wh ...

The console is throwing an error because the store variable in the template is only filled after making an asynchronous API call

Within my component, I am showcasing various properties (exemplifying two below) of the selectedPlan store variable after receiving its value from the store: <div> {{ selectedPlan.periodTitle }} </div> <div> {{ selectedPlan.currency } ...

Guide to anticipating a boolean prop in my Vue test with mocha/chai?

Utilizing Vue CLI, I am facing an issue with a unit test that checks for a true/false condition. Here is the code snippet in question: describe('The thing', () => { it('must be available.', () => { const available = t ...

Encountering issues with implementing casl for role-based permission management

Here is my definition of ability using the casl library: ability.js import { AbilityBuilder } from '@casl/ability'; export default function() { AbilityBuilder.define(can => { switch(localStorage.getItem('role')) { case & ...