Chapter 43: TypeScript Quiz
TypeScript Quiz designed exactly as if we are sitting together in a classroom or doing a live coding session right now.
I prepared three difficulty levels:
- Level 1 – Beginner / early intermediate (good warm-up)
- Level 2 – Solid intermediate (most common interview / daily coding level)
- Level 3 – Advanced / tricky (senior / library author level)
Each question has:
- the question itself
- 4 answer options (A/B/C/D)
- short explanation right after (so you can learn immediately)
- difficulty rating
You can try to answer first, then check the explanation.
Let’s start!
Level 1 – Beginner / early intermediate
Q1 What is the type of status after this line?
|
0 1 2 3 4 5 6 |
const status = "active" as const; |
A) “active” B) string C) “active” | string D) const
Correct: A as const turns it into a literal type. Very important pattern for configs, routes, roles, variants.
Q2 What happens if you write this?
|
0 1 2 3 4 5 6 |
let user: { name: string } = { name: "Sara", age: 28 }; |
A) Compile error B) Runtime error C) Compiles, but age is any D) Compiles, age is ignored
Correct: A TypeScript is structurally typed → extra properties are not allowed unless you add index signature or use type assertion.
Q3 What is the type of result?
|
0 1 2 3 4 5 6 |
const result = Math.random() > 0.5 ? "success" : 404; |
A) “success” | 404 B) string | number C) “success” | number D) string | 404
Correct: B TypeScript widens literals to base types unless as const is used or context forces narrowing.
Level 2 – Intermediate (most common real-world questions)
Q4 Which line causes a compile error?
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
interface User { readonly id: number; name?: string; } const u: User = { id: 1, name: "Sara" }; // A u.id = 2; // B u.name = undefined; // C delete u.name; // D u = { id: 3 }; |
Correct: A readonly prevents reassignment. name?: string means it can be undefined or missing. delete is allowed on optional properties. Whole object replacement is allowed.
Q5 What is the type of value inside the if block?
|
0 1 2 3 4 5 6 7 8 9 10 |
function print(value: string | number | null) { if (value == null) return; // here } |
A) string | number B) string | number | null C) string & number D) never
Correct: A value == null checks both null and undefined. TypeScript narrows correctly.
Q6 Which of these is not valid syntax in modern (stage 3) decorators?
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
A) @logged method() {} B) @validate(5) prop: string; C) class @controller MyCtrl {} D) function @pure(target) { ... } |
Correct: D Decorators are placed before the thing they decorate. function @pure is invalid syntax.
Level 3 – Advanced / tricky (senior & library level)
Q7 What is the type of result?
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
type A = { kind: "a"; value: string }; type B = { kind: "b"; value: number }; type Union = A | B; const data = { kind: "a", value: "hello" } satisfies Union; const result = data.value; |
A) string | number B) string C) “hello” D) never
Correct: B satisfies narrows the type correctly while still allowing extra properties (unlike as).
Q8 Which line causes a compile error?
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
interface Box<T> { value: T; } const stringBox: Box<string> = { value: "hi" }; // A const anyBox: Box<any> = stringBox; // B const unknownBox: Box<unknown> = stringBox; // C const neverBox: Box<never> = stringBox; // D const stringOrNumberBox: Box<string | number> = stringBox; |
Correct: C never is the bottom type — nothing is assignable to never (except never itself).
Q9 What is printed / what error do you get?
|
0 1 2 3 4 5 6 7 8 9 10 11 |
type Fn = (x: number) => string; const f1: Fn = (x) => x.toString(); const f2: Fn = (x: number, y = 0) => (x + y).toString(); console.log(f2.length); |
A) 1 B) 2 C) 0 D) Compile error
Correct: A Function length property counts required parameters only. Optional/default parameters are not counted.
Q10 – Final tricky one
Which of these does NOT compile?
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
A) const x: "a" = "a" as const; B) let y = "b" as const; y = "c"; C) const z = { a: 1 } as const; z.a = 2; D) function f<T extends string>(v: T): T { return v }; f("hello"); |
Correct: B as const on let makes it readonly literal — you cannot reassign.
How to use these questions effectively
- Try to answer without running the code first
- If unsure → paste into https://www.typescriptlang.org/play
- After seeing the answer → ask yourself: Why does TypeScript behave this way? When is this behavior useful in real code?
- Try to change one small thing in each question and see how the type/error changes
Which level felt most comfortable / most challenging for you? Or do you want 10 more questions at a specific difficulty?
We can continue the quiz right away 😄
