-
-
Notifications
You must be signed in to change notification settings - Fork 977
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
Comments
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: |
Sorry I forgot to mention that it was to be able to mock getters in tests |
By the way TypeScript complains about this error for Pinia's test file :
|
I think we made the getters read only at some point because its easier to spot bugs. |
Can we have an update for the docs at least to mention this issue? |
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); |
I forgot to update the docs with the Getters, it's fixed now! See 7218691 |
Reproduction
https://stackblitz.com/edit/github-xe2cxw?file=src%2Fstores%2Fcounter.test.ts
Steps to reproduce the bug
Given the following code:
Expected behavior
TypeScript should not complain.
Actual behavior
TypeScript raises the following error:
Additional information
This is to be able to mock getters in tests
https://pinia.vuejs.org/cookbook/testing.html#mocking-getters
The text was updated successfully, but these errors were encountered: