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

Question: How to use vuex-map-fields with typescript? #75

Open
nowrap opened this issue Sep 16, 2019 · 8 comments
Open

Question: How to use vuex-map-fields with typescript? #75

nowrap opened this issue Sep 16, 2019 · 8 comments

Comments

@nowrap
Copy link

nowrap commented Sep 16, 2019

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

@lhy666
Copy link

lhy666 commented Jan 8, 2020

you can do like this:
@Component({ computed: mapFields(['wordDetailsTemp.word']) })

@yannick-milanetto
Copy link

yannick-milanetto commented Aug 6, 2020

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 :

class VueWithMapFields extends Vue {
    public fieldOne!: boolean;
    public fieldTwo!: string[];
}

@Component({
    computed: mapFields(['fieldOne', 'fieldTwo']),
})
export default class MyComponent extends VueWithMapFields {
    // now I can access this.fieldOne or this.fieldTwo in lifecycle hooks, computed properties or methods
    // and I will have Typescript doing properly its typechecking job :)
}

@geoidesic
Copy link
Collaborator

@nowrap have the comments here solved your problem?

@ziaadini
Copy link

ziaadini commented Oct 17, 2020

if you don't want build a class for each map you can do some thing like this :

...(mapFields({
      loginUsername: 'login.username',
      loginPassword: 'login.password',
      registerUsername: 'register.username',
      registerPassword: 'register.password',
      registerEmail: 'register.email',
    }) as { [key: string]: { get(): void; set(value: any): void } })

or even better define an interface

interface VuexMapFields {
  [key: string]: { get(): void; set(value: any): void }
}
...(mapFields({
      loginUsername: 'login.username',
      loginPassword: 'login.password',
      registerUsername: 'register.username',
      registerPassword: 'register.password',
      registerEmail: 'register.email',
    }) as VuexMapFields

@azarashill
Copy link

like?

declare module 'vuex-map-fields' {
  export function mapFields<V extends { [P in U]: any }, U extends keyof V>(
    fields: V
  ): { [P in U]: () => any }
}

@tohagan
Copy link

tohagan commented Mar 15, 2021

And can you use this with vuex-module-decorators Vuex classes?

@lynx-r
Copy link

lynx-r commented Mar 26, 2021

As said @azarashill I created a file vuex-map-fields.d.ts with the type definition. Notice declare module must be single in a typings 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 }
}

@LorensDomins
Copy link

LorensDomins commented Nov 23, 2021

In order to use mapFields like mapGetters from vuex with namespaces, I've written according definitions; vuex-map-fields.d.ts

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 /* eslint-disable */ rule at the beginning of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants