函数定义参数中的大括号是什么?

What are those curly brackets within a function definition parameter?

我有一个 Angular 5 项目,我看到了这个 Typescript 代码。

(method) CreateFlightComponent.handleSave({ currentValue, stepIndex }: {
    currentValue: Partial<Flight>;
    stepIndex: number;
}): void

谁能解释一下这部分?或者有什么更好的方式用不同的语法来表达?

{ currentValue, stepIndex }: {
    currentValue: Partial<Flight>;
    stepIndex: number;
} 

嗯,handleSave 函数将复杂类型作为输入参数,returns void。输入类型包含两个属性:

  • currentValue 的类型是 Partial<Flight>
  • stepIndex 属于 number.
  • 类型

另一种用不同语法表达相同内容的方法是:

interface IHandleTypeParams {
    currentValue: Partial<Flight>;
    stepIndex: number;
}

然后改用界面:

CreateFlightComponent.handleSave( input:IHandleTypeParams): void

或解构:

CreateFlightComponent.handleSave({ currentValue, stepIndex }: IHandleTypeParams): void

参见playground

MDN 阅读更多关于解构的信息。