Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed May 8, 2020
1 parent c7bcb24 commit 48b862a
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ You can then use these in the hooks to get type checking and inference:

```typescript
<script lang="ts">
import { LocalesState, LocalesGetters, LocalesActions, LocalesMutations, RootState }
import {
LocalesState,
LocalesGetters,
LocalesActions,
LocalesMutations,
RootState,
} from './types'
import { defineComponent } from '@vue/composition-api'
import { useStore, useState, useActions, useMutations, useGetters } from 'vuex-hooks'

Expand All @@ -164,3 +170,78 @@ Doing this will result in the return values being correctly typed, so for exampl
supportedLocales === Readonly<Ref<Locale>>

```

You can also use the typings of your module directly. So exapnding on the previous example, lets assume there is a module that looks like so:

```typescript
interface User {
id: number
name: string
email: sring
company: string
}
interface UserState {
allUsers: User[]
currentUserId: number | null
}
const state: UserState = {
users: [],
currentUserId: null,
}

export default {
namespaced: true,
state,
actions: {
async getUsers(ctx: ActionContext<UserState, any>): Promise<void> {
try {
const response = await api.get('myapi/users/')
ctx.commit('SET_USERS', response.data)
} catch(error) {
...
}
},
async updateUser(ctx: ActionContext<UserState, any>, payload: Partial<User>): Promise<void | User> {
try {
const userId = ctx.state.currentUserId
const response = await api.patch(`myapi/users/${userId}/`)
return response.data
} catch(error) {
...
}
}
},
getters: {
getUserById(state: UserState) => (id: number): User | undefiend {
return state.users.find(user => user.id === id)
}
},
mutations: {
SET_USERS(state: UserState, payload: User[]): void {
state.users = payload
},
}
}
```

Obviously you already have typings in this case, and you can use these:

```typescript
import { defineComponent } from '@vue/composition-api'
import { useState, useActions, useMutations, useGetters } from 'vuex-hooks'
import UsersStore from './store'

export default defineComponent({
name: 'MyApp',
setup() {
const { currentUserId } = useState<typeof UsersStore['state']>('users') // ReadOnly<Ref<number | null>>
const { SET_USERS } = useMutations<typeof UsersStore['mutations']>('users') // (payload: User[]) => void
const { getUserById } = useActions<typeof UsersStore['actions']>('users') // (payload: Partial<User>) => Promise<void | User>
const { updateUser } = useGetters<typeof UsersStore['getters']>('users') // ReadOnly<Ref<(id: number) => User | undefiend>>

...
}
})
```

The plugin remaps the typings and strips the vuex specific arguments from actions and mutations, so for example `updateUser` will not be inferred as `(ctx: ActionContext<UserState, any>, payload: Partial<User>) => Promise<void | User>` but rather as `(payload: Partial<User>) => Promise<void | User>`. It also returns correctly curried functions from getters.

0 comments on commit 48b862a

Please sign in to comment.