TypeScript Type Assertions and Type Guards Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz exploring type assertions, narrowing with guards, typeof checks, instanceof checks, custom predicate guards, discriminated unions, and safe narrowing patterns.

40 Questions
~80 minutes
1

Question 1

What does a type assertion do?

A
Overrides TypeScript’s inferred type with a specified one
B
Creates a new runtime value
C
Performs automatic narrowing
D
Runs a compile-time function
2

Question 2

Which assertion syntax is recommended in JSX environments?

A
value as Type
B
<Type>value
C
Type[value]
D
value::Type
3

Question 3

What type does TS assume after this assertion?

typescript
const val = '42' as unknown as number;
A
number
B
string
C
unknown
D
never
4

Question 4

Why should assertions be used carefully?

A
Incorrect assumptions can break type safety
B
They slow runtime execution
C
They disable narrowing automatically
D
They change JavaScript output
5

Question 5

What does a typeof type guard check?

A
Runtime primitive category of a value
B
Property existence on objects
C
Prototype inheritance
D
Enum membership
6

Question 6

What is x inside the if block?

typescript
let x: string | number;
if (typeof x === 'string') {
  // x is now?
}
A
string
B
number
C
never
D
unknown
7

Question 7

typeof can narrow which categories?

A
string, number, boolean, symbol, bigint, undefined, object, function
B
Tuples only
C
Interfaces only
D
Enums only
8

Question 8

instanceof is best suited for:

A
Checking class instances
B
Checking primitive strings
C
Checking enum values
D
Checking tuple lengths
9

Question 9

What is pet inside the if block?

typescript
class Dog {}
class Cat {}
function check(pet: Dog | Cat) {
  if (pet instanceof Dog) {
    return 'dog';
  }
}
A
Dog
B
Cat
C
never
D
string
10

Question 10

Which statement about instanceof is accurate?

A
It checks prototype inheritance chains
B
It checks only primitive types
C
It prevents runtime errors automatically
D
It works for all interfaces
11

Question 11

The 'in' operator is used to:

A
Check if a property exists on an object
B
Determine primitive category
C
Check tuple size
D
Check inheritance chains
12

Question 12

What does the guard narrow target to?

typescript
type A = { kind: 'a'; x: number };
type B = { kind: 'b'; y: string };
function test(v: A | B) {
  if ('x' in v) {
    return v.x;
  }
}
A
A
B
B
C
never
D
unknown
13

Question 13

The 'in' operator is often used in:

A
Object-shape-based narrowing
B
Checking if a tuple is readonly
C
Comparing enums
D
Boolean coercion
14

Question 14

What must a custom type guard return?

A
A boolean with a 'value is Type' predicate signature
B
An enum
C
A number
D
A readonly tuple
15

Question 15

What does this guard identify?

typescript
function isStr(v: unknown): v is string {
  return typeof v === 'string';
}
A
string
B
boolean
C
object
D
never
16

Question 16

Custom guards are most useful when:

A
Static narrowing cannot infer object shape automatically
B
You want to skip type checking
C
You need tuple concatenation
D
You want numeric coercion
17

Question 17

A discriminated union requires:

A
A shared literal property used to discriminate
B
All members to be classes
C
A tuple of only numbers
D
Unique object shapes without common fields
18

Question 18

Which branch executes?

typescript
type E = { tag: 'x'; v: number } | { tag: 'y'; v: string };
function run(e: E) {
  if (e.tag === 'y') return e.v;
}
A
The branch where tag is 'y'
B
The 'x' branch only
C
Both branches
D
None
19

Question 19

Discriminated unions are useful because:

A
They allow safe exhaustive switching
B
They disable type inference
C
They prevent property access
D
They behave like tuples at runtime
20

Question 20

Why is unknown safer than any?

A
It requires narrowing before usage
B
It automatically narrows
C
It converts to never
D
It allows all operations
21

Question 21

What does TS allow inside the if block?

typescript
let u: unknown = 'hi';
if (typeof u === 'string') {
  u.toUpperCase();
}
A
string methods
B
number-only operations
C
Any operation
D
No operations allowed
22

Question 22

unknown requires:

A
A guard before property access
B
An enum
C
A class instance
D
Duck typing only
23

Question 23

What is a risk of type assertions?

A
They can assert incorrect types without validation
B
They throw runtime errors
C
They slow down JavaScript execution
D
They change variable mutability
24

Question 24

Why is this unsafe?

typescript
const v = 'hello' as number;
A
The asserted type does not match the actual value
B
It creates an enum
C
It changes runtime type
D
It narrows automatically
25

Question 25

Assertions should be avoided when:

A
A safer type guard alternative exists
B
Narrowing is impossible
C
JSX is enabled
D
A union is small
26

Question 26

Combining multiple guards helps:

A
Refine complex unions safely
B
Skip compile checks
C
Force primitive coercion
D
Enable inferred tuples
27

Question 27

What type is x inside the second if?

typescript
let x: unknown = 42;
if (typeof x === 'number') {
  if (x > 10) {
    // x is now?
  }
}
A
number
B
unknown
C
string
D
never
28

Question 28

Which operator narrows based on property presence?

A
in
B
==
C
===
D
~
29

Question 29

What does this function return?

typescript
function f(v: string | number) {
  if (typeof v === 'number') return v * 2;
  return v.toUpperCase();
}
A
Either a number or string based on input
B
never
C
boolean only
D
undefined only
30

Question 30

Why might you assert to unknown first?

A
To transition between unrelated types safely
B
To disable inference
C
To create enums
D
To convert tuples
31

Question 31

What is result’s type?

typescript
const raw: unknown = JSON.parse('{"x":1}');
const result = raw as { x: number };
A
{ x: number }
B
unknown
C
string
D
never
32

Question 32

Type guards improve safety by:

A
Confirming types through runtime checks
B
Replacing runtime entirely
C
Converting types automatically
D
Preventing union types
33

Question 33

What does this guard ensure?

typescript
function isObj(v: unknown): v is { a: number } {
  return typeof v === 'object' && v !== null && 'a' in v;
}
A
That v conforms to the given object shape
B
That v is a tuple
C
That v is undefined
D
That v is a symbol
34

Question 34

Discriminant properties allow narrowing by:

A
Comparing a shared literal field
B
Comparing prototypes
C
Checking tuple lengths
D
Comparing symbolic constants
35

Question 35

Which branch activates?

typescript
type V = { t: 'one'; x: number } | { t: 'two'; y: boolean };
function pick(v: V) {
  if (v.t === 'one') return v.x;
  return v.y;
}
A
Branch depends on v.t value
B
Always the first branch
C
Always the second branch
D
Neither branch
36

Question 36

What improves guard reliability?

A
Validating runtime structure
B
Asserting blindly
C
Disabling strict mode
D
Using only enums
37

Question 37

What type is returned?

typescript
function toNum(v: string | number) {
  if (typeof v === 'string') return Number(v);
  return v;
}
A
number
B
string
C
boolean
D
never
38

Question 38

Which operator can narrow union branches?

A
typeof
B
+
C
%
D
>>
39

Question 39

What narrowing occurs here?

typescript
function g(v: string | number | boolean) {
  if (typeof v === 'boolean') return !v;
  return v;
}
A
boolean branch narrows v to boolean
B
v becomes never
C
v becomes unknown
D
v becomes a tuple
40

Question 40

Type guards should be preferred over assertions when:

A
Runtime validation is possible
B
You want to ignore type safety
C
You want to convert numbers automatically
D
You want to disable inference

QUIZZES IN TypeScript