Home > Software design >  How to describe something callable with properties in TypeScript? And how to call the below function
How to describe something callable with properties in TypeScript? And how to call the below function

Time:10-06

type DescribableFunction = {
  description: string;
  (someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
  console.log(fn.description   " returned "   fn(6));
}
console.log(doSomething({description="how are you", (9)=>return true;})) //error

I'm trying to call the above function with some arguments but I'm getting error as below

"This expression is not callable. Type '{ description: string; }' has no call signatures.(2349) (property) description: any"

How do I call this function?

CodePudding user response:

You need to give your function field a name so you can refer to it later. Also you have some syntax error while passing the object as parameter:

type DescribableFunction = {
  description: string;
  call: (someArg: number) => boolean;
};
function doSomething(fn: DescribableFunction) {
  console.log(fn.description   " returned "   fn.call(6));
}
console.log(doSomething({description: "how are you", call: (x: number) => true }))

playground

  • Related