Skip to content

Instantly share code, notes, and snippets.

@vhqtvn
vhqtvn / callback-to-promise.ts
Last active June 23, 2021 16:07
Callback to promise
function make_wrapper<T extends { [k: string]: any }>(root: T) {
type VHWithoutLastElement<T extends [...args: any]> = T extends [...args: infer P, last: any] ? P : never;
type VHLastElement<T extends [...args: any]> = T extends [...args: any, last: infer LastType] ? LastType : never;
type VHCallbackExtractValue<T extends any> = T extends ((err: any, value: infer ValueType) => void) ? (ValueType extends unknown ? void : ValueType) : never;
type VHPromisifier<T extends (...args: any) => any> = T extends (...args: infer P) => any ? ((...args: VHWithoutLastElement<P>) => Promise<VHCallbackExtractValue<VHLastElement<P>>>) : never;
return <fn_t extends keyof T>(fn: fn_t): VHPromisifier<T[fn_t]> => {
return promisify(root[fn]).bind(root);
};
}
/* tslint:disable */
export function NoObserver<T>(target: T): T {
if (target === null || typeof target === 'undefined') {
return target;
}
(target as any)._isVue = true;
return target;
}
@vhqtvn
vhqtvn / vuex-module-decorators.d.ts
Created July 20, 2019 03:10
Hacky vuex-module-decorators fix for preserveState
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'
type ModulePreserveState = Module
export { Module, ModulePreserveState, VuexModule, Mutation, Action }