VSCode throw error "Property does not exist on type" when referencing to another getter #1299
-
Reproductionhttps://stackblitz.com/edit/vitejs-vite-ac81dz?file=src%2Fstore%2Findex.ts&terminal=dev Steps to reproduce the bugVSCode throw error "Property does not exist on type" when referencing another getter getters: {
subTotal: (state) => {
return state.checkout?.subtotalPriceV2?.amount;
},
totalDiscount(state) {
// This line throw the error on vscode: "Property 'subTotal' does not exist on type ..."
return this.subTotal;
},
}, Expected behavior
Actual behaviorVSCode throws the error:
Additional informationVue Language Features (Volar) v0.34.15 |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
You need to type the return: totalDiscount(): number | undefined {
return this.subTotal;
}, See https://pinia.vuejs.org/core-concepts/getters.html#getters |
Beta Was this translation helpful? Give feedback.
-
Actually, I had faced a related issue. Everything is OK after adding the return type of getters. But I want to know the exact reason... Here's a minimal example type Module = {}
const MODULES: Module[] = [{}]
export const useUserStore = defineStore('user', {
state: () => {
return {}
},
getters: {
check() {
return (module: Module) => true
},
modules() {
// this.check would get a TS2554: Expected 0 arguments, but got 1.
return MODULES.filter((module) => this.check(module))
}
}
} When I declared the return type of export const useUserStore = defineStore('user', {
state: () => {
return {}
},
getters: {
check() {
return (module: Module) => true
},
// Add return type
modules(): Module[] {
return MODULES.filter((module) => this.check(module))
}
}
} As the document pointed out:
Are there any information about this known limitation in TypeScript? |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing this with webstorm, the weird part is that in a quasar project it doesn't happens, not sure what's going on, all getters have the corresponding type declaration, but still get this error. |
Beta Was this translation helpful? Give feedback.
-
Also getting this no matter if I use arrow functions, regular functions, annotate return type or not. I've tried all. What doesn't make sense to me is why would state contain the properties that are defined as part of getters and not state? I read somewhere that the state parameter is actually a reference to the store but that makes no sense to me and in this case, it is not. It is actually a reference to state If I remove the annotation on state in the below code, then this is the inferred type that typescript is showing me. (property) state?: (() => {
items: never[];
}) | undefined import { defineStore } from 'pinia';
import { groupBy } from 'lodash';
import type { Product } from './ProductStore';
export type Item = {
product: Product;
};
export type CartStoreState = {
items: Array<Item>;
};
export const useCartStore = defineStore('CartStore', {
state: () => {
return { items: [] as Array<Item> };
},
getters: {
count: (state) => state.items.length,
isEmpty(state): boolean {
return state.count === 0;
},
grouped: (state) => groupBy(state.items, (item: Item) => item.product.name),
groupCount:
(state): ((name: string) => number) =>
(name: string) =>
state.grouped[name].length,
},
actions: {
addItemsToCart(qty: number, product: Product) {
for (let i = 0; i < qty; i++)
this.items.push({ product: { ...product } });
},
},
}); |
Beta Was this translation helpful? Give feedback.
-
No workaround works for us for this problem. TS: 5.3.3, vue-tsc: 1.8.27, vue: 3.4.21, pinia: 2.1.7. |
Beta Was this translation helpful? Give feedback.
-
Same here, none ot the presented work-arounds works pinia seems |
Beta Was this translation helpful? Give feedback.
You need to type the return:
See https://pinia.vuejs.org/core-concepts/getters.html#getters