Encountering TS2344 error when referring to the Component Ref in Vue.js during typing operations

I received a component reference on my FormCheckbox component from a patternlib. When I tried to incorporate the component into my own TestComp component, I encountered this TypeScript error that left me puzzled:

TS2344: Type '{ name: string; mixins: any[]; inheritAttrs: boolean; props: { id: { type: StringConstructor; required: boolean; }; label: { type: StringConstructor; default: string; }; checked: { ...; }; disabled: { ...; }; }; emits: string[]; computed: { ...; }; }' does not satisfy the constraint 'new (...args: any) => any'.   Type '{ name: string; mixins: any[]; inheritAttrs: boolean; props: { id: { type: StringConstructor; required: boolean; }; label: { type: StringConstructor; default: string; }; checked: { ...; }; disabled: { ...; }; }; emits: string[]; computed: { ...; }; }' provides no match for the signature 'new (...args: any): any'.

FormCheckbox ...

<script>
import spacingMixin from "../mixins/spacing.js";

export default {
  name: "FormCheckbox",
  mixins: [
    spacingMixin
  ],
  inheritAttrs: false,
  props: {
    id: {
      type: String,
      required: true
    },
    label: {
      type: String,
      default: ""
    },
    checked: {
      type: Boolean
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  emits: ["update:checked"],
  computed: {
    fieldAttributes() {
      const { ...attrs } = this.$attrs;
      delete attrs['class']
      return attrs;
    }
  }
};
</script>

...

TestComp ...

<template>
<FormCheckbox id="SelectAll" ref="selectAllCheckbox"/>
</template>
export default defineComponent({
    name: "TestComp",
    components: {
        FormCheckbox,
    },
    setup() {
      const checkbox: Ref<InstanceType<typeof FormCheckbox> | null> = ref(null);
      return {checkbox}
    },
});
</script>

...

I attempted to apply an InstanceType to my ref as I have plans to utilize the $el property in the future.

Answer №1

Here is a problem you may encounter:

export default /* FormCheckbox */ { ... }
import FormCheckbox from ...
export default /* TestComp */ defineComponent({ ... })

However, the issue arises when defineComponent requires your imported component to be a Component returned by defineComponent. It will not allow you to use an object.

To resolve this, you must update FormCheckbox to also utilize defineComponent.


If you are unable to access the code directly, consider using one of the following solutions:

// shims-vue.d.ts

/// <reference types="vite/client" />
/// <reference types="vue/ref-macros" />

// Mocks all files ending in `.vue` showing them as plain Vue instances
declare module '*.vue' {
  import type { DefineComponent } from 'vue'

  const component: DefineComponent<{}, {}, any>
  export default component
}

Alternatively, you can try:

import type { DefineComponent } from 'vue'
let myRef = Ref<DefineComponent<{}, {}, any>>(/* implied `undefined` */)

Or simply utilize this.$refs.selectAllCheckbox, eliminating the need for defining it altogether if I recall correctly.

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

Having trouble getting Safari to load preflight CORS authentication requests (XHR) in a Vue.js application hosted on Apache?

I've been spending hours researching and trying to debug, but I'm not having any luck. This workflow/request works fine on Chrome, but Safari and Firefox both fail at the OPTIONS preflight request. Safari shows two errors: (!) Failed to load res ...

The object[] | object[] type does not have a call signature for the methods 'find()' and 'foreach()'

Here are two array variables with the following structure: export interface IShop { name: string, id: number, type: string, } export interface IHotel { name: string, id: number, rooms: number, } The TypeScript code is as shown below ...

What's the deal with TypeScript tsconfig allowing .json imports, but not allowing them in built .js files?

By including the line "resolveJsonModule": true in my project's .tsconfig file, I have successfully implemented direct importing of data from .json files. The project functions properly, even when using nodemon. However, upon building the project and ...

What is the best way to implement transition effects while toggling between light mode and dark in tailwind 2.0?

Currently, I am working on a small project utilizing tailwindCSS and have decided to incorporate a dark mode feature. To achieve this, I created a button that toggles the dark class in the html tag. However, upon testing the functionality, I noticed that t ...

What steps can be taken to eliminate repeat categories and prevent the accumulation of endless iterations?

Analysis I designed an interface that takes two type parameters, with the second parameter being optional and defaulting to void. Additionally, I created a utility type called CommandReturnType which employs conditional typing to ensure that void is not r ...

using the newquestion variable later in the function

In the Vue.js code snippet below, there is a variable named newQuestion that is passed to the function getAnswer like this: this.getAnswer(newQuestion). Further down in the methods section, particularly at this line getAnswer: _.debounce(, I would like to ...

Encountering the issue of receiving "undefined" when utilizing the http .get() method for the first time in Angular 2

When working in Angular, I am attempting to extract data from an endpoint. Below is the service code: export class VideosService { result; constructor(private http: Http, public router: Router) { } getVideos() { this.http.get('http://local ...

CORS request unsuccessful on Vue.js and Express server

I have a vue application running on an apache server within a virtual environment. Express is being run with nodemon. When attempting to log in, I am encountering the following error message: Cannot read property 'status' of undefined xhr.js:160 ...

Trouble with embedding video in the background using Next.js and Tailwind CSS

This is the code snippet for app/page.tsx: export default function Home() { return ( <> <main className='min-h-screen'> <video muted loop autoPlay className="fixed -top ...

"Vue component inputs fail to update when used in the same instance

I have reused the same component in both the navigation menu and on the people's page. My problem is that when the SearchComponent inputs in the navigation update their values, the SearchComponent inputs on the people's page do not update, and v ...

Iterating variables in Vue.js is reminiscent of the process in AngularJS

When working on my application using vue.js, I am interested in finding the last repeated element within a v-for directive. I understand that in angularjs, there is a similar concept using the $last loop variable in its ngRepeat directive: <div ng-repe ...

Failure on the expect statement when comparing numbers in Jest..."The Jest magic number comparison is

I am currently conducting a test to verify that the magic number of a Buffer is in zip format. This involves extracting the first 4 bytes of the buffer into a string and comparing it with the magic number for zip, which is PK. const zipMagicNumber: str ...

Troubles with Katex/ngx-markdown Display in Angular 16

In my Angular 16 application, I utilize the ngx-markdown library alongside Katex and other dependencies. A challenging situation arises when the backend (an LLM) responds with markdown text that conflicts with Katex delimiters during rendering. I attempte ...

SonarLint versus SonarTS: A Comparison of Code Quality Tools

I'm feeling pretty lost when it comes to understanding the difference between SonarLint and SonarTS. I've been using SonarLint in Visual Studio, but now my client wants me to switch to the SonarTS plugin. SonarLint is for analyzing overall pr ...

Enhance your Vuetify components by incorporating unique custom attributes

Is there a way to include custom attributes in Vuetify tags? I am interested in adding some Schema.org attributes to my website as well. Here is the template structure I currently have: <template> <v-container> <v-layout row wr ...

What is the best way to send multiple parameters to @Directives or @Components in Angular using TypeScript?

I am facing some confusion after creating @Directive as SelectableDirective. Specifically, I am unclear on how to pass multiple values to the custom directive. Despite my extensive search efforts, I have been unable to find a suitable solution using Angula ...

The Nuxt image keeps disappearing every time I navigate to a new page

Whenever I have an image displayed on my Nuxt page and then navigate away from it, the image breaks and I can't figure out why. This is what my code looks like: <img :src="baseUrl + 'storage/'+ front.featured_image" alt="p ...

Creating PDFs with HTML content using Vue.js

Can PDF files be generated from HTML content, similar to printing to PDF? I have complex HTML with technical drawings (absolute positions, etc) that I need to convert the entire Vue file template into a PDF. ...

Getter and setter methods in Angular Typescript are returning undefined values

I am facing a challenge in my Angular project where I need a property within a class to return specific fields in an object. Although I have implemented this successfully in .Net before, I am encountering an issue with getting an "Undefined" value returned ...

Utilizing React with Typescript: A guide to working with Context

I have a super easy app snippet like import React, { createContext } from 'react'; import { render } from 'react-dom'; import './style.css'; interface IAppContext { name: string; age: number; country: string; } const A ...