Skip to content

Commit

Permalink
types: add typing support
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Apr 18, 2020
1 parent b9b43f8 commit ff229e6
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 34 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build:main": "node build/build.main.js",
"build:logger": "rollup -c build/rollup.logger.config.js",
"lint": "eslint src test",
"test": "npm run lint && npm run test:unit && npm run test:ssr && npm run test:e2e",
"test": "npm run lint && npm run test:unit && npm run test:ssr && npm run test:types && npm run test:e2e",
"test:unit": "rollup -c build/rollup.dev.config.js && jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
"test:e2e": "node test/e2e/runner.js",
"test:ssr": "rollup -c build/rollup.dev.config.js && cross-env VUE_ENV=server jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
Expand All @@ -43,6 +43,7 @@
"vue": "3.0.0-beta.2"
},
"devDependencies": {
"@types/node": "^13.13.0",
"@vue/compiler-sfc": "3.0.0-beta.2",
"babel-core": "^6.22.1",
"babel-loader": "^7.1.2",
Expand All @@ -66,7 +67,7 @@
"rollup-plugin-replace": "^2.1.0",
"terser": "^3.17.0",
"todomvc-app-css": "^2.1.0",
"typescript": "^3.7.2",
"typescript": "^3.8.3",
"vue": "3.0.0-beta.2",
"vue-loader": "16.0.0-alpha.3",
"vue-template-compiler": "^2.5.22",
Expand Down
4 changes: 2 additions & 2 deletions types/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Vue from 'vue';
import { ComponentPublicInstance } from 'vue';
import { Dispatch, Commit } from './index';

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

interface Mapper<R> {
<Key extends string>(map: Key[]): { [K in Key]: R };
Expand Down
7 changes: 4 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _Vue, { WatchOptions } from "vue";
import { App, WatchOptions, InjectionKey } from "vue";

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

install(app: App, injectKey?: InjectionKey<Store<any>>): void;

replaceState(state: S): void;

dispatch: Dispatch;
Expand All @@ -36,7 +38,7 @@ export declare class Store<S> {
}): void;
}

export declare function install(Vue: typeof _Vue): void;
export function createStore<S>(options: StoreOptions<S>): Store<S>;

export interface Dispatch {
(type: string, payload?: any, options?: DispatchOptions): Promise<any>;
Expand Down Expand Up @@ -139,7 +141,6 @@ export interface ModuleTree<R> {

declare const _default: {
Store: typeof Store;
install: typeof install;
mapState: typeof mapState,
mapMutations: typeof mapMutations,
mapGetters: typeof mapGetters,
Expand Down
4 changes: 2 additions & 2 deletions types/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue from "vue";
import { createApp } from "vue";

import {
mapState,
Expand All @@ -10,7 +10,7 @@ import {

const helpers = createNamespacedHelpers('foo');

new Vue({
createApp({
computed: {
...mapState(["a"]),
...mapState('foo', ["b"]),
Expand Down
3 changes: 0 additions & 3 deletions types/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import Vue from "vue";
import * as Vuex from "../index";
import createLogger from "../../dist/logger";

Vue.use(Vuex);

namespace StoreInstance {
const store = new Vuex.Store({
state: {
Expand Down
13 changes: 7 additions & 6 deletions types/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": [
"es5",
"dom",
"es2015.promise",
"es2015.core"
"esnext",
"dom"
],
"types": [
"node"
],
"strict": true,
"noEmit": true
Expand Down
6 changes: 4 additions & 2 deletions types/test/vue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue from "vue";
import { createApp } from "vue";
import * as Vuex from "../index";

const store = new Vuex.Store({
Expand All @@ -7,8 +7,10 @@ const store = new Vuex.Store({
}
});

const vm = new Vue({
const app = createApp({
store
});

const vm = app.mount('#el')

vm.$store.state.value;
12 changes: 7 additions & 5 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": [
"es5",
"dom",
"es2015.promise"
"esnext",
"dom"
],
"types": [
"node"
],
"strict": true,
"noEmit": true
Expand Down
10 changes: 5 additions & 5 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
* Extends interfaces in Vue.js
*/

import Vue, { ComponentOptions } from "vue";
import { ComponentCustomOptions, ComponentCustomProperties } from "vue";
import { Store } from "./index";

declare module "vue/types/options" {
interface ComponentOptions<V extends Vue> {
declare module "@vue/runtime-core" {
interface ComponentCustomOptions {
store?: Store<any>;
}
}

declare module "vue/types/vue" {
interface Vue {
declare module "@vue/runtime-core" {
interface ComponentCustomProperties {
$store: Store<any>;
}
}
13 changes: 9 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.18.tgz#1d3ca764718915584fcd9f6344621b7672665c67"
integrity sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==

"@types/node@^13.13.0":
version "13.13.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.0.tgz#30d2d09f623fe32cde9cb582c7a6eda2788ce4a8"
integrity sha512-WE4IOAC6r/yBZss1oQGM5zs2D7RuKR6Q+w+X2SouPofnWn+LbCqClRyhO3ZE7Ix8nmFgo/oVuuE01cJT2XB13A==

"@vue/babel-preset-app@3.0.0-beta.11":
version "3.0.0-beta.11"
resolved "https://registry.yarnpkg.com/@vue/babel-preset-app/-/babel-preset-app-3.0.0-beta.11.tgz#c8b889aa73464050f9cd3f9dc621951d85c24508"
Expand Down Expand Up @@ -9746,10 +9751,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==
typescript@^3.8.3:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==

uc.micro@^1.0.1, uc.micro@^1.0.5:
version "1.0.5"
Expand Down

0 comments on commit ff229e6

Please sign in to comment.