Complete the code to declare a variable with a literal type.
let direction: 'up' | 'down' = [1];
The variable direction can only be 'up' or 'down'. Assigning 'up' is correct.
Complete the function to narrow the literal type using a switch statement.
function move(direction: 'left' | 'right') { switch (direction) { case [1]: return 'Moving left'; case 'right': return 'Moving right'; } }
The switch case must match one of the literal types. 'left' is correct here.
Fix the error in the type narrowing by completing the if condition.
function isYes(answer: 'yes' | 'no'): boolean { if (answer === [1]) { return true; } return false; }
The function returns true only if the answer is exactly 'yes'.
Fill both blanks to create a function that narrows a literal type and returns a message.
function getStatus(status: 'success' | 'error') { if (status === [1]) { return 'Operation was successful'; } else if (status === [2]) { return 'Operation failed'; } }
The function checks if status is 'success' or 'error' and returns the corresponding message.
Fill all three blanks to create a function that narrows a literal type and returns a custom message.
function respond(action: 'start' | 'stop' | 'pause') { if (action === [1]) { return 'Starting'; } else if (action === [2]) { return 'Stopping'; } else if (action === [3]) { return 'Pausing'; } }
The function checks the action literal and returns the matching message.