Error in VS Code related to Vue Array Prop JSDoc TypeScript: The properties of type 'ArrayConstructor' are not found in type 'MyCustomType[]'

After reading the article "Why I no longer use TypeScript with React and why you might want to switch too", I decided to work on a Vue CLI project using ES6 without TypeScript. Instead, I enabled type checking in Visual Studio Code by utilizing JSDoc / @type comments. To keep my JavaScript code separate from Vue code, I used the following method:

<script src="./MyComponent.js"></script>

When attempting to set the type for an Array prop in a component like this within MyComponent.js:

 /**
 * @typedef {object} MyCustomType
 * @property {number} myNumberField
 */

export default {
  props: {
    /** @type {Array<MyCustomType>} */
    resultCards: Array
  }
};

I encountered a problem in Visual Studio Code with red underlines stating:

Type 'ArrayConstructor' is missing the following properties 
from type 'MyCustomType[]': pop, push, concat, join, and 25 more.ts(2740)

If anyone knows of a solution to this issue, I would appreciate it. I prefer to stick with JavaScript over converting the project to TypeScript just for type checking. If no solution can be found, I may have to forego setting types for Vue props members.

For further information, check out Microsoft/TypeScript: JSDoc support in JavaScript

Edit: While TypeScript might provide a solution for Vue, I am focused on using plain JavaScript with JSDoc and leveraging type checks from VSCode.

Source:

To address this issue, consider casting the Object as a function that returns the interface, like so:

props: {   testProp: Object as () => { test: boolean } }

Answer №1

After doing some extra searching, I finally stumbled upon the solution I was looking for right here:

Modifying my previous code snippet to the following did the trick:

export default {
  props: {
    /** @type {{ new (): MyCustomType[] }} */
    resultCards: Array
  }
};

There is another syntax that also does the job (and personally, I find it more appealing):

export default {
  props: {
    /** @type {{ new (): Array<MyCustomType> }} */
    resultCards: Array
  }
};

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

Encountering an issue with the property name despite already defining it

Encountering a property name error even though it has been defined Uncaught (in promise): TypeError: Cannot read property 'nome' of undefined export class HomePage { inscricao = "São Bernardo"; nome = "abc"; nomeInvalido; construc ...

Vue headers are incomplete without the X-CSRF-TOKEN

Trying to perform a POST request using VueJS 1.0.28 is resulting in a Laravel 5.3 TokenMismatchException error. The following code snippet in app.js caters for both jQuery and Vue: Vue.http.interceptors.push((request, next) => { request.headers.se ...

Tips for implementing accurate structure on CloudFrontWebDistribution class

I seem to be facing an issue while trying to create a new instance of the CloudFrontWebDistribution using aws-cdk v1.7. The compiler is showing some dissatisfaction with the construct I provided. import { Stack, StackProps, Construct, App } from '@aw ...

Is incorporating atomic design into my Domain-Driven Design approach a practical choice for enhancing the user interface domain?

Just Starting Out with DDD I recently came across two blog posts that discussed the differences between DDD and the MVVM pattern. They also suggested a new folder structure with modules sharing common objects like { app, router, store, eventBus }. My que ...

Issue encountered during production mode compilation: "Vue Laravel Mix" is not a valid function

I'm working on a Laravel, VueJs, VueRouter, Vuex application that utilizes Webpack for compiling assets. Here's my package.json file: { "private": true, "scripts": { "dev": "npm run development", "development": "cross-env ...

What is the method to transfer the outcome of a GET request into a POST request?

My task involves sending a GET request to retrieve information from . When the request is made, it generates a random image and returns a JSON object that includes details such as "fileSizeBytes" and "url". My goal is to extract the "url" value and then ...

Consider pushing items onto an array only once when the condition is met, instead of adding to the array every

I have been tasked with importing Excel files containing customer orders into my web application. The process involves converting the data in the file into an object of arrays, where each array represents a row from the Excel sheet. Once the data is impor ...

Exploring the implementation of Generic types within a function's body

When trying to encapsulate logic inside the OrderGuard component (which can handle two types of orders: CheckinOrder or Checkout order), I encounter an issue when passing the order to the orderLoad callback in TypeScript. The error message states that "Ch ...

Innovative idea for a time management system based on timelines and scheduling

My latest project involves creating a scrollable scheduler, inspired by vis-timeline and built using Vue.js. One of the main challenges I'm facing is achieving smooth infinite scrolling in all directions (past and future). I must confess that I&apo ...

What is the best way to send various parameters to a component using [routerLink] or router.navigate?

My app-routing.module.ts is configured as shown below: const routes: Routes = [ { path: "branches/:branch", component: BranchesComponent }, // ... ]; In addition, in my app.component.html, I have the following code: <li> ...

Encountering a Typescript error while trying to implement a custom palette color with the Chip component in Material-UI

I have created a unique theme where I included my own custom colors in the palette. I was expecting the color prop to work with a custom color. I tested it with the Button component and it performed as expected. However, when I attempted the same with the ...

Definition of DataTypes in TypeScript

My goal seems simple to me, but I am realizing that my experience with Typescript might not be enough. I want to create a type that can accept the following expressions: const dp: DataPoint = [1, 2]; const dp2: DataPoint = [1, 2, 3]; const dps: DataPoints ...

What is the process for applying this specific style to a certain element?

Here is an example of an element in an Angular2 app: <div class="ticket-card" [ngStyle]="{'background': 'url(' + ticketPath + ')' , 'background-size': 'cover'}"> I would like to enhance the style b ...

Passing the value of an Angular component to a different component

I have a menu in my application that uses IDs to route content, and I also have a detailed view where the content should be displayed based on those same IDs. Currently, I am trying to display objects by their ID when a button is clicked. However, I' ...

Unable to store object data within an Angular component

This is a sample component class: export class AppComponent { categories = { country: [], author: [] } constructor(){} getOptions(options) { options.forEach(option => { const key = option.name; this.categories[k ...

Updating Dropdown Selection in Angular 9 and 10

Is there a way to set attributes to "Selected" in HTML options based on a condition from a *ngFor loop in response.body of the component ts file? Below is the dropdown code: <select [(ngModel)]="customer.id"> <option *ngFor="let location of lo ...

Troubleshooting Vuex Action Handler Context Object and Payload ESLint regulations issue

Having an issue with the vuex action handler and ESLint regulations If variables are not used or if the object is empty, this action code will trigger an ESLint error actions:{ handler:({commit, state}, payload) =>{} } The use of commit and state a ...

How can I use JavaScript to update the content inside HTML tags with a specific string?

I have a situation where I need to replace a string in two different ways Input: Parameters-->string, AnnotationName, input Case 1: And I should input <i>Annotaion</i> as <b>input</b> Output : { displayData: `And I should inp ...

An issue arises when trying to group and sum an array of objects due to difficulty converting strings to arrays in TypeScript

Below is the provided code snippet: Definition of Interface - interface IWEXInterface { readonly Date?: string; "Exec Qty"?: string; readonly Expiry?: string; } Data Collection - let data: IWEXInterface[] = [ { Date: &qu ...

Creating a global variable in Angular that can be accessed by multiple components is a useful technique

Is there a way to create a global boolean variable that can be used across multiple components without using a service? I also need to ensure that any changes made to the variable in one component are reflected in all other components. How can this be ac ...