foibles
Version:
Composition and mixins and TypeScript classes
40 lines • 1.34 kB
TypeScript
/**
* Any function that returns `A`.
*/
export declare type AnyFunction<A> = (...input: any[]) => A;
/**
* Any constructor that creates `A`.
*/
export declare type AnyConstructor<A> = new (...input: any[]) => A;
/**
* Type generator for mixins. Use together with the result of `toMixin` to
* create a useable type:
*
* ```typescript
* type ExampleMixin = Mixin<typeof ExampleMixin>;
* const ExampleMixin = toMixin((base: object) => class extends base {
* ...
* });
* ```
*/
export declare type Mixin<T extends AnyFunction<any>> = InstanceType<ReturnType<T>>;
/**
* Function that mixes in functionality on top of a class. Will receive the
* base class to be used and must return an extended class.
*/
export declare type MixinFunction<T extends AnyConstructor<any>, O extends AnyConstructor<any>> = (base: T) => O;
/**
* Turn a function into a fully featured mixin. The mixin function should be
* a function which takes a single argument which is the class to extend and
* the function must return an extended class.
*
* ```javascript
* const ExampleMixin = toMixin(base => class extends base {
* ...
* });
* ```
*
* @param func
*/
export declare function toMixin<T extends AnyConstructor<any>, O extends AnyConstructor<any>>(func: MixinFunction<T, O>): MixinFunction<T, O>;
//# sourceMappingURL=mixin.d.ts.map