@replygirl/vanity
Version:
simple, reactive state management for any framework using @vue/reactivity
20 lines (19 loc) • 1.26 kB
TypeScript
import type { Dictionary } from 'dictionary-types';
import type { DeepReadonly, Ref } from '@vue/reactivity';
declare type Commit<S> = (payload: Partial<S>) => void;
interface Context<S> {
clear: () => void;
commit: Commit<S>;
state: S;
}
declare type Method = (...args: any[]) => any;
declare type Methods<S, M> = M extends Dictionary<Method> ? (ctx: Context<S>) => M : never;
declare type Nullable<T> = T | null;
interface ServiceOptions<S, M extends Dictionary<Method> | undefined> {
name: string;
baseState: S;
methods?: Methods<S, M>;
}
declare type State<S> = Dictionary<Nullable<Exclude<S[keyof S] extends NonNullable<S[keyof S]> ? never : S[keyof S], Ref | undefined>>>;
declare const createService: <S extends State<S> = any, M extends Dictionary<Method, string> = Dictionary<Method, string>>({ name, baseState, methods }: ServiceOptions<S, M>) => DeepReadonly<(S & M extends Ref<any> ? Ref<any> & S & M : import("@vue/reactivity").UnwrapRef<S & M>) extends Ref<any> ? Ref<any> & (S & M extends Ref<any> ? Ref<any> & S & M : import("@vue/reactivity").UnwrapRef<S & M>) : import("@vue/reactivity").UnwrapRef<S & M extends Ref<any> ? Ref<any> & S & M : import("@vue/reactivity").UnwrapRef<S & M>>>;
export default createService;