0

I saw a typescript code like

type Point = PartialPointX & { y: number; };

and I know '&' in javascript is "and",'|' is "OR", do they work different in typescript?

Haem
  • 929
  • 6
  • 15
  • 31
cao champ
  • 17
  • 2

1 Answers1

3

That is an intersection type. Basically it combines multiple types into one so your new type Point is an object with the properties of PartialPointX and { y: number; }.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • I know this,I don't understand why is "&" work different from js. – cao champ Aug 28 '19 at 08:38
  • @caochamp Because this is Typescript. – Murat Karagöz Aug 28 '19 at 08:49
  • When used on types (like in your question), they are the intersection ('&') and union ('|') operators. See https://stackoverflow.com/questions/33875609/typescript-operator answer for more details. When used on numbers, in Typescript and Javascript, they are bitwise operators (for AND and OR operations). See https://www.w3schools.com/js/js_bitwise.asp. – Rauks Aug 29 '19 at 13:05