-
Notifications
You must be signed in to change notification settings - Fork 85
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
Question: How to use vuex-map-fields with typescript? #75
Comments
you can do like this: |
In addition to what @lhy666 said, if you need to access mapped fields within your script part (in <script></script> section), you will have to tell Typescript how to infer the types. The solution I've found is to extend Vue class with your fields definition. Example in your Vue component :
|
@nowrap have the comments here solved your problem? |
if you don't want build a class for each map you can do some thing like this :
or even better define an interface
|
like?
|
And can you use this with |
As said @azarashill I created a file declare module 'vuex-map-fields' {
export function mapFields<V extends { [P in U]: any }, U extends keyof V>(
fields: V
): { [P in U]: () => any }
} |
In order to use mapFields like mapGetters from vuex with namespaces, I've written according definitions; declare module 'vuex-map-fields' {
interface Mapper<R> {
<Key extends string>(map: Key[]): { [K in Key]: R }
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R }
}
interface MapperWithNamespace<R> {
<Key extends string>(namespace: string, map: Key[]): { [K in Key]: R }
<Map extends Record<string, string>>(namespace: string, map: Map): { [K in keyof Map]: R }
}
type Computed = () => any
export const mapFields: Mapper<Computed>
& MapperWithNamespace<Computed>
} If you have highlighted eslint errors just add |
Hello,
i am trying to use vuex-map-fields with typescript. The custom definition file here in the issues section i found.
But i am not getting the computed / ...mapFields expression to work in typescript. With javascript it works like a charm.
Thank for any advises!
.nowrap
The text was updated successfully, but these errors were encountered: