4

I have an Angular 5 project and I saw this Typescript code.

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

Could anyone explain this part? Or is there any better way to express with a different syntax?

{ currentValue, stepIndex }: {
    currentValue: Partial<Flight>;
    stepIndex: number;
} 
Kyll
  • 7,036
  • 7
  • 41
  • 64
knowledgeseeker
  • 339
  • 4
  • 14
  • 1
    This is indeed parameter destructuring; the `currentValue` and `stepIndex` inside the first brackets are destructured into those arguments. After the colon the type is specified. – Alowaniak Jul 23 '19 at 12:40
  • Related in JavaScript: https://stackoverflow.com/q/10804982/4174897 – Kyll Jul 23 '19 at 12:48

1 Answers1

3

Well, the handleSave function takes a complex type as input parameter, and returns void. The input type contains two properties:

  • currentValue is of type Partial<Flight>
  • stepIndex is of type number.

Another way to express the same with a different syntax could be:

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

and then use the interface instead:

CreateFlightComponent.handleSave( input:IHandleTypeParams): void

or with destructuring:

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

See the playground.

Read more about destructuring on the MDN.

Kyll
  • 7,036
  • 7
  • 41
  • 64
MikNiller
  • 1,242
  • 11
  • 17