Skip to content

Commit 7ad573b

Browse files
authored
fix: add missing typings and docs of createNamespacedHelpers (#910)
* fix(typings): add missing createNamespacedHelpers typings * docs(en): add a description of createNamespacedHelpers * fix(typings): use interface rather than type alias
1 parent a8c76e4 commit 7ad573b

File tree

4 files changed

+103
-28
lines changed

4 files changed

+103
-28
lines changed

docs/en/api.md

+4
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,7 @@ const store = new Vuex.Store({ ...options })
193193
Create component methods options that commit a mutation. [Details](mutations.md#commiting-mutations-in-components)
194194
195195
The first argument can optionally be a namespace string. [Details](modules.md#binding-helpers-with-namespace)
196+
197+
- **`createNamespacedHelpers(namespace: string): Object`**
198+
199+
Create namespaced component binding helpers. The returned object contains `mapState`, `mapGetters`, `mapActions` and `mapMutations` that are bound with the given namespace. [Details](modules.md#binding-helpers-with-namespace)

docs/en/modules.md

+25
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,31 @@ methods: {
206206
}
207207
```
208208

209+
Furthermore, you can create namespaced helpers by using `createNamespacedHelpers`. It returns an object having new component binding helpers that are bound with the given namespace value:
210+
211+
``` js
212+
import { createNamespacedHelpers } from 'vuex'
213+
214+
const { mapState, mapActions } = createNamespacedHelpers('some/nested/module')
215+
216+
export default {
217+
computed: {
218+
// look up in `some/nested/module`
219+
...mapState({
220+
a: state => state.a,
221+
b: state => state.b
222+
})
223+
},
224+
methods: {
225+
// look up in `some/nested/module`
226+
...mapActions([
227+
'foo',
228+
'bar'
229+
])
230+
}
231+
}
232+
```
233+
209234
#### Caveat for Plugin Developers
210235

211236
You may care about unpredictable namespacing for your modules when you create a [plugin](plugins.md) that provides the modules and let users add them to a Vuex store. Your modules will be also namespaced if the plugin users add your modules under a namespaced module. To adapt this situation, you may need to receive a namespace value via your plugin option:

types/helpers.d.ts

+47-27
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
11
import Vue = require("vue");
22

33
type Dictionary<T> = { [key: string]: T };
4+
type Computed = () => any;
5+
type MutationMethod = (...args: any[]) => void;
6+
type ActionMethod = (...args: any[]) => Promise<any>;
47

5-
export function mapState (map: string[]): Dictionary<() => any>;
6-
export function mapState (namespace: string, map: string[]): Dictionary<() => any>;
7-
export function mapState (map: Dictionary<string>): Dictionary<() => any>;
8-
export function mapState (namespace: string, map: Dictionary<string>): Dictionary<() => any>;
9-
export function mapState <S>(
10-
map: Dictionary<(this: typeof Vue, state: S, getters: any) => any>
11-
): Dictionary<() => any>;
12-
export function mapState <S>(
13-
namespace: string,
14-
map: Dictionary<(this: typeof Vue, state: S, getters: any) => any>
15-
): Dictionary<() => any>;
8+
interface Mapper<R> {
9+
(map: string[]): Dictionary<R>;
10+
(map: Dictionary<string>): Dictionary<R>;
11+
}
1612

17-
type MutationMethod = (...args: any[]) => void;
18-
export function mapMutations (map: string[]): Dictionary<MutationMethod>;
19-
export function mapMutations (namespace: string, map: string[]): Dictionary<MutationMethod>;
20-
export function mapMutations (map: Dictionary<string>): Dictionary<MutationMethod>;
21-
export function mapMutations (namespace: string, map: Dictionary<string>): Dictionary<MutationMethod>;
22-
23-
export function mapGetters (map: string[]): Dictionary<() => any>;
24-
export function mapGetters (namespace: string, map: string[]): Dictionary<() => any>;
25-
export function mapGetters (map: Dictionary<string>): Dictionary<() => any>;
26-
export function mapGetters (namespace: string, map: Dictionary<string>): Dictionary<() => any>;
27-
28-
type ActionMethod = (...args: any[]) => Promise<any[]>;
29-
export function mapActions (map: string[]): Dictionary<ActionMethod>;
30-
export function mapActions (namespace: string, map: string[]): Dictionary<ActionMethod>;
31-
export function mapActions (map: Dictionary<string>): Dictionary<ActionMethod>;
32-
export function mapActions (namespace: string, map: Dictionary<string>): Dictionary<ActionMethod>;
13+
interface MapperWithNamespace<R> {
14+
(namespace: string, map: string[]): Dictionary<R>;
15+
(namespace: string, map: Dictionary<string>): Dictionary<R>;
16+
}
17+
18+
interface MapperForState {
19+
<S>(
20+
map: Dictionary<(this: typeof Vue, state: S, getters: any) => any>
21+
): Dictionary<Computed>;
22+
}
23+
24+
interface MapperForStateWithNamespace {
25+
<S>(
26+
namespace: string,
27+
map: Dictionary<(this: typeof Vue, state: S, getters: any) => any>
28+
): Dictionary<Computed>;
29+
}
30+
31+
interface NamespacedMappers {
32+
mapState: Mapper<Computed> & MapperForState;
33+
mapMutations: Mapper<MutationMethod>;
34+
mapGetters: Mapper<Computed>;
35+
mapActions: Mapper<ActionMethod>;
36+
}
37+
38+
export declare const mapState: Mapper<Computed>
39+
& MapperWithNamespace<Computed>
40+
& MapperForState
41+
& MapperForStateWithNamespace;
42+
43+
export declare const mapMutations: Mapper<MutationMethod>
44+
& MapperWithNamespace<MutationMethod>;
45+
46+
export declare const mapGetters: Mapper<Computed>
47+
& MapperWithNamespace<Computed>;
48+
49+
export declare const mapActions: Mapper<ActionMethod>
50+
& MapperWithNamespace<ActionMethod>;
51+
52+
export declare function createNamespacedHelpers(namespace: string): NamespacedMappers;

types/test/helpers.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import {
44
mapState,
55
mapGetters,
66
mapActions,
7-
mapMutations
7+
mapMutations,
8+
createNamespacedHelpers
89
} from "../index";
910

11+
const helpers = createNamespacedHelpers('foo');
12+
1013
new Vue({
1114
computed: Object.assign({},
1215
mapState(["a"]),
@@ -33,6 +36,19 @@ new Vue({
3336
e: "e"
3437
}),
3538

39+
helpers.mapState(["k"]),
40+
helpers.mapState({
41+
k: "k"
42+
}),
43+
helpers.mapState({
44+
k: (state: any, getters: any) => state.k + getters.k
45+
}),
46+
47+
helpers.mapGetters(["l"]),
48+
helpers.mapGetters({
49+
l: "l"
50+
}),
51+
3652
{
3753
otherComputed () {
3854
return "f";
@@ -59,6 +75,16 @@ new Vue({
5975
j: "j"
6076
}),
6177

78+
helpers.mapActions(["m"]),
79+
helpers.mapActions({
80+
m: "m"
81+
}),
82+
83+
helpers.mapMutations(["n"]),
84+
helpers.mapMutations({
85+
n: "n"
86+
}),
87+
6288
{
6389
otherMethod () {}
6490
}

0 commit comments

Comments
 (0)