Skip to content

Use keyof to improve type inference in helpers #1105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions types/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,45 @@ type Computed = () => any;
type MutationMethod = (...args: any[]) => void;
type ActionMethod = (...args: any[]) => Promise<any>;
type CustomVue = Vue & Dictionary<any>
type TypedDictionary<K extends string, V> = { [key in K]: V }
type CompleteObject = { [key: string]: string };

interface Mapper<R> {
(map: string[]): Dictionary<R>;
(map: Dictionary<string>): Dictionary<R>;
<T extends CompleteObject, K extends keyof T>(map: K[]): TypedDictionary<K, R>;
<T extends CompleteObject, K extends keyof T>(map: { [key in K]: string }): TypedDictionary<K, R>;
}

interface MapperWithNamespace<R> {
(namespace: string, map: string[]): Dictionary<R>;
(namespace: string, map: Dictionary<string>): Dictionary<R>;
<T extends CompleteObject, K extends keyof T>(namespace: string, map: K[]): TypedDictionary<K, R>;
<T extends CompleteObject, K extends keyof T>(namespace: string, map: { [key in K]: string }): TypedDictionary<K, R>;
}

type MappingFunction<F> = (this: CustomVue, fn: F, ...args: any[]) => any

interface FunctionMapper<F, R> {
(map: Dictionary<(this: CustomVue, fn: F, ...args: any[]) => any>): Dictionary<R>;
<T extends CompleteObject, K extends keyof T>(map: { [key in K]: MappingFunction<F> }): TypedDictionary<K, R>;
}

interface FunctionMapperWithNamespace<F, R> {
(
<T extends CompleteObject, K extends keyof T>(
namespace: string,
map: Dictionary<(this: CustomVue, fn: F, ...args: any[]) => any>
): Dictionary<R>;
map: { [key in K]: MappingFunction<F> }
): TypedDictionary<K, R>;
}

type StateMappingFunction<S> = (this: CustomVue, state: S, getters: any) => any

interface MapperForState {
<S>(
map: Dictionary<(this: CustomVue, state: S, getters: any) => any>
): Dictionary<Computed>;
<S, T extends CompleteObject, K extends keyof T>(
map: { [key in K]: StateMappingFunction<S> }
): TypedDictionary<K, Computed>;
}

interface MapperForStateWithNamespace {
<S>(
<S, T extends CompleteObject, K extends keyof T>(
namespace: string,
map: Dictionary<(this: CustomVue, state: S, getters: any) => any>
): Dictionary<Computed>;
map: { [key in K]: StateMappingFunction<S> }
): TypedDictionary<K, Computed>;
}

interface NamespacedMappers {
Expand Down
38 changes: 31 additions & 7 deletions types/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
mapGetters,
mapActions,
mapMutations,
createNamespacedHelpers
createNamespacedHelpers,
Commit,
Dispatch
} from "../index";

const helpers = createNamespacedHelpers('foo');
Expand Down Expand Up @@ -65,7 +67,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',
Expand All @@ -80,7 +82,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',
Expand All @@ -96,7 +98,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',
Expand All @@ -111,7 +113,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',
Expand All @@ -127,7 +129,7 @@ new Vue({
m: "m"
}),
helpers.mapActions({
m (dispatch, value: string) {
m (dispatch: Dispatch, value: string) {
dispatch('m', value)
}
}),
Expand All @@ -137,13 +139,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()