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?
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