Issue "TS2339: Could not find attribute 'X' on type 'Y'" encountered while accessing a data property within a function

New to Vuejs 3 and Typescript here. I have a button that should fetch tasks for a "to do" list when clicked. Below is the script I'm using:

<script lang="ts">
import axios from 'axios';

export default {
  name: 'Todos',

  data() {
    return {
      tasks: Array<Task>(),
    }
  },
  methods: {
    async getTasks() {
        await axios.get('https://.../tasks').then(response => {
           this.tasks = response.data 
        })
  }
}

But when I try writing this.tasks, I encounter an error:

TS2339: Property 'tasks' does not exist on type '{ getTasks(): Promise ; }'.

Any ideas on how to access a data property from within a method?

Answer №1

To utilize TypeScript in Vue, you must define components in the following manner:

import { defineComponent } from 'vue'

export default defineComponent({
    ...
})

Official Documentation

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 is the process for passing a URL from a component and assigning it as the new service URL?

Hello, I am new to Angular and I am trying to pass a URL from a component and set it as the new service URL. Here is my code: pokemon.service.ts private _url: string = "https://pokeapi.co/api/v2/pokemon"; constructor(private http : HttpClient) { } ...

Challenges with TypeScript build in DevOps related to Material UI Box Component

Currently, I am facing an issue while trying to build a front end React Typescript application using Material ui in my build pipeline on Azure DevOps. The problem seems to be related to the code for the Material ui library. Despite successfully building th ...

Challenges encountered when using Tailwindcss and Nextjs with class and variables

Hey there, I'm currently facing a major issue with tailwindcss + nextjs... The problem lies in setting classes using a variable. Although the class is defined in the css, tailwind fails to convert it into a style. This is how I need it to be: https ...

The color syntax in the text editor of Visual Studio 2022 is being lost when casting an interface

After attempting to cast an interface, the entire code turns white. let object : someInterface = <someInterface> someUnknownHapiRequestPayload View a screenshot of the text editor here I have already tried common troubleshooting steps such as updat ...

The implementation of race in React Redux Saga is proving to have negligible impact

I have implemented the following saga effect: function* loginSaga() { const logoutTimeoutCreationDate: string | null = yield localStorage.getItem('logoutTimeoutCreationDate'); let logoutTimeout: number; if (!logoutTimeoutCreationDate || + ...

Adding a type declaration to the severity property in React Alert - A guide to Typescript

I've developed a type declaration object for the incoming data, but no matter what I try to define as the type for the property "severity", it's not happy. The options it wants (as displayed below) don't seem feasible. I'm curious if th ...

There has been an error of type TypeError, as the property 'replace' cannot be read from a null value

I encountered a TypeError message, even though my application seems to be functioning properly. "ERROR TypeError: Cannot read property 'replace' of null" I'm struggling to understand how to fix this issue. Can someone provide me ...

Angular - Set value on formArrayName

I'm currently working on a form that contains an array of strings. Every time I try to add a new element to the list, I encounter an issue when using setValue to set values in the array. The following error is displayed: <button (click)="addNewCom ...

Is there a way to automatically scroll to the bottom of a div when it first

Looking to enhance my application with a chat feature that automatically scrolls to the bottom of the chat page to display the latest messages. Utilizing VueJs: <template> <div id="app"> <div class="comments" ...

Unable to assign a value to an element obtained from the Axios response in React

I'm having trouble mapping an array, it doesn't seem to be working. You can find the code here : https://codepen.io/ilaan16/pen/eYRKgOm setUtils(result.data); if (!utils) { console.log("ERROR_UTILS", result); } else if ...

How can we fetch data from the server in Vue 2.0 before the component is actually mounted?

Can anyone help me with this question noted in the title? How can I prevent a component from mounting in <router-view> until it receives data from the server, or how can I fetch the data before the component is mounted in <router-view>? Here a ...

Tips for setting variable values in Angular 7

I'm encountering an issue with assigning values to variables in my code. Can anyone provide assistance in finding a solution? Here is the snippet of my code: app.component.ts: public power:any; public ice:any; public cake:any; changeValue(prop, ...

Updating the property value of an element in Angular 9

I'm facing an issue where I can't change the attribute value of an element successfully. Is there a best practice in Angular that can help solve this problem? <div name="hai" (click)=test($event) #ckechName> In the TypeScript ...

I am unable to locate a declaration file for the module 'react-native-foo-package'

Whenever I try to add a new component to my pure React Native project, the screen turns completely white. The import statement for 'react-native-foo-package' has some unusual symbols next to the package name, and triggers this error message: [ ...

What distinctions can be made between extends and mixins in the context of the vue-class-component library?

Heads-up: Our Vue 2 setup is currently not utilizing the Composition API and we have no immediate plans to do so. This discussion focuses on vue-class-components. Query: The guide for vue-class-components mentions that we can employ a "normal" extends to ...

Sharing data between two Angular components without using HTML

In my file structure, I have: 1. a topbar folder - with html(1) and ts(1) for the topbar 2. a page folder - containing html(2) and ts(2) for the page But now, there's a BUTTON in html(1) <button (click)="refresh()" class="navbar-btn btn btn-succe ...

Tips for setting up a full-size image with nextJS and the <Image /> component

Upgrading NextJS to the latest version has resulted in some errors when using the Image component: // import Image from 'next/image' <div style={Object.assign({}, styles.slide, style)} key={key}> <Image src={src} alt="&quo ...

How can I correctly enable the css property in Emotion 11 and Next.js 10 applications?

The css property is not being detected during the build process. It's unclear whether this issue stems from a misconfiguration in the project settings or if it's a known bug. Type '{ children: (string | number | boolean | {} | ReactElement ...

Can anyone suggest a simpler method for creating function signatures with multiple conditions?

In my function, the first argument now determines if the function should receive an array or not. This function is similar to Foo type stringF = (arr: false, type: 'str', value: string) => void type numberF = (arr, false, type: 'num&apo ...

What is the process of refactoring TypeScript and React project expressions?

Currently, I am utilizing TypeScript in a React project but encountering type errors. Please assist me by examining my code. I need help with refactoring this code. I believe the issue lies in the expression that uses the args of the useTabs function. ...