]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: improve getter return type
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 4 Mar 2024 15:44:55 +0000 (16:44 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 4 Mar 2024 15:44:55 +0000 (16:44 +0100)
packages/docs/core-concepts/getters.md

index 02ae81a5d642626bd899cd6579f152c17af82e04..ff36bd767707442a906240058806129493482c87 100644 (file)
@@ -55,9 +55,28 @@ const store = useCounterStore()
 
 ## Accessing other getters
 
-As with computed properties, you can combine multiple getters. Access any other getter via `this`. Even if you are not using TypeScript, you can hint your IDE for types with the [JSDoc](https://jsdoc.app/tags-returns.html):
+As with computed properties, you can combine multiple getters. Access any other getter via `this`. In this scenario, **you will need to specify a return type** for the getter.
 
-```js
+::: code-group
+
+```ts [counterStore.ts]
+export const useCounterStore = defineStore('counter', {
+  state: () => ({
+    count: 0,
+  }),
+  getters: {
+    doubleCount(state) {
+      return state.count * 2
+    },
+    doubleCountPlusOne(): number {
+      return this.doubleCount + 1
+    },
+  },
+})
+```
+
+```js [counterStore.js]
+// You can use [JSDoc](https://jsdoc.app/tags-returns.html) in JavaScript
 export const useCounterStore = defineStore('counter', {
   state: () => ({
     count: 0,
@@ -80,6 +99,8 @@ export const useCounterStore = defineStore('counter', {
 })
 ```
 
+:::
+
 ## Passing arguments to getters
 
 _Getters_ are just _computed_ properties behind the scenes, so it's not possible to pass any parameters to them. However, you can return a function from the _getter_ to accept any arguments: