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

Closed
nterray opened this issue Aug 30, 2022 · 7 comments
Closed

Cannot bypass getters with TypeScript #1605

nterray opened this issue Aug 30, 2022 · 7 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?

@sarahsporck
Copy link

I adjusted the mockedStore implementation in the docs & made the getters writable in typescript. This seems to work quite fine.

type Writeable<T> = { -readonly [P in keyof T]: T[P] };

function mockedStore<TStoreDef extends () => unknown>(
  useStore: TStoreDef,
): TStoreDef extends StoreDefinition<infer Id, infer State, infer Getters, infer Actions>
  ?
      | Exclude<
          Store<
            Id,
            State,
            Getters,
            {
              [K in keyof Actions]: Actions[K] extends (...args: infer Args) => infer ReturnT
                ? // 👇 depends on your testing framework
                  Mock<Args, ReturnT>
                : Actions[K];
            }
          >,
          _StoreWithGetters<Getters>
        >
      | Writeable<_StoreWithGetters<Getters>>
  : ReturnType<TStoreDef> {
  return useStore() as any;
}

const mockedCounterStore = mockStore(useCounterStore);

@posva posva closed this as completed in 7218691 May 29, 2024
@posva
Copy link
Member

posva commented May 29, 2024

I forgot to update the docs with the Getters, it's fixed now! See 7218691

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

5 participants