//#region src/companion/Companion.d.ts /** * Creates a function-object hybrid similar to Scala's companion objects. * This utility allows creating TypeScript function objects with attached methods, * mimicking Scala's class + companion object pattern without using classes. * * @param object The main function that will be invoked when the object is called * @param companion Additional static methods to attach to the function * @returns A function with the attached methods * * @example * const greet = (name: string) => `Hello, ${name}!`; * const methods = { * formal: (name: string) => `Good day, ${name}.`, * casual: (name: string) => `Hey ${name}!` * }; * const Greeter = createCompanionObject(greet, methods); * * // Usage: * Greeter("World"); // Hello, World! * Greeter.formal("Sir"); // Good day, Sir. * Greeter.casual("Friend"); // Hey Friend! */ declare function Companion(object: ObjectF, companion: CompanionF): ObjectF & CompanionF; //#endregion //#region src/companion/CompanionTypes.d.ts /** * Helper types for working with the Companion pattern * @module CompanionTypes */ /** * Extracts the companion methods type from a Companion object * @typeParam T - The Companion type * @example * ```typescript * type OptionCompanionMethods = CompanionMethods * // { from: ..., none: ..., fromJSON: ..., etc. } * ``` */ type CompanionMethods = T extends ((...args: never[]) => unknown) & (infer C) ? C : never; /** * Extracts the instance type from a constructor function * @typeParam T - The constructor function type * @example * ```typescript * type OptionInstance = InstanceType * // Option * ``` */ type InstanceType = T extends ((...args: infer Args) => infer R) ? R extends ((...args: unknown[]) => unknown) ? ReturnType : R : never; /** * Type guard to check if a value is a Companion object (has both constructor and companion methods) * @param value - The value to check * @returns True if value is a Companion object */ declare const isCompanion: (value: unknown) => value is ((...args: never[]) => unknown) & Record; //#endregion export { Companion as i, InstanceType as n, isCompanion as r, CompanionMethods as t };