Unlock the power of TypeScript decorators with 30 MCQs designed to test your knowledge on class, method, property decorators, parameter decorators, and the metadata reflection API to improve your coding skills.
Chapter 14: Decorators in TypeScript
Introduction to Decorators
What is a decorator in TypeScript?
a) A function that modifies the behavior of a class, method, or property
b) A way to define variables in TypeScript
c) A function that adds event listeners to elements
d) A class that handles metadata
Which of the following is the correct syntax to define a decorator in TypeScript?
a) @decorator()
b) @decorator
c) function decorator() {}
d) decorator@()
Where can decorators be applied in TypeScript?
a) Only to classes
b) Only to methods
c) Classes, methods, properties, and parameters
d) Only to functions
Which TypeScript feature must be enabled to use decorators?
a) strictNullChecks
b) experimentalDecorators
c) esModuleInterop
d) noImplicitAny
What is the role of a decorator factory?
a) It creates a new class
b) It is used to create a decorator with parameters
c) It defines the type of a class
d) It adds methods to a class
Class, Method, and Property Decorators
Which of the following is a valid class decorator?
a) @classDecorator()
b) @classDecorator
c) @decoratorClass()
d) @decorator()
What is the main purpose of a class decorator?
a) To modify the class’s prototype
b) To modify the class’s constructor or add properties
c) To attach event listeners to the class
d) To prevent the class from being instantiated
What is the correct syntax for a method decorator?
a) @methodDecorator()
b) @methodDecorator
c) method @decorator()
d) method @decorator
What does a method decorator receive as arguments?
a) The class constructor, property key, and descriptor
b) Only the property key
c) The class and the method parameters
d) The instance of the class
Which of the following describes a property decorator?
a) It can be applied only to static properties
b) It can modify a property in a class
c) It is used to bind methods to events
d) It adds private or protected properties
Parameter Decorators
What is a parameter decorator used for in TypeScript?
a) To modify the behavior of a method
b) To add metadata to method parameters
c) To define the type of a method parameter
d) To prevent method invocation
How is a parameter decorator defined in TypeScript?
a) @parameterDecorator(parameterIndex)
b) @parameterDecorator()
c) parameterDecorator(parameterIndex)
d) @parameterDecorator(parameterIndex, target)
What information does a parameter decorator have access to?
a) The method’s name and the class’s constructor
b) The parameter’s index and the method’s prototype
c) The parameter’s type
d) The method’s return type
Which of the following is true about parameter decorators?
a) They can be used to modify the return value of methods
b) They only work on instance methods
c) They can’t modify parameter values, only add metadata
d) They are used to add event listeners
When are parameter decorators invoked in TypeScript?
a) During method definition
b) After the method is called
c) When the method is invoked
d) When the class is instantiated
Metadata Reflection API
What does the Metadata Reflection API in TypeScript provide?
a) A way to reflect on and analyze the types and metadata of classes and their members
b) A way to modify a class’s metadata dynamically
c) A way to generate classes dynamically
d) A method for type checking at runtime
Which library needs to be imported to use the Metadata Reflection API?
a) reflect-metadata
b) ts-metadata
c) type-reflect
d) meta-reflection
How do you define metadata using the reflection API?
a) Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey)
b) Reflect.metadata(metadataKey, metadataValue)
c) Reflect.setMetadata(target, metadataKey, metadataValue)