Skip to content

Commit 93b5b5f

Browse files
committed
Add alias for dictionary with typed keys
1 parent 0af686f commit 93b5b5f

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

types/helpers.d.ts

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

4+
type TypedDictionary<K extends string, V> = { [key in K]: V }
5+
type Dictionary<T> = { [key: string]: T };
46
type CompleteObject = { [key: string]: string };
57
type Computed = () => any;
68
type MutationMethod = (...args: any[]) => void;
79
type ActionMethod = (...args: any[]) => Promise<any>;
8-
type CustomVue = Vue & { [key: string]: any }
10+
type CustomVue = Vue & Dictionary<any>
911

1012
interface Mapper<R> {
11-
<T extends CompleteObject, K extends keyof T>(map: K[]): { [key in K]: R };
12-
<T extends CompleteObject, K extends keyof T>(map: { [key in K]: string }): { [key in K]: R };
13+
<T extends CompleteObject, K extends keyof T>(map: K[]): TypedDictionary<K, R>;
14+
<T extends CompleteObject, K extends keyof T>(map: { [key in K]: string }): TypedDictionary<K, R>;
1315
}
1416

1517
interface MapperWithNamespace<R> {
16-
<T extends CompleteObject, K extends keyof T>(namespace: string, map: K[]): { [key in K]: R };
17-
<T extends CompleteObject, K extends keyof T>(namespace: string, map: { [key in K]: string }): { [key in K]: R };
18+
<T extends CompleteObject, K extends keyof T>(namespace: string, map: K[]): TypedDictionary<K, R>;
19+
<T extends CompleteObject, K extends keyof T>(namespace: string, map: { [key in K]: string }): TypedDictionary<K, R>;
1820
}
1921

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

2224
interface FunctionMapper<F, R> {
23-
<T extends CompleteObject, K extends keyof T>(map: { [key in K]: MappingFunction<F> }): { [key in K]: R };
25+
<T extends CompleteObject, K extends keyof T>(map: { [key in K]: MappingFunction<F> }): TypedDictionary<K, R>;
2426
}
2527

2628
interface FunctionMapperWithNamespace<F, R> {
2729
<T extends CompleteObject, K extends keyof T>(
2830
namespace: string,
2931
map: { [key in K]: MappingFunction<F> }
30-
): { [key in K]: R };
32+
): TypedDictionary<K, R>;
3133
}
3234

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

3537
interface MapperForState {
3638
<S, T extends CompleteObject, K extends keyof T>(
3739
map: { [key in K]: StateMappingFunction<S> }
38-
): { [key in K]: Computed };
40+
): TypedDictionary<K, Computed>;
3941
}
4042

4143
interface MapperForStateWithNamespace {
4244
<S, T extends CompleteObject, K extends keyof T>(
4345
namespace: string,
4446
map: { [key in K]: StateMappingFunction<S> }
45-
): { [key in K]: Computed };
47+
): TypedDictionary<K, Computed>;
4648
}
4749

4850
interface NamespacedMappers {

0 commit comments

Comments
 (0)