Skip to content

Commit 5b44e8e

Browse files
authored
types: add typing support (#1717)
* types: add typing support * types: remove `ComponentCustomProperties`
1 parent df3f690 commit 5b44e8e

11 files changed

+66
-223
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ app.use(store)
4444
app.mount('#app')
4545
```
4646

47+
### Typings for `ComponentCustomProperties`
48+
49+
Vuex 4 removes its global typings for `this.$store` within Vue Component due to solving [issue #994](https://github.com/vuejs/vuex/issues/994). When using TypeScript, you must provide your own augment declaration.
50+
51+
Please place the following code in your project to have `this.$store` working.
52+
53+
```ts
54+
// vuex-shim.d.ts
55+
56+
declare module "@vue/runtime-core" {
57+
// Declare your own store states.
58+
interface State {
59+
count: number
60+
}
61+
62+
interface ComponentCustomProperties {
63+
$store: Store<State>;
64+
}
65+
}
66+
```
67+
4768
## Known issues
4869

4970
- The code is kept as close to Vuex 3 code base as possible, and there're plenty of places where we should refactor. However, we are waiting for all of the test cases to pass before doing so (some tests require Vue 3 update).

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"build:main": "node scripts/build-main.js",
2222
"build:logger": "node scripts/build-logger.js",
2323
"lint": "eslint src test",
24-
"test": "npm run lint && npm run test:unit && npm run test:ssr && npm run test:e2e",
24+
"test": "npm run lint && npm run test:unit && npm run test:ssr && npm run test:types && npm run test:e2e",
2525
"test:unit": "jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
2626
"test:e2e": "node test/e2e/runner.js",
2727
"test:ssr": "cross-env VUE_ENV=server jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
@@ -49,6 +49,7 @@
4949
"@rollup/plugin-commonjs": "^11.1.0",
5050
"@rollup/plugin-node-resolve": "^7.1.3",
5151
"@rollup/plugin-replace": "^2.3.1",
52+
"@types/node": "^13.13.0",
5253
"@vue/compiler-sfc": "3.0.0-beta.2",
5354
"babel-core": "^6.22.1",
5455
"babel-loader": "^7.1.2",
@@ -74,7 +75,7 @@
7475
"rollup": "^2.6.1",
7576
"rollup-plugin-terser": "^5.3.0",
7677
"todomvc-app-css": "^2.1.0",
77-
"typescript": "^3.7.2",
78+
"typescript": "^3.8.3",
7879
"vue": "3.0.0-beta.2",
7980
"vue-loader": "16.0.0-alpha.3",
8081
"vue-template-compiler": "^2.5.22",

types/helpers.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import Vue from 'vue';
1+
import { ComponentPublicInstance } from 'vue';
22
import { Dispatch, Commit } from './index';
33

44
type Computed = () => any;
55
type InlineComputed<T extends Function> = T extends (...args: any[]) => infer R ? () => R : never
66
type MutationMethod = (...args: any[]) => void;
77
type ActionMethod = (...args: any[]) => Promise<any>;
88
type InlineMethod<T extends (fn: any, ...args: any[]) => any> = T extends (fn: any, ...args: infer Args) => infer R ? (...args: Args) => R : never
9-
type CustomVue = Vue & Record<string, any>;
9+
type CustomVue = ComponentPublicInstance & Record<string, any>;
1010

1111
interface Mapper<R> {
1212
<Key extends string>(map: Key[]): { [K in Key]: R };

types/index.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _Vue, { WatchOptions } from "vue";
1+
import { App, WatchOptions, InjectionKey } from "vue";
22

33
// augment typings of Vue.js
44
import "./vue";
@@ -13,6 +13,8 @@ export declare class Store<S> {
1313
readonly state: S;
1414
readonly getters: any;
1515

16+
install(app: App, injectKey?: InjectionKey<Store<any>>): void;
17+
1618
replaceState(state: S): void;
1719

1820
dispatch: Dispatch;
@@ -39,7 +41,7 @@ export declare class Store<S> {
3941
}): void;
4042
}
4143

42-
export declare function install(Vue: typeof _Vue): void;
44+
export function createStore<S>(options: StoreOptions<S>): Store<S>;
4345

4446
export interface Dispatch {
4547
(type: string, payload?: any, options?: DispatchOptions): Promise<any>;
@@ -142,7 +144,6 @@ export interface ModuleTree<R> {
142144

143145
declare const _default: {
144146
Store: typeof Store;
145-
install: typeof install;
146147
mapState: typeof mapState,
147148
mapMutations: typeof mapMutations,
148149
mapGetters: typeof mapGetters,

types/test/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Vue from "vue";
1+
import { createApp } from "vue";
22

33
import {
44
mapState,
@@ -10,7 +10,7 @@ import {
1010

1111
const helpers = createNamespacedHelpers('foo');
1212

13-
new Vue({
13+
createApp({
1414
computed: {
1515
...mapState(["a"]),
1616
...mapState('foo', ["b"]),

types/test/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import Vue from "vue";
21
import * as Vuex from "../index";
32
import createLogger from "../../dist/logger";
43

5-
Vue.use(Vuex);
6-
74
namespace StoreInstance {
85
const store = new Vuex.Store({
96
state: {

types/test/tsconfig.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
4-
"module": "es2015",
3+
"target": "esnext",
4+
"module": "esnext",
55
"moduleResolution": "node",
66
"lib": [
7-
"es5",
8-
"dom",
9-
"es2015.promise",
10-
"es2015.core"
7+
"esnext",
8+
"dom"
9+
],
10+
"types": [
11+
"node"
1112
],
1213
"strict": true,
1314
"noEmit": true

types/test/vue.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Vue from "vue";
1+
import { createApp } from "vue";
22
import * as Vuex from "../index";
33

44
const store = new Vuex.Store({
@@ -7,8 +7,6 @@ const store = new Vuex.Store({
77
}
88
});
99

10-
const vm = new Vue({
10+
const app = createApp({
1111
store
1212
});
13-
14-
vm.$store.state.value;

types/tsconfig.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
4-
"module": "es2015",
3+
"target": "esnext",
4+
"module": "esnext",
55
"moduleResolution": "node",
66
"lib": [
7-
"es5",
8-
"dom",
9-
"es2015.promise"
7+
"esnext",
8+
"dom"
9+
],
10+
"types": [
11+
"node"
1012
],
1113
"strict": true,
1214
"noEmit": true

types/vue.d.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22
* Extends interfaces in Vue.js
33
*/
44

5-
import Vue, { ComponentOptions } from "vue";
5+
import { ComponentCustomOptions, ComponentCustomProperties } from "vue";
66
import { Store } from "./index";
77

8-
declare module "vue/types/options" {
9-
interface ComponentOptions<V extends Vue> {
8+
declare module "@vue/runtime-core" {
9+
interface ComponentCustomOptions {
1010
store?: Store<any>;
1111
}
1212
}
13-
14-
declare module "vue/types/vue" {
15-
interface Vue {
16-
$store: Store<any>;
17-
}
18-
}

0 commit comments

Comments
 (0)