I'm using the options api and when attempting to access a property of the Vue data object in a computed property I get an error in the typechecking.
Property 'quantity' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, { item: unknown; total: unknown; menuItemCategories: any; }, Readonly<Record<never, any>>>'
The property does exist since the page is able to load and display rendered template correctly using the computed property - only the type checker complains.
The component code (simplified for length):
import Vue from "vue";
export default Vue.extend({
data() {
quantity: 1,
},
computed: {
total() {
return this.item.price * this.quantity;
}
},
});
Edit
I've been able to work around the issue by using the data
property as an object.
This does create some issues though since it is best practice to use data
as a function which returns an object. The same issue applies to asyncData
.
Further trial and error has shown I'm able to access the data
function properties through the methods
property. However if I use the mapGetters
helper from vuex it throws the same type error as computed properties.
The methods
are also not available in the CombinedVueInstance
type inside computed properties.
tsconfig.json
// tsconfig.json
{
"compilerOptions": {
"target": "es2018",
"module": "esnext",
"moduleResolution": "node",
"lib": [
"esnext",
"esnext.asynciterable",
"dom"
],
"esModuleInterop": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"noEmit": true,
"baseUrl": "./src",
"paths": {
"~/*": [
"./*"
],
"@/*": [
"./*"
]
},
"types": [
"@types/node",
"@nuxt/types",
"@nuxtjs/axios"
]
},
"exclude": [
"node_modules"
]
}
vue-shim.d.ts
declare module "*.vue" {
import Vue from 'vue'
export default Vue
}