Shape with discriminated property kindcircle, rectangle, and trianglecalculateArea that takes a Shape and returns its areaswitch statement on shape.kind to handle each shape typeShape with discriminated property kindcircle, rectangle, and trianglecalculateArea that takes a Shape and returns its areaswitch statement on shape.kind to handle each shape typeShape that is a union of three object types. Each object must have a kind property with values 'circle', 'rectangle', or 'triangle'. Add these properties: radius: number for circle, width: number and height: number for rectangle, and base: number and height: number for triangle.Use a union type with | and make sure each shape has a kind property with a unique string literal.
shapes with three elements. Each element should be an object matching one of the Shape types you defined: a circle with radius 5, a rectangle with width 10 and height 4, and a triangle with base 6 and height 3.Make sure the array is called shapes and each object matches the Shape type exactly.
calculateArea that takes one parameter shape of type Shape. Use a switch statement on shape.kind to return the area for each shape: circle area is ฯ x radiusยฒ (use 3.14 for ฯ), rectangle area is width x height, triangle area is 0.5 x base x height.Use a switch on shape.kind and return the correct formula for each shape.
for loop to go through each shape in the shapes array. Inside the loop, print a message like Area of circle: 78.5 using console.log. Use the calculateArea function to get the area.Use a for...of loop and console.log with a template string to print the area for each shape.
What is the main purpose of a discriminated union in TypeScript?
Which of the following is the correct syntax to define a discriminated union type in TypeScript?
type Shape = ?kind with literal types ('circle' or 'square') to discriminate.kind.What will be the output of the following TypeScript code?
type Animal =
| { type: 'dog'; barkVolume: number }
| { type: 'cat'; livesLeft: number };
function describe(animal: Animal) {
switch (animal.type) {
case 'dog':
return `Dog barks at volume ${animal.barkVolume}`;
case 'cat':
return `Cat has ${animal.livesLeft} lives left`;
}
}
console.log(describe({ type: 'cat', livesLeft: 7 }));{ type: 'cat', livesLeft: 7 }, so the type is 'cat'.`Cat has ${animal.livesLeft} lives left`, which becomes 'Cat has 7 lives left'.Identify the error in this TypeScript code using discriminated unions:
type Vehicle =
| { kind: 'car'; wheels: number }
| { kind: 'bike'; pedals: number };
function getWheels(vehicle: Vehicle) {
if (vehicle.kind === 'car') {
return vehicle.wheels;
} else {
return vehicle.wheels;
}
}vehicle.wheels, but bike type has no wheels property.wheels is not defined for { kind: 'bike'; pedals: number }.Given the following discriminated union, how can you safely add a new shape type rectangle with width and height properties and update the area function accordingly?
type Shape =
| { kind: 'circle'; radius: number }
| { kind: 'square'; side: number };
function area(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2;
case 'square':
return shape.side ** 2;
}
}kind: 'rectangle' and properties width and height to the Shape union.shape.width * shape.height to correctly calculate the area.Pass the test and this pattern moves unknown