TypeScript Interfaces and Type Aliases Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz covering interfaces, type aliases, structural typing, merging behavior, call signatures, index signatures, intersections, unions, and interface augmentation.

40 Questions
~80 minutes
1

Question 1

Which keyword is used to declare an interface in TypeScript?

A
interface
B
struct
C
shape
D
model
2

Question 2

Which property is optional in this interface?

typescript
interface User { id: number; name?: string; active: boolean; }
A
name
B
id
C
active
D
None are optional
3

Question 3

What does readonly do inside an interface?

A
Prevents reassignment of that property
B
Prevents reading the property
C
Converts the value to immutable deep state
D
Forces JSON serialization
4

Question 4

What error occurs here?

typescript
interface Point { readonly x: number; y: number; }
const p: Point = { x: 1, y: 2 };
p.x = 3;
A
Cannot assign to 'x' because it is a read-only property
B
Property 'x' does not exist
C
Unexpected token
D
No error
5

Question 5

Interfaces use which typing model?

A
Structural typing
B
Nominal typing
C
Symbolic typing
D
Pointer typing
6

Question 6

Which keyword creates a type alias?

A
type
B
alias
C
define
D
shape
7

Question 7

What is the type of value?

typescript
type Result = number | string;
let value: Result;
A
number or string
B
unknown
C
object
D
never
8

Question 8

Which statement about type aliases is true?

A
They can name any TypeScript type expression
B
They only work with objects
C
They create runtime variables
D
They must always be generic
9

Question 9

Which type is represented?

typescript
type Pair = [number, string];
A
A tuple type
B
An array of numbers
C
An object type
D
A readonly structure
10

Question 10

Which feature is supported by interfaces but not type aliases?

A
Declaration merging
B
Union types
C
Intersection types
D
Generics
11

Question 11

What does this extend?

typescript
interface A { x: number }
interface B extends A { y: number }
A
B inherits x from A
B
A inherits from B
C
Neither extends anything
D
Both become tuples
12

Question 12

Which statement is true comparing interfaces and type aliases?

A
Both can describe object shapes
B
Only type aliases can describe objects
C
Only interfaces can describe primitives
D
Only type aliases support generics
13

Question 13

What does this interface define?

typescript
interface Calc { (a: number, b: number): number }
A
A call signature for a function
B
An index signature
C
A tuple
D
A constructor
14

Question 14

Which option defines a method inside an interface?

A
speak(): string
B
(speak): string
C
speak => string
D
method speak
15

Question 15

What is the return type?

typescript
type Fn = (msg: string) => boolean;
A
boolean
B
string
C
void
D
unknown
16

Question 16

What does an index signature allow?

A
Flexible property names with enforced value types
B
Automatic JSON parsing
C
Static readonly fields only
D
Nested inference for arrays
17

Question 17

What type do values have?

typescript
interface Bag { [key: string]: number }
A
number
B
string
C
any
D
boolean
18

Question 18

Which constraint applies to index signatures?

A
Known properties must be compatible with the index signature
B
They can override readonly fields
C
They disable union types
D
They force numeric indices
19

Question 19

What does an intersection type do?

A
Combines multiple types into one
B
Chooses one type randomly
C
Removes shared fields
D
Creates optional properties
20

Question 20

What properties does Combined have?

typescript
type A = { x: number }
type B = { y: string }
type Combined = A & B;
A
x and y
B
x only
C
y only
D
No properties
21

Question 21

Unions allow which behaviour?

A
Allowing multiple possible types as input
B
Combining all fields into one
C
Enforcing strict single-type usage
D
Auto-casting between types
22

Question 22

What type is Status?

typescript
type Status = 'success' | 'error' | 'loading';
A
A union of string literal types
B
A tuple
C
A number union
D
An interface
23

Question 23

What is declaration merging?

A
Automatically combining multiple interface declarations
B
Removing unused interface fields
C
Overwriting a previous type alias
D
Preventing re-exporting
24

Question 24

What fields exist after merging?

typescript
interface Box { size: number }
interface Box { weight: number }
A
size and weight
B
size only
C
weight only
D
None, merging erases fields
25

Question 25

Which statements can interfaces extend?

A
Other interfaces and type aliases
B
Only classes
C
Only type aliases
D
Only primitives
26

Question 26

What does this produce?

typescript
type A = { a: number }
interface B extends A { b: string }
A
B has a and b
B
B has only b
C
B has only a
D
Invalid: interfaces cannot extend types
27

Question 27

Which is a benefit of using interfaces?

A
They allow augmentation through merging
B
They create runtime classes
C
They always prevent unions
D
They cannot extend others
28

Question 28

Type aliases are especially useful for:

A
Union and intersection types
B
Class inheritance
C
Preventing all merging
D
Runtime polymorphism
29

Question 29

Which approach models a discriminated union?

A
Using literal types combined in a union
B
Using arrays only
C
Creating multiple interfaces with same fields
D
Relying on default parameters
30

Question 30

Which statement about intersections is correct?

A
All intersected fields must be satisfied
B
Only one field is required
C
Intersections remove fields
D
Intersections work only on primitives
31

Question 31

What is the purpose of interface augmentation?

A
Adding new fields to existing interfaces
B
Replacing interfaces
C
Removing methods
D
Turning interfaces into classes
32

Question 32

Which pattern helps unify two compatible types?

A
Intersection types
B
Readonly modifiers
C
Type assertions always
D
Optional chaining
33

Question 33

Which can describe a function type?

A
Both interface and type alias
B
Type alias only
C
Interface only
D
Neither of them
34

Question 34

Which is true about optional interface fields?

A
They automatically include undefined in their type
B
They become readonly
C
They forbid assignment
D
They must be removed later
35

Question 35

Which type construct allows creating aliases for primitive types?

A
type aliases
B
interfaces
C
classes
D
decorators
36

Question 36

A property conflict in interface extension produces:

A
A type error when incompatible
B
Automatic merging
C
Ignored fields
D
Always a union type
37

Question 37

Which describes structural typing?

A
Compatibility based on shape instead of declarations
B
Exact matching by name only
C
Using runtime reflection
D
Matching only primitive types
38

Question 38

Why prefer interfaces for public API shapes?

A
They support extension and augmentation
B
They compile to runtime objects
C
They prevent union types
D
They disable type inference
39

Question 39

Which is NOT possible with type aliases?

A
Declaration merging
B
Union types
C
Intersection types
D
Tuple definitions
40

Question 40

Which scenario favors using a type alias?

A
When modelling unions or intersections
B
When automatic merging is needed
C
When defining class constructors
D
When preventing external augmentation

QUIZZES IN TypeScript