通过TypeScript中的花括号声明方法

通过TypeScript中的花括号声明方法,typescript,Typescript,如何以这种方式读取方法声明(或编译器如何读取): 这像是为这个add方法创建一个type吗?这很奇怪,因为它读起来像add是一个只有一个函数的对象 上下文: interface ComponentProps { add: {(id: number): void}; } 然后,当某个React组件实现此功能时,我将写: <Component add={(item) => {console.log(`Added item ${item}`);} } /> {console.

如何以这种方式读取方法声明(或编译器如何读取):

这像是为这个
add
方法创建一个
type
吗?
这很奇怪,因为它读起来像
add
是一个只有一个函数的对象

上下文:

interface ComponentProps {
  add: {(id: number): void};
}
然后,当某个React组件实现此功能时,我将写:

<Component add={(item) => {console.log(`Added item ${item}`);} } />
{console.log(`Added item${item}`);}/>

add
是一个函数字段。我们通常在typescript中编写函数签名类型的方式是
(id:number)=>void
,但这是
{(id:number):void}
的一种简短形式。这两个语法是等价的,都定义了一个具有可调用签名的类型。这种更详细的语法具有允许更多签名的优点:

interface ComponentProps {
  add: {
     (id: number): void
     (id: string, otherParam: number): void
  };
}

你能提供更多的背景吗?如果
add
是一个对象属性,那么它看起来不像是一个有效的TS代码。据我所知,这个语法是无效的(如@zerkms所说)。它是一个有效的语法。你能提供一个样本@MuratK.@UmutOzel它是一个类方法。在这种情况下,它是有效的。这个问题本来可以更清楚一些,这样我们就不用猜测了。
interface ComponentProps {
  add: {
     (id: number): void
     (id: string, otherParam: number): void
  };
}