FuncType - v0.8.64
    Preparing search index...

    Function Companion

    • 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.

      Type Parameters

      • ObjectF extends object
      • CompanionF extends object

      Parameters

      • object: ObjectF

        The main function that will be invoked when the object is called

      • companion: CompanionF

        Additional static methods to attach to the function

      Returns ObjectF & CompanionF

      A function with the attached methods

      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!