Shape that can be a Circle or a Square.isCircle that checks if a shape is a Circle.for loop to go through an array of shapes and calculate the area using the type predicate.Shape that can be a Circle or a Square.isCircle that checks if a shape is a Circle.for loop to go through an array of shapes and calculate the area using the type predicate.Circle with a radius number property, a type called Square with a side number property, and a union type called Shape that can be either Circle or Square. Then create a constant array called shapes with these exact values: a Circle with radius 5, a Square with side 10, and a Circle with radius 3.Start by defining the types exactly as described, then create the array with the exact objects.
isCircle that takes a parameter shape of type Shape and returns a type predicate shape is Circle. The function should return true if shape has the property radius, otherwise false.Use the syntax function isCircle(shape: Shape): shape is Circle and check if radius exists on shape.
for loop with the variable shape to go through the shapes array. Inside the loop, use the isCircle function to check if shape is a Circle. If it is, calculate the area as Math.PI * shape.radius * shape.radius. Otherwise, calculate the area as shape.side * shape.side. Store the area in a variable called area.Use if (isCircle(shape)) to check the type and calculate area accordingly.
for loop, after calculating the area, write a console.log statement that prints exactly: Area: followed by the area rounded to 2 decimal places using area.toFixed(2).Use console.log("Area: " + area.toFixed(2)) to print the area with two decimals.
What is the main purpose of a type predicate function in TypeScript?
Which of the following is the correct syntax for a type predicate function that checks if a value is a string?
function isString(value: unknown): ??? {
return typeof value === 'string';
}paramName is Type in the return type.value is a string, so the return type is value is string.What will be the output of this TypeScript code?
function isNumber(value: unknown): value is number {
return typeof value === 'number';
}
const data: unknown[] = [1, 'two', 3];
const numbers = data.filter(isNumber);
console.log(numbers);isNumber function returns true only for numbers, so filter keeps only numbers.[1, 'two', 3], only 1 and 3 pass the check, so numbers is [1, 3].Identify the error in this type predicate function and choose the fix:
function isBoolean(value: unknown): boolean {
return typeof value === 'boolean';
}value is boolean.value is boolean, not just boolean.Given this code, what is the best way to safely extract all strings from a mixed array using a type predicate?
type Mixed = (string | number | boolean)[];
function isString(value: unknown): value is string {
return typeof value === 'string';
}
const mixedArray: Mixed = ['hello', 42, true, 'world'];
// What code extracts only strings safely?
filter(isString) uses the type predicate to narrow type safely, so strings is typed as string[].instanceof String checks object wrappers, not primitives. const strings = mixedArray.map(x => x as string); casts all elements to string unsafely.Pass the test and this pattern moves unknown