TypeScript Literal Types and Type Inference Quiz

TypeScript
0 Passed
0% acceptance

A 35-question TypeScript quiz about literal types, inference behavior, widening rules, const contexts, and practical modelling using specific literal values.

35 Questions
~70 minutes
1

Question 1

What does a literal type represent?

A
A type restricted to one exact value
B
A type that accepts any string
C
A type that always widens
D
A runtime-only constraint
2

Question 2

Which of the following is a literal type?

A
'ready'
B
string
C
number
D
boolean
3

Question 3

What is the type of status?

typescript
const status = 'idle';
A
'idle'
B
string
C
string | number
D
any
4

Question 4

Literal types are often used to:

A
Model exact allowable values
B
Disable inference
C
Replace generics entirely
D
Avoid narrowing
5

Question 5

What type does TypeScript infer for value?

typescript
let value = 'active';
A
string
B
'active'
C
string | undefined
D
unknown
6

Question 6

Which variable declaration preserves literal types by default?

A
const
B
let
C
var
D
any declaration
7

Question 7

What does widening refer to?

A
Converting a literal type into a general primitive type
B
Narrowing a value to a specific branch
C
Turning types into never
D
Only supporting boolean literals
8

Question 8

What is required to perform narrowing on literal unions?

A
A check that eliminates impossible values
B
A decorator
C
A namespace
D
A constructor
9

Question 9

What type does kind narrow to inside the if block?

typescript
type Mode = 'view' | 'edit';
function run(kind: Mode) {
  if (kind === 'edit') {
    return 'editing';
  }
}
A
'edit'
B
'view'
C
string
D
never
10

Question 10

Literal narrowing enables:

A
Exhaustive branching with discriminants
B
Runtime casting
C
Disabled inference
D
Automatic enum creation
11

Question 11

Which is a boolean literal type?

A
true
B
boolean
C
string
D
never
12

Question 12

What is the inferred type of count?

typescript
const count = 42;
A
42
B
number
C
string
D
any
13

Question 13

Why are string literal unions helpful?

A
They define finite sets of allowed values
B
They create enums internally
C
They disable inference
D
They allow undeclared variables
14

Question 14

What is the type of data?

typescript
const data = { state: 'loading' } as const;
A
{ readonly state: 'loading' }
B
{ state: string }
C
{ state: any }
D
unknown
15

Question 15

const assertions prevent:

A
Widening of literal values
B
Function calls
C
Object creation
D
Type checking
16

Question 16

What is the type of nums?

typescript
const nums = [1, 2, 3] as const;
A
readonly [1, 2, 3]
B
number[]
C
readonly number[]
D
any[]
17

Question 17

When does widening typically occur?

A
When using let for variable declarations
B
With const declarations
C
Inside readonly tuples
D
Inside const-asserted objects
18

Question 18

What is the inferred type of msg?

typescript
let msg = 'hello';
A
string
B
'hello'
C
any
D
unknown
19

Question 19

Widening from literal types helps:

A
Enable variable reassignment with compatible values
B
Freeze values permanently
C
Create tuple types
D
Disable narrowing
20

Question 20

What is returned's inferred type?

typescript
function getState() {
  return 'ready';
}
A
string
B
'ready'
C
any
D
unknown
21

Question 21

Literal types can be combined into:

A
Unions of finite value sets
B
Primitive-only classes
C
Runtime-only types
D
Decorators
22

Question 22

What is the type of lvl?

typescript
const lvl: 1 | 2 | 3 = 2;
A
1 | 2 | 3
B
number
C
any
D
never
23

Question 23

Literal inference for arrays without const results in:

A
number[] or string[]
B
readonly literal tuples
C
never[]
D
literal-position arrays
24

Question 24

Literal value sets are commonly used for:

A
Restricting configuration options
B
Replacing generics
C
Turning off inference
D
Runtime-only evaluation
25

Question 25

Which values can mode accept?

typescript
type Mode = 'auto' | 'manual';
let mode: Mode;
A
'auto' or 'manual'
B
any string
C
true | false
D
never
26

Question 26

Literal numeric unions can be used to:

A
Define limited step sizes or levels
B
Force floating-point precision
C
Disable numeric operations
D
Assign NaN safely
27

Question 27

What is the type of label?

typescript
let label = 'high' as const;
A
'high'
B
string
C
boolean
D
any
28

Question 28

Why can literal widening be surprising?

A
Variables declared with let widen even if initialized with literals
B
const never widens
C
as const always widens
D
Functions force literal inference always
29

Question 29

Which describes the effect of a literal annotation?

A
It permanently restricts a variable to that value
B
It allows widening freely
C
It removes type information
D
It requires object types
30

Question 30

Why must literal sets be updated carefully?

A
Missing values may create unreachable branches
B
They disable inference
C
They affect runtime speed
D
They enforce object immutability
31

Question 31

Which values does TypeScript widen string literals to?

A
string
B
any
C
unknown
D
boolean
32

Question 32

Literal inference for function parameters:

A
Treats incoming values as widened types
B
Preserves literal types permanently
C
Disables narrowing
D
Enforces readonly behavior
33

Question 33

Why do object property literals widen unless const-asserted?

A
To allow reassignment of properties
B
To freeze object shapes
C
To enable merging
D
To disable control flow
34

Question 34

What does a literal-typed variable prevent?

A
Assigning broader primitive values later
B
Being used in unions
C
Being used in functions
D
Being narrowed
35

Question 35

Literal types improve API design by:

A
Ensuring only specific, intentional values may be provided
B
Allowing arbitrary values
C
Replacing object types entirely
D
Disabling strict mode

QUIZZES IN TypeScript