From 623c526b4801377064c3975d0ca6b965b6360e5d Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Tue, 26 Dec 2017 21:47:43 +0200 Subject: [PATCH 1/2] Use keyof to improve type inference in helpers --- types/helpers.d.ts | 34 +++++++++++++++++++--------------- types/test/helpers.ts | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/types/helpers.d.ts b/types/helpers.d.ts index e86650485..84f7c04f2 100644 --- a/types/helpers.d.ts +++ b/types/helpers.d.ts @@ -1,43 +1,47 @@ import Vue from 'vue'; import { Dispatch, Commit } from './index'; -type Dictionary = { [key: string]: T }; +type CompleteObject = { [key: string]: string }; type Computed = () => any; type MutationMethod = (...args: any[]) => void; type ActionMethod = (...args: any[]) => Promise; interface Mapper { - (map: string[]): Dictionary; - (map: Dictionary): Dictionary; + (map: K[]): { [key in K]: R }; + (map: { [key in K]: string }): { [key in K]: R }; } interface MapperWithNamespace { - (namespace: string, map: string[]): Dictionary; - (namespace: string, map: Dictionary): Dictionary; + (namespace: string, map: K[]): { [key in K]: R }; + (namespace: string, map: { [key in K]: string }): { [key in K]: R }; } +type MappingFunction = (this: typeof Vue, fn: F, ...args: any[]) => any + interface FunctionMapper { - (map: Dictionary<(this: typeof Vue, fn: F, ...args: any[]) => any>): Dictionary; + (map: { [key in K]: MappingFunction }): { [key in K]: R }; } interface FunctionMapperWithNamespace { - ( + ( namespace: string, - map: Dictionary<(this: typeof Vue, fn: F, ...args: any[]) => any> - ): Dictionary; + map: { [key in K]: MappingFunction } + ): { [key in K]: R }; } +type StateMappingFunction = (this: typeof Vue, state: S, getters: any) => any + interface MapperForState { - ( - map: Dictionary<(this: typeof Vue, state: S, getters: any) => any> - ): Dictionary; + ( + map: { [key in K]: StateMappingFunction } + ): { [key in K]: Computed }; } interface MapperForStateWithNamespace { - ( + ( namespace: string, - map: Dictionary<(this: typeof Vue, state: S, getters: any) => any> - ): Dictionary; + map: { [key in K]: StateMappingFunction } + ): { [key in K]: Computed }; } interface NamespacedMappers { diff --git a/types/test/helpers.ts b/types/test/helpers.ts index 460b692a4..46fc81f74 100644 --- a/types/test/helpers.ts +++ b/types/test/helpers.ts @@ -5,7 +5,9 @@ import { mapGetters, mapActions, mapMutations, - createNamespacedHelpers + createNamespacedHelpers, + Commit, + Dispatch } from "../index"; const helpers = createNamespacedHelpers('foo'); @@ -62,7 +64,7 @@ new Vue({ h: "h" }), mapActions({ - g (dispatch, a: string, b: number, c: boolean): void { + g (dispatch: Dispatch, a: string, b: number, c: boolean): void { dispatch('g', { a, b, c }) dispatch({ type: 'g', @@ -77,7 +79,7 @@ new Vue({ h: "h" }), mapActions('foo', { - g (dispatch, a: string, b: number, c: boolean): void { + g (dispatch: Dispatch, a: string, b: number, c: boolean): void { dispatch('g', { a, b, c }) dispatch({ type: 'g', @@ -93,7 +95,7 @@ new Vue({ j: "j" }), mapMutations({ - i (commit, a: string, b: number, c: boolean): void { + i (commit: Commit, a: string, b: number, c: boolean): void { commit('i', { a, b, c }) commit({ type: 'i', @@ -108,7 +110,7 @@ new Vue({ j: "j" }), mapMutations('foo', { - i (commit, a: string, b: number, c: boolean): void { + i (commit: Commit, a: string, b: number, c: boolean): void { commit('i', { a, b, c }) commit({ type: 'i', @@ -124,7 +126,7 @@ new Vue({ m: "m" }), helpers.mapActions({ - m (dispatch, value: string) { + m (dispatch: Dispatch, value: string) { dispatch('m', value) } }), @@ -134,13 +136,35 @@ new Vue({ n: "n" }), helpers.mapMutations({ - n (commit, value: string) { + n (commit: Commit, value: string) { commit('m', value) } }), + helpers.mapMutations({ + n: "n", + m: "m" + }), { otherMethod () {} } ) }); + +const actions = mapActions({ + mAlias: "m" +}) + +actions.mAlias() + +const actionsNamespaced = mapActions('namespace', { + mAlias: "m" +}) + +actionsNamespaced.mAlias() + +const actionsNamespaced2 = helpers.mapActions({ + mAlias: "m" +}) + +actionsNamespaced2.mAlias() From 7434b4841fee7c6d8286d1a47f4e5f19ba552f08 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Thu, 4 Jan 2018 10:42:42 +0200 Subject: [PATCH 2/2] Add alias for dictionary with typed keys --- types/helpers.d.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/types/helpers.d.ts b/types/helpers.d.ts index 37ec38ff6..1b80ff05c 100644 --- a/types/helpers.d.ts +++ b/types/helpers.d.ts @@ -1,33 +1,35 @@ import Vue from 'vue'; import { Dispatch, Commit } from './index'; -type CompleteObject = { [key: string]: string }; +type Dictionary = { [key: string]: T }; type Computed = () => any; type MutationMethod = (...args: any[]) => void; type ActionMethod = (...args: any[]) => Promise; -type CustomVue = Vue & { [key: string]: any } +type CustomVue = Vue & Dictionary +type TypedDictionary = { [key in K]: V } +type CompleteObject = { [key: string]: string }; interface Mapper { - (map: K[]): { [key in K]: R }; - (map: { [key in K]: string }): { [key in K]: R }; + (map: K[]): TypedDictionary; + (map: { [key in K]: string }): TypedDictionary; } interface MapperWithNamespace { - (namespace: string, map: K[]): { [key in K]: R }; - (namespace: string, map: { [key in K]: string }): { [key in K]: R }; + (namespace: string, map: K[]): TypedDictionary; + (namespace: string, map: { [key in K]: string }): TypedDictionary; } type MappingFunction = (this: CustomVue, fn: F, ...args: any[]) => any interface FunctionMapper { - (map: { [key in K]: MappingFunction }): { [key in K]: R }; + (map: { [key in K]: MappingFunction }): TypedDictionary; } interface FunctionMapperWithNamespace { ( namespace: string, map: { [key in K]: MappingFunction } - ): { [key in K]: R }; + ): TypedDictionary; } type StateMappingFunction = (this: CustomVue, state: S, getters: any) => any @@ -35,14 +37,14 @@ type StateMappingFunction = (this: CustomVue, state: S, getters: any) => any interface MapperForState { ( map: { [key in K]: StateMappingFunction } - ): { [key in K]: Computed }; + ): TypedDictionary; } interface MapperForStateWithNamespace { ( namespace: string, map: { [key in K]: StateMappingFunction } - ): { [key in K]: Computed }; + ): TypedDictionary; } interface NamespacedMappers {