Skip to content

Commit 8cd5a70

Browse files
ktsnyyx990803
authored andcommitted
TypeScript declarations for 2.0 (#301)
* Modify type files to external module definition format to publish them via npm (#242) * Rewrite d.ts in external module definition format * Publish type files via npm * update d.ts for 2.0 * update d.ts tests for 2.0 * provide helpers.d.ts via npm
1 parent de1c7dc commit 8cd5a70

File tree

9 files changed

+396
-223
lines changed

9 files changed

+396
-223
lines changed

logger.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Types for the logger plugin.
3+
* This file must be put alongside the JavaScript file of the logger.
4+
*/
5+
6+
import { Payload, Plugin } from './types/index'
7+
8+
export interface LoggerOption<S> {
9+
collapsed?: boolean;
10+
transformer?: (state: S) => any;
11+
mutationTransformer?: <P extends Payload>(mutation: P) => any;
12+
}
13+
14+
export default function createLogger<S>(option: LoggerOption<S>): Plugin<S>;

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
"version": "2.0.0-rc.5",
44
"description": "state management for Vue.js",
55
"main": "dist/vuex.js",
6+
"typings": "types/index.d.ts",
67
"files": [
78
"dist",
89
"src",
9-
"logger.js"
10+
"types/index.d.ts",
11+
"types/helpers.d.ts",
12+
"types/vue.d.ts",
13+
"logger.js",
14+
"logger.d.ts"
1015
],
1116
"scripts": {
1217
"dev": "node examples/server.js",

types/helpers.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type Dictionary<T> = { [key: string]: T };
2+
3+
export function mapState (map: string[]): Dictionary<() => any>;
4+
export function mapState (map: Dictionary<string>): Dictionary<() => any>;
5+
export function mapState <S>(
6+
map: Dictionary<(this: vuejs.Vue, state: S, getters: any) => any>
7+
): Dictionary<() => any>;
8+
9+
type MutationMethod = (...args: any[]) => void;
10+
export function mapMutations (map: string[]): Dictionary<MutationMethod>;
11+
export function mapMutations (map: Dictionary<string>): Dictionary<MutationMethod>;
12+
13+
export function mapGetters (map: string[]): Dictionary<() => any>;
14+
export function mapGetters (map: Dictionary<string>): Dictionary<() => any>;
15+
16+
type ActionMethod = (...args: any[]) => Promise<any[]>;
17+
export function mapActions (map: string[]): Dictionary<ActionMethod>;
18+
export function mapActions (map: Dictionary<string>): Dictionary<ActionMethod>;

types/index.d.ts

Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,103 @@
1-
declare namespace Vuex {
2-
class Store<S> {
3-
constructor(options: StoreOption<S>);
1+
import "./vue";
42

5-
state: S;
3+
export * from "./helpers";
64

7-
dispatch(mutationName: string, ...args: any[]): void;
8-
dispatch<P>(mutation: MutationObject<P>): void;
5+
export declare class Store<S> {
6+
constructor(options: StoreOptions<S>);
97

10-
replaceState(state: S): void;
8+
readonly state: S;
9+
readonly getters: any;
1110

12-
watch<T>(getter: Getter<S, T>, cb: (value: T) => void, options?: WatchOption): void;
11+
replaceState(state: S): void;
1312

14-
hotUpdate(options: {
15-
mutations?: MutationTree<S>;
16-
modules?: ModuleTree;
17-
}): void;
13+
dispatch: Dispatch;
14+
commit: Commit;
1815

19-
subscribe(cb: (mutation: MutationObject<any>, state: S) => void): () => void;
20-
}
16+
subscribe<P extends Payload>(fn: (mutation: P, state: S) => any): () => void;
17+
watch<T>(getter: (state: S) => T, cb: (value: T) => void, options?: WatchOption): void;
2118

22-
function install(Vue: vuejs.VueStatic): void;
19+
registerModule<T>(path: string, module: Module<T, S>): void;
20+
registerModule<T>(path: string[], module: Module<T, S>): void;
2321

24-
interface StoreOption<S> {
25-
state?: S;
22+
unregisterModule(path: string): void;
23+
unregisterModule(path: string[]): void;
24+
25+
hotUpdate(options: {
26+
actions?: ActionTree<S, S>;
2627
mutations?: MutationTree<S>;
27-
modules?: ModuleTree;
28-
plugins?: Plugin<S>[];
29-
strict?: boolean;
30-
}
31-
32-
type Getter<S, T> = (state: S) => T;
33-
type Action<S> = (store: Store<S>, ...args: any[]) => any;
34-
type Mutation<S> = (state: S, ...args: any[]) => void;
35-
type Plugin<S> = (store: Store<S>) => void;
36-
37-
interface MutationTree<S> {
38-
[key: string]: Mutation<S>;
39-
}
40-
41-
interface MutationObject<P> {
42-
type: string;
43-
silent?: boolean;
44-
payload?: P;
45-
}
46-
47-
interface Module<S> {
48-
state: S;
49-
mutations: MutationTree<S>;
50-
}
51-
52-
interface ModuleTree {
53-
[key: string]: Module<any>;
54-
}
55-
56-
interface ComponentOption<S> {
57-
getters: { [key: string]: Getter<S, any> };
58-
actions: { [key: string]: Action<S> };
59-
}
60-
61-
interface WatchOption {
62-
deep?: boolean;
63-
immidiate?: boolean;
64-
}
65-
66-
function createLogger<S>(option: LoggerOption<S>): Plugin<S>;
67-
68-
interface LoggerOption<S> {
69-
collapsed?: boolean;
70-
transformer?: (state: S) => any;
71-
mutationTransformer?: (mutation: MutationObject<any>) => any;
72-
}
28+
getters?: GetterTree<S, S>;
29+
modules?: ModuleTree<S>;
30+
}): void;
31+
}
32+
33+
export declare function install(Vue: vuejs.VueStatic): void;
34+
35+
export interface Dispatch {
36+
(type: string, payload?: any): Promise<any[]>;
37+
<P extends Payload>(payloadWithType: P): Promise<any[]>;
38+
}
39+
40+
export interface Commit {
41+
(type: string, payload?: any, options?: CommitOptions): void;
42+
<P extends Payload>(payloadWithType: P, options?: CommitOptions): void;
43+
}
44+
45+
export interface ActionInjectee<S, R> {
46+
dispatch: Dispatch;
47+
commit: Commit;
48+
state: S;
49+
getters: any;
50+
rootState: R;
51+
}
52+
53+
export interface Payload {
54+
type: string;
7355
}
7456

75-
declare namespace vuejs {
76-
interface ComponentOption {
77-
vuex?: Vuex.ComponentOption<any>;
78-
store?: Vuex.Store<any>;
79-
}
57+
export interface CommitOptions {
58+
silent?: boolean;
59+
}
60+
61+
export interface StoreOptions<S> {
62+
state?: S;
63+
getters?: GetterTree<S, S>;
64+
actions?: ActionTree<S, S>;
65+
mutations?: MutationTree<S>;
66+
modules?: ModuleTree<S>;
67+
plugins?: Plugin<S>[];
68+
strict?: boolean;
69+
}
70+
71+
export type Getter<S, R> = (state: S, getters: any, rootState: R) => any;
72+
export type Action<S, R> = (injectee: ActionInjectee<S, R>, payload: any) => any;
73+
export type Mutation<S> = (state: S, payload: any) => any;
74+
export type Plugin<S> = (store: Store<S>) => any;
75+
76+
export interface Module<S, R> {
77+
state?: S;
78+
getters?: GetterTree<S, R>;
79+
actions?: ActionTree<S, R>;
80+
mutations?: MutationTree<S>;
81+
modules?: ModuleTree<R>;
82+
}
83+
84+
export interface GetterTree<S, R> {
85+
[key: string]: Getter<S, R>;
86+
}
87+
88+
export interface ActionTree<S, R> {
89+
[key: string]: Action<S, R>;
90+
}
8091

81-
interface Vue {
82-
$store?: Vuex.Store<any>;
83-
}
92+
export interface MutationTree<S> {
93+
[key: string]: Mutation<S>;
8494
}
8595

86-
declare module 'vuex' {
87-
export = Vuex
96+
export interface ModuleTree<R> {
97+
[key: string]: Module<any, R>;
8898
}
8999

90-
declare module 'vuex/logger' {
91-
export default Vuex.createLogger;
100+
export interface WatchOption {
101+
deep?: boolean;
102+
immediate?: boolean;
92103
}

types/test/helpers.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as Vue from "vue";
2+
3+
import {
4+
mapState,
5+
mapGetters,
6+
mapActions,
7+
mapMutations
8+
} from "../index";
9+
10+
new Vue({
11+
computed: Object.assign({},
12+
mapState(["a"]),
13+
mapState({
14+
b: "b"
15+
}),
16+
mapState({
17+
c: (state: any, getters: any) => state.c + getters.c
18+
}),
19+
20+
mapGetters(["d"]),
21+
mapGetters({
22+
e: "e"
23+
}),
24+
25+
{
26+
otherComputed () {
27+
return "f";
28+
}
29+
}
30+
),
31+
32+
methods: Object.assign({},
33+
mapActions(["g"]),
34+
mapActions({
35+
h: "h"
36+
}),
37+
38+
mapMutations(["i"]),
39+
mapMutations({
40+
j: "j"
41+
}),
42+
43+
{
44+
otherMethod () {}
45+
}
46+
)
47+
});

0 commit comments

Comments
 (0)