cooky-cutter
Version:
Object factories for testing in TypeScript
21 lines (20 loc) • 1.24 kB
TypeScript
import { Factory, AttributeFunction, ArrayFactory } from "./index";
import { DiffProperties, ArrayElement } from "./utils";
import { DerivedFunction } from "./derive";
declare type Merge<Base, Result> = DiffProperties<Result, Base> & Partial<Base>;
declare type ExtendConfig<Base, Result> = {
[Key in keyof Merge<Base, Result>]: Merge<Base, Result>[Key] | AttributeFunction<Merge<Base, Result>[Key]> | Factory<Merge<Base, Result>[Key]> | DerivedFunction<Result, Merge<Base, Result>[Key]> | ArrayFactory<ArrayElement<Merge<Base, Result>[Key]>>;
};
/**
* Define a new factory function from an existing factory. The return value is a
* function that can be invoked as many times as needed to create a given type
* of object. Use the config param to define how the object is generated on each
* invocation.
*
* @param base An existing factory to extend.
* @param config An object that defines how the factory should generate objects.
* Each key can either be a static value, a function that receives the
* invocation count as the only parameter or another factory.
*/
declare function extend<Base, Result extends Base>(base: Factory<Base>, config: ExtendConfig<Base, Result>): Factory<Result>;
export { extend, ExtendConfig };