Keeping TS types when wrapping mapState function #606
-
We are incrementally migrating a big Instead of directly using, for instance: // Vue SFC component
import {mapState} from 'pinia';
import {useUserStore} from 'user-store';
// Options API
...mapState(useUserStore, {loggedIn: 'loggedIn'}); We would like to wrap all/some user state functions into something like: // User pinia store (user-store.js)
import {mapState} from 'pinia';
import {useUserStore} from 'user-store';
export const mapUserState = (args) => mapState(useUserStore, args); // Vue SFC component
import {mapUserState} from 'user-store';
// Options API
...mapUserState({loggedIn: 'loggedIn'}); The wrapper works but we are losing TS type inference. Here is a super simple project showing the issue: https://github.com/agualis/pinia-wrappers Using plain Using custom This is the type hint that I see in the wrapped version. It seems correct but not 100% sure: Does anyone has any clue on why this is happening and how to make it work? Could it be just an unrelated Vetur issue? 🙏🏽 The same happens when passing array params instead of object ones to mapState: javascript
Is it a known drawback of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
for the array, you need to pass About you helper, you likely need to add a generic to it, something in the lines of function mapHelper<T>(mapper: T) {
return mapState(useUserStore, mapper)
} |
Beta Was this translation helpful? Give feedback.
Ah yeah, then
You can also create a
d.ts
where you create the type in TS and then import it: