Certain Array properties cause Array methods to generate errors

I am working with an Array prop in my component's props that is defined like this

props: {
  datasetsList: {
    type: Array as PropType<Array<ParallelDataset>>,
    required: false
  }
},

Within the setup() method of my component, I have a function that looks like this

const selectDataset = (radioValue: number) => {
  setCustomDataSet(props.datasetsList.find(dataset => dataset.id === Number(radioValue)));
}

When running this code, I encounter an error specifically on the .find() part, stating

Property 'find' does not exist on type 'unknown'.Vetur(2339)

To resolve this issue, I changed the line from

props.datasetsList.find()

to

(props.datasetsList as Array<ParallelDataset>).find()

which successfully removed the error. Does this indicate that there is an issue with how I defined the Array prop? Or could it be something else? I am currently utilizing the latest es version.

Edit:

Here is the full code for the component:

<template>
  <div class="datasets-listing xs:mb-14 sm:mb-8 w-full">
    <div class="grid xs:grid-cols-1 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-x-5 gap-y-5 mb-16">
      <div class="w-12/12 custom-dataset rounded" v-for="dataset in datasetsList" :key="dataset.id">
        <FormRadio
          :id="dataset.name"
          :value="dataset.id"
          :v-model="radioVmodel"
          :isInputHidden="true"
          class="rounded border"
          :class="radioVmodel == dataset.id ? 'selected-dataset border-blue-700' : 'default-dataset border-gray-300'"
          @change="selectDataset">
          <template v-slot:radio-label>
            <ParallelDatasetCard 
              :dataset="dataset"/>
          </template>
        </FormRadio>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import Vue, { PropType } from "vue";
import Tag from "@/components/Tag.vue";
import ParallelDatasetCard from "@/components/ParallelDatasetCard.vue";
import FormRadio from "@/components/form-elements/FormRadio.vue";
import { ref } from "@vue/composition-api";
import Button from "@/components/Button.vue";
import Translate  from "@/services/TranslationService";
import { ParallelDataset } from "@/models/models";

export default Vue.extend({
  name: "DatasetsListing",
  props: {
    datasetsList: {
      type: Array as PropType<Array<ParallelDataset>>,
      required: false
    }
  },
  setup(props, context) {
    const { setCustomDataSet, getCustomDataset } = Translate();
    const pickedDataset = getCustomDataset;
    const radioVmodel = ref(String(pickedDataset.value.id));

    const selectDataset = (radioValue: number) => {
      radioVmodel.value = String(radioValue);
      setCustomDataSet(props.datasetsList.find(dataset => dataset.id === Number(radioValue)));
    }

    return {
      radioVmodel: radioVmodel,
      pickedDataset: pickedDataset,
      selectDataset: selectDataset
    }
  },
  components: {
    Tag,
    Button,
    FormRadio,
    ParallelDatasetCard
  }
})
</script>

<style lang="scss" scoped>
.datasets-listing {
  .custom-dataset {
    transition: .2s all ease-in-out;

    &:hover {
      box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.1);
    }
  }
  .selected-dataset {
    box-shadow: 0px 0px 3px 0px #2da4df;
  }
}
</style>

Answer №1

If you want to benefit from type inference, make sure to utilize the defineComponent helper when creating your component:

import { defineComponent, ..... } from 'vue'

export default defineComponent({
    props: {
        ......
    },
    setup(props) {
            // Your code here
        }
});

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 Duplicate Identifier Error while working on Angular 2 Typescript in Visual Studio Code

Currently attempting to configure a component in Angular 2 with Typescript using Visual Studio Code on Mac. Encounter the following errors when trying the code below: duplicate identifier 'Component'. and Duplicate identifier' DashboardCompo ...

Uploading multiple strings to an Amazon S3 bucket using Node.js by piping a string

Suppose I have a simple loop similar to the one shown below: for (const i=0; i<3; i++) { to(`This incrementer is ${i}`) } At the end of the loop, I expect my file to contain: This counter is 0 This counter is 1 This counter is 2 I at ...

The error message "TextEncoder is not defined with mongodb nodes" is indicating that there is

Encountering an issue while running jest test cases: Getting the error message - ReferenceError: TextEncoder is not defined. Current Node version being used is 14.18.0. Mongodb NPM package version is 4.1.3. Typescript version installed is 4.4.3. Here ...

Generating auto UUIDs in PostgreSQL using TypeORM

Currently, I am in the process of developing a REST API and utilizing TypeORM for data access. While I have been able to use it successfully so far, I am facing an issue regarding setting up a UUID auto-generated primary key on one of my tables. If anyone ...

Stopping npm build when ESLint detects warnings

Dealing with a particularly immature team, I am determined to make the react-typescript build fail whenever ESLint issues warnings. src/modules/security/components/ForgotPasswordBox/index.tsx Line 8:18: 'FormikHelpers' is defined but never use ...

Error message: The database query function is encountering an issue where the property 'relation.referencedTable' is undefined and cannot be accessed

Currently, I am working with two schemas named products.ts and category.ts. The relationship between these files is defined as one-to-many. In the products.ts file: import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; import ...

Experiencing unexpected behavior with Next.JS getStaticProps functionality

I am currently working on a website where I want to display dynamic feedback as cards. However, my fetchData variable within the Home function is always returning undefined. Here's the code snippet I have tried: import UserCard from "../component ...

Having trouble getting Firebase phone number authentication to work with Vue.js

I am currently in the process of developing a new Vue.js application using the Webpack template. Within this app, I have implemented a /sign-in route that displays a component named SignIn. To authenticate users, I am utilizing Firebase Phone Number authen ...

NativeScript: TypeScript for Formatting Numbers

Being a beginner in NativeScript, I'm finding it difficult to find basic information through Google search. But now, I have a specific question: I have the number 1234567.89 stored in a variable, and I want to display it in a label with the format ...

What is the best way to trim a string property of an object within an array?

I am seeking a solution to access the "description" property of objects within an array and utilize a string cutting method, such as slice, in order to return an array of modified objects. I have attempted using for loops but have been unsuccessful. Here ...

Refreshing the Mat Dialog content when removing items in Angular Material

I have successfully implemented a mat dialog table with 3 columns - name, email, and delete icon. When the user clicks on the delete icon, it prompts a confirmation message to confirm the deletion. Upon confirming, the item is removed from the database. Ho ...

What is the recommended lifecycle hook in Vue.js2 to execute a function when the page is loaded?

I have a dynamic table that can be filled with various numbers of rows, and I want to add an overlay before the data is loaded using my applyOverlay() function. Below is the structure of my HTML: <table id="table" class="datatable" s ...

Having Trouble Displaying Material UI Icons in Your React App? Here's Why: "Invalid Objects as React Children"

I have been working on a basic app that showcases information about a patient. In this specific component, I am only displaying the name, occupation, and a symbol from Material UI to indicate whether the patient is male or female. However, when I attempt ...

Can you identify the reason for the hydration issue in my next.js project?

My ThreadCard.tsx component contains a LikeButton.tsx component, and the liked state of LikeButton.tsx should be unique for each logged-in user. I have successfully implemented the thread liking functionality in my app, but I encountered a hydration error, ...

Show or hide a div in Vuejs based on checkbox selection

I've been attempting to toggle the visibility of a container div using Vuejs with no success. I've tried two different methods, but neither seem to work for me. Here is Method 1: <body> <div class="checkbox" id = "selector"& ...

The problem arises in VueJS when attempting to show an image and a message using the id retrieved from the API

I'm attempting to achieve something similar to this: <vx-card> <div class="header"> <div class="left"> <img :src="'./img/info_&ap ...

Building a dropdown menu component in react native

Looking to implement a dropdown menu in React Native using TypeScript. Any suggestions on how to achieve this for both iOS and Android platforms? Check out this example of a dropdown menu ...

Comparing plain objects and class instances for modeling data objects

What is the recommended approach for creating model objects in Angular using TypeScript? Is it advisable to use type annotation with object notation (where objects are plain instances of Object)? For example, let m: MyModel = { name: 'foo' } ...

Uncover hidden mysteries within the object

I have a function that takes user input, but the argument type it receives is unknown. I need to make sure that... value is an object value contains a key named "a" function x(value: unknown){ if(value === null || typeof value !== 'obj ...

Create a debounce click directive for buttons in a TypeScript file

I'm facing an issue with implementing debounce click on a dynamically added button using TypeScript. I need help with the correct syntax to make it work. private _initActionsFooter(): void { this.actionsFooterService.add([ { ...