UNPKG

@xoid/feature

Version:

A plugin system to compose JavaScript classes

28 lines (26 loc) 1.23 kB
type From = { <T extends new (...args: any) => any>(item: T): InstanceType<T>; <T extends { id: any; type: any; }>(item: T): T['type']; }; type FeatureConstructor = new (options: any, from?: any) => Feature<any>; type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; type GetKey<I, K extends PropertyKey> = I extends { [L in K]: infer P; } ? P : never; type ExtractKey<U, K extends PropertyKey> = GetKey<UnionToIntersection<U>, K>; type MergedTypes<P extends FeatureConstructor> = { [K in keyof UnionToIntersection<InstanceType<P>>]: ExtractKey<InstanceType<P>, K>; }; declare class Feature<T = {}> { id?: PropertyKey; externalOptions: T; options: T; from: From; getOptions: <U>(defaultOptions: U) => U & T; constructor(options: T, from?: From); } declare const compose: <P extends FeatureConstructor, V>(config: P[], getResult: (from: From, imaginary: MergedTypes<P>) => V) => (options: GetKey<UnionToIntersection<InstanceType<P>>, "externalOptions">, overrides?: FeatureConstructor[]) => V; export { ExtractKey, Feature, FeatureConstructor, From, UnionToIntersection, compose };