Skip to content

Commit 6730597

Browse files
ktsnyyx990803
authored andcommitted
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
1 parent 751660a commit 6730597

File tree

7 files changed

+88
-75
lines changed

7 files changed

+88
-75
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 { MutationObject, Plugin } from './types/index'
7+
8+
export interface LoggerOption<S> {
9+
collapsed?: boolean;
10+
transformer?: (state: S) => any;
11+
mutationTransformer?: (mutation: MutationObject<any>) => any;
12+
}
13+
14+
export default function createLogger<S>(option: LoggerOption<S>): Plugin<S>;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
"version": "1.0.0-rc.2",
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/vue.d.ts",
12+
"logger.js",
13+
"logger.d.ts"
1014
],
1115
"scripts": {
1216
"counter": "cd examples/counter && webpack-dev-server --inline --hot --config ../webpack.shared.config.js",

types/index.d.ts

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,66 @@
1-
declare namespace Vuex {
2-
class Store<S> {
3-
constructor(options: StoreOption<S>);
1+
import './vue'
42

5-
state: S;
3+
export class Store<S> {
4+
constructor(options: StoreOption<S>);
65

7-
dispatch(mutationName: string, ...args: any[]): void;
8-
dispatch<P>(mutation: MutationObject<P>): void;
6+
state: S;
97

10-
replaceState(state: S): void;
8+
dispatch(mutationName: string, ...args: any[]): void;
9+
dispatch<P>(mutation: MutationObject<P>): void;
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+
watch<T>(getter: Getter<S, T>, cb: (value: T) => void, options?: WatchOption): void;
1814

19-
subscribe(cb: (mutation: MutationObject<any>, state: S) => void): () => void;
20-
}
21-
22-
function install(Vue: vuejs.VueStatic): void;
23-
24-
interface StoreOption<S> {
25-
state?: S;
15+
hotUpdate(options: {
2616
mutations?: MutationTree<S>;
2717
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-
modules?: ModuleTree;
51-
}
18+
}): void;
5219

53-
interface ModuleTree {
54-
[key: string]: Module<any>;
55-
}
20+
subscribe(cb: (mutation: MutationObject<any>, state: S) => void): () => void;
21+
}
5622

57-
interface ComponentOption<S> {
58-
getters: { [key: string]: Getter<S, any> };
59-
actions: { [key: string]: Action<S> };
60-
}
23+
export function install(Vue: vuejs.VueStatic): void;
6124

62-
interface WatchOption {
63-
deep?: boolean;
64-
immidiate?: boolean;
65-
}
25+
export interface StoreOption<S> {
26+
state?: S;
27+
mutations?: MutationTree<S>;
28+
modules?: ModuleTree;
29+
plugins?: Plugin<S>[];
30+
strict?: boolean;
31+
}
32+
33+
type Getter<S, T> = (state: S) => T;
34+
type Action<S> = (store: Store<S>, ...args: any[]) => any;
35+
type Mutation<S> = (state: S, ...args: any[]) => void;
36+
type Plugin<S> = (store: Store<S>) => void;
6637

67-
function createLogger<S>(option: LoggerOption<S>): Plugin<S>;
38+
export interface MutationTree<S> {
39+
[key: string]: Mutation<S>;
40+
}
6841

69-
interface LoggerOption<S> {
70-
collapsed?: boolean;
71-
transformer?: (state: S) => any;
72-
mutationTransformer?: (mutation: MutationObject<any>) => any;
73-
}
42+
export interface MutationObject<P> {
43+
type: string;
44+
silent?: boolean;
45+
payload?: P;
7446
}
7547

76-
declare namespace vuejs {
77-
interface ComponentOption {
78-
vuex?: Vuex.ComponentOption<any>;
79-
store?: Vuex.Store<any>;
80-
}
48+
export interface Module<S> {
49+
state?: S;
50+
mutations?: MutationTree<S>;
51+
modules?: ModuleTree;
52+
}
8153

82-
interface Vue {
83-
$store?: Vuex.Store<any>;
84-
}
54+
export interface ModuleTree {
55+
[key: string]: Module<any>;
8556
}
8657

87-
declare module 'vuex' {
88-
export = Vuex
58+
export interface VuexComponentOption<S> {
59+
getters: { [key: string]: Getter<S, any> };
60+
actions: { [key: string]: Action<S> };
8961
}
9062

91-
declare module 'vuex/logger' {
92-
export default Vuex.createLogger;
63+
export interface WatchOption {
64+
deep?: boolean;
65+
immidiate?: boolean;
9366
}

types/test/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Vue from 'vue';
2-
import * as Vuex from 'vuex';
3-
import createLogger from 'vuex/logger';
2+
import * as Vuex from '../index';
3+
import createLogger from '../../logger';
44

55
Vue.use(Vuex);
66

types/test/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"files": [
88
"index.ts",
99
"../index.d.ts",
10+
"../../logger.d.ts",
11+
"../vue.d.ts",
1012
"../typings/index.d.ts"
1113
]
1214
}

types/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
},
77
"files": [
88
"index.d.ts",
9+
"../logger.d.ts",
10+
"vue.d.ts",
911
"typings/index.d.ts"
1012
]
1113
}

types/vue.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Extends interfaces in Vue.js
3+
*/
4+
5+
import { VuexComponentOption, Store } from './index'
6+
7+
declare global {
8+
namespace vuejs {
9+
interface ComponentOption {
10+
vuex?: VuexComponentOption<any>;
11+
store?: Store<any>;
12+
}
13+
14+
interface Vue {
15+
$store?: Store<any>;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)