Sample questions
Question 1Easy
The boolean type is equivalent to which union?
- true | false
- true | false | null
- true | undefined
- 0 | 1
Answer: A. true | false
Explanation
TypeScript treats boolean as the union of the two literal types true and false, which is why narrowing on a boolean works.
Step-by-step solution
- Boolean is defined as the union of two literal types.
- The two literal types are true and false.
- So boolean is the same as true | false (option A).
Why the other options are wrong
- B. Null is not part of boolean.
- C. Undefined is not part of boolean, and true alone leaves out false.
- D. Those are numbers, not boolean literals.
Tip: Boolean is true | false, which is why narrowing on a boolean works like narrowing on a union.
Question 2Easy
What is printed?
enum Dir { Up, Down, Left, Right }
console.log(Dir.Left);
- Left
- 2
- 3
- "Left"
Answer: B. 2
Explanation
Numeric enum members start at 0 and increase by 1, so
- Up = 0,
- Down = 1,
- Left = 2.
Step-by-step solution
- Enum members without a value start at 0 and increase by 1.
- Up is 0, Down is 1, Left is 2 and Right is 3.
- Dir.Left is therefore the number 2.
- console.log prints 2 (option B).
Why the other options are wrong
- A. A numeric enum member prints its value, not its name.
- C. Left is the third member, starting at 0, so its value is 2, not 3.
- D. The value is the number 2, not the string Left.
Tip: Numeric enum members count from 0 unless you set a value; each next member adds 1.
Question 3Easy
Why do many teams prefer type Status = "active" | "inactive" over an enum?
- Unions of strings are checked at runtime as well
- A union type can be iterated with a for loop
- Enums are not allowed when strict mode is on
- It emits no runtime code; values are plain strings
Answer: D. It emits no runtime code; values are plain strings
Explanation
A union of string literals is erased at compile time and still gives autocomplete and checking. An enum generates a JavaScript object.
Step-by-step solution
- A union of string literals is erased by the compiler.
- It still gives autocomplete and type checking at compile time.
- An enum generates a JavaScript object at runtime.
- So the union emits no runtime code and its values are plain strings (option D).
Why the other options are wrong
- A. Type unions are not checked at runtime; they are erased.
- B. A union is a type, not a collection, so it cannot be looped over.
- C. Enums are allowed in strict mode.
Tip: String literal unions are erased at compile time; enums add runtime code.
Question 4Medium
What is printed?
enum Level { Low = 1, Mid, High = 10, Max }
console.log(Level.Mid, Level.Max);
- 1 10
- 2 4
- Mid Max
- 2 11
Answer: D. 2 11
Explanation
A member without an initialiser takes the previous value plus 1: Mid follows 1, and Max follows 10.
Step-by-step solution
- Low = 1 is set, so Mid has no value and takes 1 + 1 = 2.
- High = 10 is set explicitly.
- Max has no value and takes 10 + 1 = 11.
- console.log prints 2 11 (option D).
Why the other options are wrong
- A. Mid is 2, not 1, and Max is 11, not 10.
- B. Max is 11, not 4; the count does not restart.
- C. Enum members print as their numeric values, not their names.
Tip: A member without a value takes the previous numeric value plus 1.
Question 5Medium
What is the type of t?
const t = [1, "a"] as const;
- [number, string]
- readonly (1 | "a")[]
- (string | number)[]
- readonly [1, "a"]
Answer: D. readonly [1, "a"]
Explanation
as const on an array literal produces a readonly tuple with literal element types.
Step-by-step solution
- As const on an array literal gives a readonly tuple.
- The elements keep their literal types: 1 and 'a'.
- So t has the type readonly [1, 'a'] (option D).
Why the other options are wrong
- A. The array is readonly and its elements are the literals 1 and 'a', not number and string.
- B. The type is a tuple, not an array of a union.
- C. The type is a readonly tuple with literal types, not a mutable array of string | number.
Tip: As const turns an array literal into a readonly tuple with literal element types.
Question 6Medium
What does TypeScript report for the second line?
enum Level { Low = "LOW", High = "HIGH" }
let l: Level = "LOW";
- No error; the string matches Low's value
- Error: "LOW" is not assignable to type Level
- No error; string enums accept any string
- Error: enums cannot be used as types
Answer: B. Error: "LOW" is not assignable to type Level
Explanation
String enums are nominal: only Level.Low (not the raw string) is assignable to Level, even though the value is the same.
Step-by-step solution
- Level is a string enum, so its type is nominal.
- The raw string 'LOW' is not a Level value.
- Only Level.Low is assignable to Level, even though the runtime value is the same.
- So the second line errors (option B).
Why the other options are wrong
- A. Having the same value is not enough; the type is nominal.
- C. String enum types reject raw strings.
- D. Enums can be used as types.
Tip: A string enum type accepts only its members, so write Level.Low, not 'LOW'.
Question 7Medium
Which enums get a reverse mapping from value to name in the emitted JavaScript?
- Neither kind of enum
- Numeric enums only
- String enums only
- Both numeric and string enums
Answer: B. Numeric enums only
Explanation
Numeric members emit both Color["Red"] = 0 and Color[0] = "Red". String members only emit the name-to-value direction.
Step-by-step solution
- A numeric enum emits both Color.Red = 0 and Color[0] = 'Red'.
- A string enum emits only the name-to-value direction.
- So only numeric enums get a reverse mapping.
- The answer is numeric enums only (option B).
Why the other options are wrong
- A. Numeric enums do get the reverse mapping.
- C. String enums do not get it.
- D. String enums do not get the reverse mapping, so not both.
Tip: Only numeric enums support value-to-name lookup at runtime.
Question 8Medium
What is the main benefit of satisfies over a type annotation here?
type Palette = Record<string, string | number[]>;
const p = { red: [255, 0, 0], green: "#0f0" } satisfies Palette;
p.green.toUpperCase();
- It makes every property of the object readonly
- It checks the value but keeps the narrower inferred type
- It turns off excess property checks for the object
- It converts the value to the Palette type at runtime
Answer: B. It checks the value but keeps the narrower inferred type
Explanation
satisfies validates the expression against Palette without widening it, so p.green is still known to be a string.
Step-by-step solution
- Satisfies checks the object against Palette.
- It does not widen the inferred type of the object.
- So p.green keeps type string, and toUpperCase exists on it.
- The main benefit is keeping the narrow type (option B).
Why the other options are wrong
- A. Satisfies does not make properties readonly.
- C. Excess property checks still apply with satisfies.
- D. Satisfies is erased; it does no runtime conversion.
Tip: Use satisfies to check a value's shape while keeping its specific inferred type.
Question 9Hard
What is printed?
enum Dir { Up, Down }
console.log(Object.keys(Dir));
- [ '0', '1', 'Up', 'Down' ]
- [ 'Up', 'Down', '0', '1' ]
- [ '0', '1' ]
- [ 'Up', 'Down' ]
Answer: A. [ '0', '1', 'Up', 'Down' ]
Explanation
The reverse mapping adds the numeric keys too. Integer-like keys come first in key order, so iterating a numeric enum needs filtering.
Step-by-step solution
- Dir has Up = 0 and Down = 1, and the reverse mapping adds keys '0' and '1'.
- The keys are created in the order 'Up', '0', 'Down', '1'.
- Object.keys lists integer-like keys first, in ascending order: '0', '1'.
- Then the names follow in creation order, giving [ '0', '1', 'Up', 'Down' ] (option A).
Why the other options are wrong
- B. Integer-like keys come first, not last.
- C. The names Up and Down are keys too.
- D. The numeric reverse keys are also present.
Tip: Object.keys of a numeric enum includes the reverse keys; filter out the numeric ones to get names only.
Question 10Hard
What does TypeScript report for this enum?
enum E { A = "a", B }
- No error; B becomes 0
- No error; B becomes 1
- Error: B needs an initialiser
- No error; B becomes "b"
Answer: C. Error: B needs an initialiser
Explanation
Auto-increment only works after a numeric member. After a string member there is nothing to increment, so B must be initialised explicitly.
Step-by-step solution
- Enum auto-increment only works after a numeric member.
- A is the string 'a', so there is no number to add 1 to for B.
- B has no initialiser, so TypeScript reports an error.
- The answer is the error that B needs an initialiser (option C).
Why the other options are wrong
- A. B gets no implicit value after a string member, so there is an error.
- B. No implicit 1 is given either; the initialiser is required.
- D. B is not set to 'b'; no value is inferred.
Tip: After a string member, every later member needs its own initialiser.
Practise all 17 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →