The automatic inference of props types by TypeScript inside the computed property within the setup function is not supported

My component is designed to accept 3 arrays as props, and I need to merge them all before displaying the data. However, I am encountering TypeScript errors that are causing some issues:

<script lang="ts">
import {RisikoentitetRevision, IdentitetsmarkoerRevision, RisikomarkeringRevision} from '@/model/Risikoentitet'
import {defineComponent, computed, PropType} from 'vue'

export default defineComponent({
  name: 'HistorikLogTabel',
  props: {
    risikoentitetRevisioner: {
      type: Array as PropType<RisikoentitetRevision[]>,
      required: true,
    },
    identitetsmarkoerRevisioner: {
      type: Array as PropType<IdentitetsmarkoerRevision[][]>,
      required: true,
    },
    risikomarkeringRevisioner: {
      type: Array as PropType<RisikomarkeringRevision[][]>,
      required: true,
    },
  },
  setup(props) {
    console.log({props})
    const allRevisions = computed(() => {
      console.log("2nd", {props})
      return [
        ...props.risikoentitetRevisioner, 
        ...props.identitetsmarkoerRevisioner.flat(), 
        ...props.risikomarkeringRevisioner.flat(), 
      ]
    })
    return {
      allRevisions,
    }
  }
})
</script>

I'm struggling to identify what might be wrong with my setup. Even passing props into the computed argument hasn't resolved the issue.

EDIT:

A temporary workaround involves ignoring the TS errors, allowing the logic to function correctly. However, this solution is not ideal, and I am eager to understand the root cause of these TypeScript problems.

    const allRevisions = computed(() => {
      console.log("2nd", {props})
      return [
        // @ts-ignore
        ...props.risikoentitetRevisioner, 
        // @ts-ignore
        ...props.identitetsmarkoerRevisioner.flat(),
        // @ts-ignore
        ...props.risikomarkeringRevisioner.flat(), 
      ]
    })
    console.log({allRevisions})

Answer №1

Upon conducting further research, I came to the realization that I was overcomplicating things unnecessarily. It dawned on me that I was conflating Options API with Composition API. Since my project predominantly relies on Options API, I have decided to adhere to it.

There was no need for me to utilize setup() when a simple computed: would suffice. Here is the finalized code snippet that TypeScript approved of and performed as expected:

<script lang="ts">
import {RisikoentitetRevision, IdentitetsmarkoerRevision, RisikomarkeringRevision} from '@/model/Risikoentitet'
import {defineComponent, PropType} from 'vue'

export default defineComponent({
  name: 'HistorikLogTabel',
  props: {
    risikoentitetRevisioner: {
      type: Array as PropType<RisikoentitetRevision[]>,
      required: true,
    },
    identitetsmarkoerRevisioner: {
      type: Array as PropType<IdentitetsmarkoerRevision[][]>,
      required: true,
    },
    risikomarkeringRevisioner: {
      type: Array as PropType<RisikomarkeringRevision[][]>,
      required: true,
    },
  },
  computed: {
    allRevisions() {
      const revisions = [
        ...this.risikoentitetRevisioner,
        ...this.identitetsmarkoerRevisioner.flat(),
        ...this.risikomarkeringRevisioner.flat(),
      ]
      console.log({revisions}) // Success! ✅
      return revisions
    }
  }
})
</script>

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

What function does the super() method serve in TypeScript subclasses?

On top of being irritating and requiring modifications to every child class whenever the parent class is updated... ...

Solving dependencies for npm modules

I have a project where I am utilizing a custom library to share Vue components across various applications. To achieve this, I have added my component library as an npm module and included it in the application's package.json file, which is functioni ...

The Angular reactive form is being submitted without completing the submission process

I've been working on an Angular 5 reactive form and everything seems to be functioning correctly. However, I've encountered a strange issue with a button used to close the form by hiding it from view. Whenever I click on this close button, the f ...

Trying to create a function in TypeScript that works like lodash's "bindKey"?

I am looking to create a function that serves as a shortcut for obj.func.bind(obj). It should work like this: const bound = <...>(obj: ..., fnKey: ...) : ... => obj[fnKey].bind(obj); const obj = { a: "abc", b() { console.log(thi ...

Tips for modifying an axios instance during response interception

Is there a way to automatically update an axios instance with the latest token received in a response, without making a second request? The new token can be included in any response after any request, and I want to make sure that the last received token ...

Ava tests hitting a snag with TypeScript ("Oops! Unexpected identifier found")

Currently delving into the realms of TypeScript, I decided to venture into creating a TypeScript React application using create-react-app. This application involves a separate TypeScript file called logic.ts, which in turn imports a JSON file. import past ...

Combining various attributes into a component by utilizing an object as props in Vue 3

Every time I try to bind multiple properties to a component using an object, I encounter some issues. I expect to pass the props according to the ContentOptions interface when using this component on different pages and have them be inherited by the compon ...

Implementing Angular - Injecting a component dynamically into another component

Currently, I am working on developing a small UI components framework for my personal use and enjoyment. One of the components I'm working on is a Tab component. To test this component, I need to dynamically inject another component (TabContainerCompo ...

Tips for effectively implementing React.usecallback

Looking for a way to rewrite the handleClick method using React.useCallback in a function called Parent, which is triggered by a button click event in React and TypeScript. function Parent () { const [isOpen, setIsOpen] = React.useState(false); ...

Learn the best way to retrieve the highest number from a Array<String> in TypeScript or JavaScript

Can someone help me create a function in JS or TS that meets the following requirements? I am looking for a functional programming approach. ・Input type: Array(String) ・Output type: string or undefined Examples Input Result ["" ...

Social Chat with Nuxt and Vue

After integrating the Vue Social Chat module into my Nuxt app, I am facing difficulties in understanding how to customize the props and color settings. Currently, everything is functioning properly, but the WhatsApp icon and top bar within the speech bubbl ...

ngx-datatable Retrieve the most recent clicked row using multi-click mode

Currently, I am attempting to retrieve the most recent 'clicked' row from ngx-datatable. Here is what I have in my code: <ngx-datatable [rows]="rows" [selected]="selected" [selectionType]="'multiClick'" (select)='on ...

Utilizing Typescript generics to define constraints for the type of T[K], where K is the key and T is the object

In the setting I'm dealing with, specific objects with an id attribute expire every "tick" and require retrieval using getObjectById. I am interested in creating a setter function to update a property of an object by mapping thing.property => getOb ...

showing the values stored in local storage

My goal is to show only the values stored in local storage, excluding the key value that displays all data after submitting the login form. welcome <span id="demo"></span>; <script> document.getElementById('demo').innerHTML = ...

Guide on making a personal component for displaying icons with element-plus?

Within element-plus, there are pre-existing icons such as <i-mdi-edit /> To utilize these icons and create a custom component, you can do the following: <Icon name="edit" /> By passing the name property, you can access the desired ...

In Angular, the process of duplicating an array by value within a foreach function is not

I have been attempting to duplicate an array within another array and make modifications as needed. this.question?.labels.forEach((element) => { element["options"] = [...this.question?.options]; // I've tried json.stringify() as wel ...

"An error occurred in Vue: The property or method 'msg' is not defined on the current instance but is being referenced during rendering. Please check your code for any typos or missing declarations

This is the code for my view page <div id="app"> @{{ msg }} @{{ content }} Including a form here <form method="post" enctype="multipart/form-data" v-on:submit.prevent="addPost"> <textarea v-model="content" id="postTe ...

How to eliminate the ng-component tag from an Angular 8 table row template

I currently have a dynamic table component with 2 modes: Normal table - functioning properly Custom Row Template - encountering issues due to Angular adding the <ng-component> tag The logic within the TableComponent is not the main concern, it&apo ...

To dismiss a popup on a map, simply click on any area outside the map

Whenever I interact with a map similar to Google Maps by clicking on various points, a dynamically generated popup appears. However, I am facing an issue where I want to close this popup when clicking outside the map area. Currently, the code I have writte ...

Is it possible to apply async/await within a describe block?

I have been struggling to run the following complete end-to-end test for my project. I have a collection of pages, each consisting of a page title and a list of steps required. However, in order to retrieve these pages, an asynchronous call is necessary. C ...