TypeScript Decorators Quiz

TypeScript
0 Passed
0% acceptance

A 35-question TypeScript quiz covering decorator fundamentals, class decorators, method and property decorators, decorator factories, metadata reflection, and execution order.

35 Questions
~70 minutes
1

Question 1

Decorators can be applied to:

A
Classes, methods, accessors, properties, and parameters
B
Functions only
C
Variables declared with let
D
Top-level statements only
2

Question 2

Which config flag must be enabled to use decorators?

A
experimentalDecorators
B
allowSyntheticDefaultImports
C
resolveJsonModule
D
useUnknownInCatchVariables
3

Question 3

A decorator is:

A
A function called with a specific target during class evaluation
B
A runtime variable
C
A built-in keyword
D
A type alias
4

Question 4

What does the decorator receive?

typescript
function Log(target: Function) {}
@Log
class Box {}
A
The constructor function of the class
B
An instance of the class
C
The module
D
A property descriptor
5

Question 5

Class decorators can:

A
Replace or modify the class constructor
B
Access uninitialized instance properties
C
Execute after instances are created
D
Auto-generate imports
6

Question 6

When does this run?

typescript
@Log
class App {}
function Log(constructor: Function) { console.log('run'); }
A
When the class is defined, before any instance is created
B
When calling new App()
C
Only during compilation
D
During import resolution
7

Question 7

A decorator factory is:

A
A function returning the decorator function
B
A decorator applied twice
C
A class with static fields
D
A factory for TypeScript types
8

Question 8

What prints first?

typescript
function Tag(name: string) {
  return function(target: Function) {
    console.log('decorator');
  };
}
@Tag('box')
class Box {}
A
decorator
B
box
C
Both print together
D
Nothing prints
9

Question 9

Decorator factories are useful for:

A
Configuring decorators with parameters
B
Creating new operators
C
Avoiding decorator invocation
D
Skipping static analysis
10

Question 10

What does a method decorator receive?

typescript
class A {
  @Dec
  run() {}
}
function Dec(target: any, key: string, desc: PropertyDescriptor) {}
A
Prototype, method name, and descriptor
B
Constructor only
C
The method return value
D
Instance of the class
11

Question 11

Method decorators can:

A
Wrap, replace, or modify the method via the descriptor
B
Modify private fields directly
C
Define class inheritance
D
Rewrite import paths
12

Question 12

Property decorators receive:

typescript
class P { @Label name: string; }
function Label(target: any, key: string) {}
A
Prototype and property name
B
A property descriptor
C
The property value
D
An instance of P
13

Question 13

Property decorators cannot:

A
Directly modify property initializers
B
Access the property name
C
Attach metadata
D
Use decorator factories
14

Question 14

What does a parameter decorator receive?

typescript
class C {
  run(@Info id: number) {}
}
function Info(target: any, key: string, index: number) {}
A
Prototype, method name, and parameter index
B
Only the value
C
The class instance
D
A metadata descriptor
15

Question 15

Parameter decorators are often used for:

A
Dependency injection frameworks
B
Declaring return types
C
Modifying parameter order
D
Runtime type casting
16

Question 16

Decorator expression evaluation runs:

A
Top-to-bottom
B
Bottom-to-top
C
Randomly
D
Only for class decorators
17

Question 17

Decorator application order is:

A
Bottom-to-top for stacked decorators
B
Top-to-bottom
C
Left-to-right only
D
Parallel
18

Question 18

In what order do these apply?

typescript
@A
@B
class X {}
A
B then A
B
A then B
C
A only
D
Neither runs
19

Question 19

design:type metadata applies to:

typescript
import 'reflect-metadata';
class T {
  @Reflective
  name: string;
}
A
Property types
B
Variable hoisting
C
Module resolution
D
Namespace merging
20

Question 20

Metadata reflection requires:

A
emitDecoratorMetadata: true
B
resolveJsonModule: true
C
noEmit: true
D
strictNullChecks: false
21

Question 21

Why might this decorator fail?

typescript
function Make(target: any, key: string) {
  target[key] = 10;
}
A
Property decorators cannot set instance values
B
Decorators cannot access keys
C
Property decorators must return a class
D
Decorators cannot modify anything
22

Question 22

Decorators are applied:

A
When the class is evaluated, before any instance exists
B
After an instance is created
C
Only after module import
D
Only for methods
23

Question 23

What does returning a function from a decorator factory allow?

typescript
function Flag(x: boolean) {
  return function(constructor: Function) {};
}
A
Configurable decorators
B
Runtime imports
C
Readonly constructors
D
Keyword aliasing
24

Question 24

Decorators cannot:

A
Modify local variable declarations
B
Wrap constructors
C
Override methods
D
Store metadata
25

Question 25

Where does this decorator attach?

typescript
class Car {
  @Meta
  speed = 10;
}
A
The class prototype
B
The instance
C
The class constructor
D
A static context
26

Question 26

Decorators can be composed by:

A
Stacking multiple decorators in order
B
Using decorator-only files
C
Avoiding metadata
D
Auto-wrapping imports
27

Question 27

Decorators are most commonly used for:

A
Framework-level features like dependency injection and metadata-driven patterns
B
Replacing type inference
C
Module resolution
D
Hoisting functions
28

Question 28

Decorators cannot be applied to:

A
Normal function declarations outside classes
B
Class constructors
C
Static methods
D
Parameter lists
29

Question 29

Returning a new constructor from a class decorator:

A
Replaces the original class
B
Throws an error
C
Removes properties
D
Deletes methods
30

Question 30

Decorator factories allow:

A
Parameterized behavior and reusable decorator logic
B
Implicit import statements
C
Type inference suppression
D
Static-only restrictions
31

Question 31

Decorators run during:

A
Class definition evaluation
B
File load after runtime
C
Type checking only
D
Instance creation
32

Question 32

Decorators are enabled by:

A
Specific compiler flags
B
JS runtime settings
C
Global variables
D
Automatic imports
33

Question 33

Decorators cannot:

A
Be attached to plain functions outside classes
B
Wrap class constructors
C
Modify method descriptors
D
Store metadata for frameworks
34

Question 34

Decorator metadata helps:

A
Frameworks analyze types automatically
B
Changing runtime prototypes
C
Removing property descriptors
D
Skipping type checking
35

Question 35

Overall, decorators enable developers to:

A
Extend class behavior with reusable, expressive abstractions
B
Automatically freeze class instances
C
Replace module resolution logic
D
Modify variable declarations

QUIZZES IN TypeScript