Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot bypass getters with TypeScript #1605

Open
nterray opened this issue Aug 30, 2022 · 5 comments
Open

Cannot bypass getters with TypeScript #1605

nterray opened this issue Aug 30, 2022 · 5 comments
Labels
✨ enhancement New feature or request has workaround The issue contains a temporary solution to get around the problem 🧪 pkg:testing Related to the @pinia/testing package

Comments

@nterray
Copy link

nterray commented Aug 30, 2022

Reproduction

https://stackblitz.com/edit/github-xe2cxw?file=src%2Fstores%2Fcounter.test.ts

Steps to reproduce the bug

Given the following code:

import { defineStore } from 'pinia';
import { createTestingPinia } from '@pinia/testing';

const useStore = defineStore('lol', {
  state: () => ({ n: 0 }),
  getters: {
    double: (state) => state.n * 2,
  },
});
const pinia = createTestingPinia();
const store = useStore(pinia);

store.n++;
store.double = 3;

Expected behavior

TypeScript should not complain.

Actual behavior

TypeScript raises the following error:

TS2540: Cannot assign to 'double' because it is a read-only property.

Additional information

This is to be able to mock getters in tests
https://pinia.vuejs.org/cookbook/testing.html#mocking-getters

@LinusBorg
Copy link
Member

Why would you assign to a getter? It's a getter, it has not setter.

TS is warning you correctly here. if you want to do this for some exotic reason: //@ts-expect-error

@nterray
Copy link
Author

nterray commented Aug 30, 2022

Sorry I forgot to mention that it was to be able to mock getters in tests
https://pinia.vuejs.org/cookbook/testing.html#mocking-getters

@nterray
Copy link
Author

nterray commented Aug 30, 2022

By the way TypeScript complains about this error for Pinia's test file :

➜ ./node_modules/.bin/tsc packages/testing/src/testing.spec.ts 
[…]
packages/testing/src/testing.spec.ts:217:11 - error TS2540: Cannot assign to 'double' because it is a read-only property.

217     store.double = 3
              ~~~~~~

@posva
Copy link
Member

posva commented Aug 30, 2022

I think we made the getters read only at some point because its easier to spot bugs.
A possible solution for this is to have some kind of wrapper when calling useStore but still not ideal. At the moment, a ts-expect-error comment in tests will do but maybe we can come up with a more ergonomic solution

@posva posva added ✨ enhancement New feature or request has workaround The issue contains a temporary solution to get around the problem 🧪 pkg:testing Related to the @pinia/testing package labels Aug 30, 2022
@Hatzen
Copy link

Hatzen commented Sep 7, 2023

Can we have an update for the docs at least to mention this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request has workaround The issue contains a temporary solution to get around the problem 🧪 pkg:testing Related to the @pinia/testing package
Projects
None yet
Development

No branches or pull requests

4 participants