'typescript type with call signature instantiation [duplicate]

While reading TypeScript documentation I stumbled upon this:

type DescribableFunction = {
  description: string;
  (someArg: number): boolean;
};

function doSomething(fn: DescribableFunction) {
  console.log(fn.description + " returned " + fn(6));
}

How can I create and instance of DescribableFunction?



Solution 1:[1]

Edit: here's the cleaner way: SO answer link

I don't see how you can do it without casting. I would create a separate function for creating the DescribableFunction:

function createDesirableFunction(description: string, func: (someArg: number) => boolean): DescribableFunction {
    const result = func;
    (result as DescribableFunction).description = description;
    return result as DescribableFunction;
}

Here it is in action: Typescript Playground

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1