函数定义参数中的那些花括号是什么?

时间:2019-07-23 12:32:38

标签: typescript destructuring

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

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

有人可以解释这部分吗?还是有更好的方法来表达不同的语法?

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

1 个答案:

答案 0 :(得分:1)

好吧,handleSave函数将复杂类型作为输入参数,并返回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上进行销毁的更多信息。