TypeScript Type Assertions and Type Guards Quiz
A 40-question TypeScript quiz exploring type assertions, narrowing with guards, typeof checks, instanceof checks, custom predicate guards, discriminated unions, and safe narrowing patterns.
Question 1
What does a type assertion do?
Question 2
Which assertion syntax is recommended in JSX environments?
Question 3
What type does TS assume after this assertion?
const val = '42' as unknown as number;Question 4
Why should assertions be used carefully?
Question 5
What does a typeof type guard check?
Question 6
What is x inside the if block?
let x: string | number;
if (typeof x === 'string') {
// x is now?
}Question 7
typeof can narrow which categories?
Question 8
instanceof is best suited for:
Question 9
What is pet inside the if block?
class Dog {}
class Cat {}
function check(pet: Dog | Cat) {
if (pet instanceof Dog) {
return 'dog';
}
}Question 10
Which statement about instanceof is accurate?
Question 11
The 'in' operator is used to:
Question 12
What does the guard narrow target to?
type A = { kind: 'a'; x: number };
type B = { kind: 'b'; y: string };
function test(v: A | B) {
if ('x' in v) {
return v.x;
}
}Question 13
The 'in' operator is often used in:
Question 14
What must a custom type guard return?
Question 15
What does this guard identify?
function isStr(v: unknown): v is string {
return typeof v === 'string';
}Question 16
Custom guards are most useful when:
Question 17
A discriminated union requires:
Question 18
Which branch executes?
type E = { tag: 'x'; v: number } | { tag: 'y'; v: string };
function run(e: E) {
if (e.tag === 'y') return e.v;
}Question 19
Discriminated unions are useful because:
Question 20
Why is unknown safer than any?
Question 21
What does TS allow inside the if block?
let u: unknown = 'hi';
if (typeof u === 'string') {
u.toUpperCase();
}Question 22
unknown requires:
Question 23
What is a risk of type assertions?
Question 24
Why is this unsafe?
const v = 'hello' as number;Question 25
Assertions should be avoided when:
Question 26
Combining multiple guards helps:
Question 27
What type is x inside the second if?
let x: unknown = 42;
if (typeof x === 'number') {
if (x > 10) {
// x is now?
}
}Question 28
Which operator narrows based on property presence?
Question 29
What does this function return?
function f(v: string | number) {
if (typeof v === 'number') return v * 2;
return v.toUpperCase();
}Question 30
Why might you assert to unknown first?
Question 31
What is result’s type?
const raw: unknown = JSON.parse('{"x":1}');
const result = raw as { x: number };Question 32
Type guards improve safety by:
Question 33
What does this guard ensure?
function isObj(v: unknown): v is { a: number } {
return typeof v === 'object' && v !== null && 'a' in v;
}Question 34
Discriminant properties allow narrowing by:
Question 35
Which branch activates?
type V = { t: 'one'; x: number } | { t: 'two'; y: boolean };
function pick(v: V) {
if (v.t === 'one') return v.x;
return v.y;
}Question 36
What improves guard reliability?
Question 37
What type is returned?
function toNum(v: string | number) {
if (typeof v === 'string') return Number(v);
return v;
}Question 38
Which operator can narrow union branches?
Question 39
What narrowing occurs here?
function g(v: string | number | boolean) {
if (typeof v === 'boolean') return !v;
return v;
}Question 40
Type guards should be preferred over assertions when:
