简体   繁体   English

如何描述 TypeScript 中可调用的属性? 以及如何调用下面的function?

[英]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我试图用一些 arguments 调用上面的 function 但我收到如下错误

"This expression is not callable. Type '{ description: string; }' has no call signatures.(2349) (property) description: any" “此表达式不可调用。类型‘{描述:字符串;}’没有调用签名。(2349)(属性)描述:任何”

How do I call this function?我怎么称呼这个 function?

You need to give your function field a name so you can refer to it later.您需要为 function 字段命名,以便稍后引用。 Also you have some syntax error while passing the object as parameter:在将 object 作为参数传递时,您也有一些语法错误:

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 操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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