Skip to content

Commit d76fc43

Browse files
committed
🏷️ generate type definitions
1 parent 2ee2ef4 commit d76fc43

File tree

5 files changed

+56
-35
lines changed

5 files changed

+56
-35
lines changed

types/loadable.d.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import { LoadableMixinInstance } from './LoadableMixin';
22
/**
3-
* Decorate a function to causes loading states changes during its execution. It
4-
* set state as loading when function is init and unset on throws an error or
3+
* An union of any function and functions that have access to `this`
4+
* (Vue instance).
5+
*/
6+
export declare type Method = ((...args: any[]) => any) | ((this: LoadableMixinInstance, ...args: any[]) => any);
7+
/**
8+
* A Higher-order type to trasnform a method into loadable method that have
9+
* access to `this` (Vue instance) and returns a Promise.
10+
*/
11+
export declare type LoadableMethod<T extends Method> = (this: LoadableMixinInstance, ...args: Parameters<T>) => ReturnType<T> extends Promise<any> ? ReturnType<T> : Promise<ReturnType<T>>;
12+
/**
13+
* Decorate a method to causes loading states changes during its execution. It
14+
* sets state as loading when function is init and unsets on throws an error or
515
* resolve/return.
6-
* @example ```js
16+
* @example
717
* Vue.component('SignInForm', {
818
* methods: {
919
* signIn: loadable(async function ({ email, password }) {
1020
* // ...
1121
* }, 'signIn')
1222
* }
13-
* })```
14-
* @param λ - A function, commonly async, which causes loading state changes.
15-
* @param [state] - Loading state name.
23+
* });
24+
* @param method - A method, commonly async, which causes loading state changes.
25+
* @param [state] - Loading state name. It's "unknown" if not defined.
1626
*/
17-
export default function loadable<Return, Params extends any[]>(λ: (this: LoadableMixinInstance, ...params: Params) => Return | Promise<Return>, state?: string): (this: LoadableMixinInstance, ...params: Params) => Promise<Return>;
27+
declare const loadable: <T extends Method>(method: T, state?: string) => LoadableMethod<T>;
28+
export default loadable;

types/mapActions.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

types/mapLoadableActions.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

types/mapLoadableMethods.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Method, LoadableMethod } from './loadable';
2+
/**
3+
* Type of an object whose keys are `string` and the values are methods.
4+
*/
5+
export declare type Methods = Record<string, Method>;
6+
/**
7+
* A Higher-order type to transform methods into loadable methods. It keeps keys
8+
* as-is, but values have access to `this` (Vue instance) and returns a Promise.
9+
*/
10+
export declare type LoadableMethods<T extends Methods> = {
11+
[K in keyof T]: LoadableMethod<T[K]>;
12+
};
13+
/**
14+
* Maps an object, whose keys are `string` and the values are methods, to
15+
* loadable methods that triggers loading states. It uses property's keys as
16+
* loading state names.
17+
* @example
18+
* Vue.component('SignInForm', {
19+
* ...,
20+
* methods: {
21+
* onClick() {
22+
* if (this.$isLoading('signIn') || this.$isLoading('signUp'))
23+
* return;
24+
* // ...
25+
* },
26+
* ...mapLoadableMethods(
27+
* mapActions('authentication', [
28+
* 'signIn',
29+
* 'signUp'
30+
* ])
31+
* )
32+
* }
33+
* });
34+
*/
35+
declare const mapLoadableMethods: <T extends Record<string, Method>>(methods: T) => LoadableMethods<T>;
36+
export default mapLoadableMethods;

types/vue-loadable.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ declare module 'vue/types/vue' {
2424
}
2525
}
2626
export { LoadableMixin };
27-
export { default as loadable } from './loadable';
28-
export { default as mapLoadableActions } from './mapLoadableActions';
27+
export { default as loadable, Method, LoadableMethod } from './loadable';
28+
export { default as mapLoadableMethods, Methods, LoadableMethods } from './mapLoadableMethods';
2929
/**
3030
* Installs LoadableMixin globally.
3131
* @example ```js

0 commit comments

Comments
 (0)