Error in TypeScript when using Vue/Nuxt.js: The property 'latitude' is not found in the type '(() => any) | ComputedOptions<any>'

As a newcomer to Vue.js, I am currently utilizing it with Typescript on a Nuxt.js (v2.15.8) application.

The code snippet provided below is functioning correctly:


        export default Vue.extend({
            name: 'MyComponent',
            computed: {
                isLatitudeValid() {
                    return this.form.latitude ? this.form.latitude >= -90 && this.form.latitude <= 90 : null;
                }
            },
            data: () => ({
                form: {
                    address: null,
                    city: null,
                    postalCode: null,
                    latitude: null,
                    longitude: null
                }
            })
        });
    

However, I encountered a Typescript error when attempting to introduce props, which prevented me from accessing this.form.latitude within the isLatitudeValid function.


        export default Vue.extend({
            name: 'MyComponent',
            props: { // Added this recently
                someProp: String
            },
            computed: {
                isLatitudeValid() {
                    return this.form.latitude ? this.form.latitude >= -90 && this.form.latitude <= 90 : null;
                    // Error message: Property 'latitude' does not exist on type '(() => any) | ComputedOptions<any>'.
                    // Property 'latitude' does not exist on type '() => any'.Vetur(2339)
                }
            },
            data: () => ({
                form: {
                    address: null,
                    city: null,
                    postalCode: null,
                    latitude: null,
                    longitude: null
                }
            })
        });
    

It appears that Visual Studio Code/Vetur/Typescript compiler is no longer able to recognize this properties when props are added.

Referring to this resource (in the "Avoiding naming collisions" section), I should be able to access properties defined in both props and data, as long as there are no naming conflicts.

I believe I am overlooking something: how can I resolve this issue?

Answer №1

Discovered a solution: The issue was resolved by specifying the return type of the isLatitudeValid function:

export default Vue.extend({
        name: 'MyComponent',
        props: { // Added this
            someProp: String
        },
        computed: {
            isLatitudeValid(): boolean | null {
                return this.form.latitude ? this.form.latitude >= -90 && this.form.latitude <= 90 : null; // Now it compiles successfully! \o/
            }
        },
        data: () => ({
            form: {
                address: null,
                city: null,
                postalCode: null,
                latitude: null,
                longitude: null
            }
        })
});

Source: https://github.com/vuejs/vue/issues/8625#issuecomment-411687109

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

[Vue Alert]: Render Error - "TypeError: Unable to retrieve the 'getters' property of an undefined object"

Here is the code snippet from my app.js file. Can someone please review it and confirm if I have defined the items correctly according to the Vuex documentation? import store from "./store"; require('./bootstrap'); window.Vue = require( ...

navigate to a new page in vue with node.js

As I continue to expand my knowledge in JavaScript, Vue, and Node.js, I encountered a specific issue that I need help with. My goal is to redirect the Vue page after logging in using Node.js. Below you'll find the code snippets for my Vue setup and sc ...

The error message "Property <property> is not recognized on the type 'jQueryStatic<HTMLElement>'" is indicating an issue with accessing a specific property within the TypeScript codebase that utilizes Angular CLI, NPM,

My Development Environment I am utilizing Angular, Angular CLI, NPM, and Typescript in my web application development. Within one of my components, I require the use of jQuery to initialize a jQuery plugin. In this particular case, the plugin in question ...

Is there a way to ensure in TypeScript that a generic type includes a property that implements a particular method?

To better explain my query, let me provide an illustration. Suppose I aim to create a method that accepts three parameters, as shown below: customFilter<T>(values: T[], filterString: string, propName: string) any[] { return values.filter((value) ...

How can I exclude the 'node_modules' directory but still include specific subfiles in the tsconfig file for TypeScript?

My tsconfig file is structured as follows: { "compileOnSave": false, "compilerOptions": { "module": "es2015", "target": "es2015", "sourceMap": true, "jsx": "react", "allowSyntheticDefaultImports": true, "noImplicitAny": false, ...

Exploring the Differences between Angular's Http Module and the Fetch API

While I grasp the process Angular uses for HTTP requests, I find myself leaning towards utilizing the Fetch API instead. It eliminates the need to subscribe and unsubscribe just for a single request, making it more straightforward. When I integrated it int ...

Tips for sending class objects with Typescript/React

I need to establish an Array of items with different characteristics as a Prop on my component. My goal is to transmit an array of items with unique properties to a component. Here's an example of what my array of objects might look like: ` let dish ...

What is the best way to remove uncategorized posts from WordPress for good?

In my WordPress site, I am attempting to delete the uncategorized category permanently. After doing some research, it seems like there may not be a straightforward solution for this issue. However, in my vue.js project where I am utilizing the wp api and ...

Tips for leveraging the functions service in Next.js for better code reusability

I am just starting to learn Next.js and I have a preference for organizing my API functions in a separate folder called services. I attempted to implement some code based on this topic but unfortunately, it did not work as expected. It seems like my api fu ...

Using Vue.js to submit a form in Laravel and redirecting with a flash message

I am facing an issue where I have two components named Index and Create, loaded from separate blade files. The challenge is passing a flash message as a prop between these components due to their file separation. How can I redirect after submitting a form ...

Tips for refreshing the 'state' of an Angular Router component

I'm facing an issue with passing data between pages using Angular routing. Everything works as expected when navigating to the "next" page for the first time. However, upon returning to the home page and selecting a different item, the Router's s ...

Displaying a message when there are no results in Vue.js using transition-group and encountering the error message "children must be keyed

Utilizing vue.js, I crafted a small data filter tool that boasts sleek transitions for added flair. However, I encountered an issue with displaying a message when there are no results matching the current filters. Here's what I attempted: <transit ...

The error TS2339 is indicating that there is no property called myProperty on the type SetStateAction<User>

I'm encountering a TypeScript error while working with React that's leaving me puzzled: <html>TS2339: Property 'subEnd' does not exist on type 'SetStateAction&lt;User&gt;'.<br/>Property 'subEnd' d ...

Tips for monitoring Google Analytics/ Adwords Conversions within VueJS

I have developed a VueJS application in which users submit a form, and the data is sent to the server using Vue-resource. I want to track this as a conversion for Google Analytics. Google has provided me with a script that needs to be placed on a page lik ...

Tips for organizing MUI Card effectively within a React application using TypeScript

Struggling to build a React card component with Material-UI (MUI) and TypeScript that represents a car? The card should contain text, an image, checkboxes, a rating, and a button. Here's the code I've come up with so far: import React from ' ...

Tips for managing errors when utilizing pipe and mergemap in Angular

In the code snippet provided, two APIs are being called. If there is an error in the first API call, I want to prevent the second API call from being made. Can you suggest a way to handle errors in this scenario? this.userService.signUp(this.signUpForm.v ...

Why is my Vue/MySQL Database not showing up online, even though it is accessible locally?

While my application runs smoothly locally, I encounter an issue when deploying to the Heroku server. The page contents linked to the MySQL Database fail to display without any CORS errors or fetch call issues in Chrome Dev Tools. The page loads, but remai ...

When typing declarations are used, they clarify whether the entity being referenced is an Object or

I am currently working on aligning the Dockerode run typings to match the actual implementation. The issue arises when I invoke run as TypeScript consistently identifies the return value as a Promise. It seems that TypeScript is having trouble distinguish ...

The HTML table is displaying with an offset, which is probably caused by the *ngFor directive

I'm having trouble aligning the HTML table properly, as it seems to be misaligned. The issue I am facing is related to the inner loop (modification) which is a list inside of Revision (basically, Revision 'has a' modification list). Althoug ...

I'm encountering an issue with my React 18 application using TypeScript: the module './App' cannot be found

Encountering an issue while attempting to update to react 18. I'm curious if this problem is related to my file types. I am using Typescript, so do both the app and index files need to have a .tsx extension? Both the app and index files are located ...