Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a union literal type for colors.
Typescript
type Color = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using enum syntax instead of union literal type.
Using a general string type instead of specific literals.
✗ Incorrect
The union literal type lists allowed string values directly.
2fill in blank
mediumComplete the code to declare an enum for colors.
Typescript
enum Color { [1] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using union literal syntax inside enum.
Listing values without identifiers.
✗ Incorrect
Enum members can be assigned string values for clarity.
3fill in blank
hardFix the error in this enum usage to get the string value.
Typescript
const colorName: string = Color[1]Red; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using brackets instead of dot notation.
Omitting the dot before member name.
✗ Incorrect
Use dot notation to access enum members.
4fill in blank
hardFill both blanks to define a function accepting only specific colors.
Typescript
function paint(color: [1]): void { console.log(`Painting with ${color}`); } paint([2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a general string type for the parameter.
Passing a string not in the union type.
✗ Incorrect
The function parameter uses a union literal type, and the call uses an enum member.
5fill in blank
hardFill all three blanks to create a type guard for enum or union literal types.
Typescript
function isColor(value: any): value is [1] { return typeof value === [2] && [3]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using enum type in the type guard without proper checks.
Incorrect logical OR syntax in the return statement.
✗ Incorrect
The type guard checks if the value is a string and matches one of the allowed literals.