简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM