]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commit
feat(store): pass state to getters as first argument
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 May 2021 08:28:42 +0000 (10:28 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 May 2021 09:01:17 +0000 (11:01 +0200)
commit9f3132bd7a59c47f3bd9005695c4f1224dbaea16
tree3d10954665a8f91a08e5104a3aa2ecfdd538cceb
parent35497a1e93936b2c4da22bdce2d21d3abdce747d
feat(store): pass state to getters as first argument

BREAKING CHANGE: getters now receive the state as their first argument and it's properly typed so you can write getters with arrow functions:

```js
defineStore({
  state: () => ({ n: 0 }),
  getters: {
    double: state => state.n * 2
  }
})
```

To access other getters, you must still use the syntax that uses `this` **but it is now necessary to explicitely type the getter return type**. The same limitation exists in Vue for computed properties and it's a known limitation in TypeScript:

```ts
defineStore({
  state: () => ({ n: 0 }),
  getters: {
    double: state => state.n * 2,
    // the `: number` is necessary when accessing `this` inside of
    // a getter
    doublePlusOne(state): number {
      return this.double + 1
    },
  }
})
```

For more information, refer to [the updated documentation for getters](https://pinia.esm.dev/core-concepts/getters.html).
.eslintrc.js
src/mapHelpers.ts
src/store.ts
src/types.ts
test-dts/customizations.test-d.ts
test-dts/deprecated.test-d.ts
test-dts/mapHelpers.test-d.ts
test-dts/plugins.test-d.ts [new file with mode: 0644]
test-dts/store.test-d.ts