A literal type is a type that represents a specific fixed value, like a string literal "hello" or a number literal 42, instead of a general type like string or number.
Click to reveal answer
beginner
How does TypeScript narrow types using literal types?
TypeScript narrows types by checking values against literal types in conditions, so inside an if statement checking for a specific literal, the type becomes that literal type.
Click to reveal answer
beginner
What will be the type of variable 'x' after this check?
if (x === "open") { /* here */ }
Inside the if block, 'x' is narrowed to the literal type "open" because the condition ensures 'x' equals that exact string.
Click to reveal answer
intermediate
Why are literal types useful in function parameters?
Literal types let you restrict function parameters to specific allowed values, helping catch errors early and making code easier to understand.
Click to reveal answer
intermediate
Explain how union types and literal types work together for narrowing.
When a variable has a union of literal types, TypeScript can narrow it to one specific literal inside conditional checks, allowing precise control over possible values.
Click to reveal answer
What type does TypeScript assign to a variable declared as const status = "success";?
Aunknown
Bstring
Cany
D"success" (literal type)
✗ Incorrect
Because 'status' is a const assigned to a string literal, TypeScript infers the literal type "success".
Given let mode: "auto" | "manual";, what happens inside if (mode === "auto") block?
Amode is narrowed to "auto"
Bmode remains "auto" | "manual"
Cmode is any
Dmode is string
✗ Incorrect
The condition narrows 'mode' to the literal type "auto" inside the if block.
Which of these is NOT a literal type in TypeScript?
A"hello"
B42
Cboolean
Dtrue
✗ Incorrect
"boolean" is a general type, not a literal type. Literal types are specific values like true or false.
How does TypeScript treat a variable declared as let x = 10; without a type annotation?
Ax has type number
Bx has type 10 (literal)
Cx has type any
Dx has type unknown
✗ Incorrect
Variables declared with let and assigned a number get the general number type, not the literal type.
What is the benefit of using literal types in TypeScript?
AThey convert types to any
BThey allow precise control over allowed values
CThey disable type checking
DThey make variables mutable
✗ Incorrect
Literal types restrict variables to exact values, improving safety and clarity.
Describe what literal types are and how TypeScript uses them to narrow values in code.
Think about how checking a variable against a specific value changes its type.
You got /3 concepts.
Explain how union types combined with literal types help in writing safer TypeScript functions.
Consider how limiting inputs to specific values prevents errors.
You got /3 concepts.
Practice
(1/5)
1. What does a literal type in TypeScript allow you to do?
easy
A. Specify exact allowed values for a variable
B. Create a new class
C. Define a function return type as any
D. Ignore type checking
Solution
Step 1: Understand the purpose of literal types
Literal types let you restrict a variable to exact values like 'yes' or 10 instead of general types like string or number.
Step 2: Compare options with the definition
Only Specify exact allowed values for a variable correctly describes this feature; others describe unrelated concepts.
Final Answer:
Specify exact allowed values for a variable -> Option A
2. Which of the following is the correct way to declare a variable with a literal type of the string 'open' in TypeScript?
easy
A. let status = 'open';
B. let status: string = 'open';
C. let status: 'open' = 'open';
D. let status: 'open'; status = 'closed';
Solution
Step 1: Check the syntax for literal type declaration
To declare a variable with a literal type, you specify the exact value in quotes as the type, like 'open'.
Step 2: Evaluate each option
let status: 'open' = 'open'; correctly declares status with type 'open' and assigns 'open'. let status: string = 'open'; uses string type, not literal. let status = 'open'; infers string type but not literal. let status: 'open'; status = 'closed'; declares type 'open' but assigns 'closed', which is invalid.
Final Answer:
let status: 'open' = 'open'; -> Option C
Quick Check:
Literal type syntax uses exact value in quotes [OK]
Hint: Use quotes around value to declare literal type [OK]
Common Mistakes:
Assigning a different value than the literal type
Using general types instead of literal types
Omitting the type and expecting literal type
3. What will be the output of the following TypeScript code?
type Direction = 'left' | 'right';
function move(dir: Direction) {
if (dir === 'left') {
return 'Moving left';
} else {
return 'Moving right';
}
}
console.log(move('right'));
medium
A. Moving right
B. Type error at compile time
C. Moving left
D. Undefined
Solution
Step 1: Understand the function behavior with literal type
The function move accepts only 'left' or 'right'. It returns 'Moving left' if input is 'left', else 'Moving right'.
Step 2: Trace the function call with 'right'
Calling move('right') triggers the else branch, returning 'Moving right'.
Final Answer:
Moving right -> Option A
Quick Check:
Input 'right' returns 'Moving right' [OK]
Hint: Check input matches condition for output [OK]
Common Mistakes:
Confusing which branch runs for 'right'
Expecting compile error for valid literal
Ignoring else branch
4. Identify the error in this TypeScript code using literal types and value narrowing:
type Status = 'success' | 'error';
function handle(status: Status) {
if (status === 'success') {
console.log('Success!');
} else if (status === 'fail') {
console.log('Failed!');
}
}
medium
A. Missing return type in function
B. No error, code is correct
C. Variable status is not declared
D. The else if condition checks an invalid literal 'fail'
Solution
Step 1: Check the allowed literal types for Status
Status can only be 'success' or 'error'.
Step 2: Analyze the else if condition
The code checks if status === 'fail', which is not part of Status type, causing a type error.
Final Answer:
The else if condition checks an invalid literal 'fail' -> Option D
Quick Check:
Check conditions must match literal types [OK]
Hint: Check all compared literals are in the type union [OK]
Common Mistakes:
Using literals not declared in the union type
Ignoring TypeScript errors on invalid comparisons
Assuming missing return type causes error here
5. Given the following TypeScript code, what is the type of variable result after the if statement?
type Mode = 'auto' | 'manual';
function setMode(mode: Mode) {
let result: string | undefined;
if (mode === 'auto') {
result = 'Automatic mode set';
} else {
result = undefined;
}
return result;
}
hard
A. string
B. string | undefined
C. undefined
D. Mode
Solution
Step 1: Analyze variable declaration and assignments
Variable result is declared as string | undefined. Inside if, assigned string; else assigned undefined.
Step 2: Determine narrowed type after if
Since result can be either string or undefined depending on mode, its type remains string | undefined.
Final Answer:
string | undefined -> Option B
Quick Check:
Variable type includes all assigned values [OK]
Hint: Variable type covers all assigned values after narrowing [OK]
Common Mistakes:
Assuming type narrows to string only
Ignoring undefined assignment
Confusing Mode type with result type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.