6. Advanced Types
Activity 6.01: Intersection Type
Solution:
- Create a
Motor
type, which will house some common properties you may reuse on their own or in combination with other types to describe a vehicle object. You can use the following properties as a starting point:color
,doors
,wheels
, andfourWheelDrive
:type Motor = { color: string; doors: number; wheels: number; fourWheelDrive: boolean; }
- Create a
Truck
type with properties common to a truck, for example,doubleCab
andwinch
:type Truck = { doubleCab: boolean; winch: boolean; }
- Intersect the two types to create a
PickUpTruck
type:type PickUpTruck = Motor & Truck;
- Build a
TruckBuilder
function that returns ourPickUpTruck
type and also takesPickUpTruck
as an argument:function TruckBuilder (truck: PickUpTruck): PickUpTruck { return...