6

I have an object property with the following signature.

handleItem = (data: Output & { isValid: boolean }) => {}

I do not understand the & part.

Basically I trying to pass some arguments as:

handleItem (outputItem, { isValid: false })

and I receive an error

Expected 1 arguments, but got 2.'

How to pass values property? How the & is used in this instance?

msanford
  • 11,803
  • 11
  • 66
  • 93
Radex
  • 7,815
  • 23
  • 54
  • 86

4 Answers4

6

handleItem takes a single argument, an object that has the properties of Output and the isValid property. You need to construct such an object. Using the spread operator is a good option:

handleItem ({ isValid: false, ...outputItem })

You can read more about intersection types here

Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
3

this is called Intersection Type and it means that your param of your handleItem() method combines Output and { isValid: boolean } - will say that your parameter needs to have both types, Output and { isValid: boolean }.

Typescript & operator

so you need to call it this way:

handleItem ({ isValid: false, ...outputItem })

if you want to pass two parameters do the following:

handleItem = (data: Output, { isValid: boolean }) => {}

afterwards you can do:

handleItem (outputItem, { isValid: false })
messerbill
  • 5,499
  • 1
  • 27
  • 38
2

& is a type intersection. Output & { isValid: boolean } means it expects something as argument that conforms both to the interface Output and { isValid: boolean }. In other words, it expects one object which has an isValid property and whatever else Output defines.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

I think the syntax expect the property isValid of data to be boolean

Faly
  • 13,291
  • 2
  • 19
  • 37