Complete the code to declare a string enum with two colors.
enum Colors {
Red = [1],
Blue = "blue"
}The enum member must be assigned a string literal like "red" to create a string enum.
Complete the code to define a union type instead of an enum for colors.
type Colors = [1];A union of string literals is a lightweight alternative to enums without runtime code.
Fix the error in the code by replacing the enum with a union type to avoid runtime code.
enum Status {
Active,
Inactive
}
function checkStatus(status: [1]) {
return status === "Active";
}Replacing the enum type with a union of string literals avoids extra runtime code and matches the string comparison.
Fill both blanks to create a union type and a function that accepts only those values.
type Direction = [1]; function move(dir: Direction): void { console.log("Moving", dir); } move([2]);
The type Direction is a union of allowed strings. The function call uses one of those strings.
Fill all three blanks to rewrite the enum as a union type and use it in a function with a default parameter.
type Status = [1]; function setStatus(status: Status = [2]): string { return `Status is ${status}`; } console.log(setStatus([3]));
The union type defines allowed statuses. The default parameter and function call use valid values from the union.