Skip to content

Instantly share code, notes, and snippets.

@vhqtvn
vhqtvn / callback-to-promise.ts
Last active Jun 23, 2021
Callback to promise
View callback-to-promise.ts
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);
};
}
View vue-no-reactive.ts
/* 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 Jul 20, 2019
Hacky vuex-module-decorators fix for preserveState
View vuex-module-decorators.d.ts
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'
type ModulePreserveState = Module
export { Module, ModulePreserveState, VuexModule, Mutation, Action }