TypeScript TypeScript Classes & Interfaces 2 — Questions and Answers
Question 1: What are TypeScript decorators primarily used for?
- Applying metadata or modifying the behavior of classes, methods, and properties at design time (Correct answer)
- Replacing the `new` keyword
- Adding runtime type checks automatically
- Compiling TypeScript faster
Correct answer: Applying metadata or modifying the behavior of classes, methods, and properties at design time
Decorators are special declarations (prefixed with `@`) that can attach metadata or wrap the behavior of a class, method, accessor, property, or parameter.
Question 2: What does `readonly` do when applied to a class property?
- Makes the property private
- Prevents the property from being reassigned after initialization (Correct answer)
- Makes the property optional
- Hides the property from subclasses
Correct answer: Prevents the property from being reassigned after initialization
`readonly` properties can only be assigned during declaration or in the constructor; any attempt to reassign them later causes a compile-time error.
Question 3: Which TypeScript feature allows a class to have multiple method signatures?
- Generic methods
- Method overriding
- Function overloads (Correct answer)
- Abstract methods
Correct answer: Function overloads
Function (or method) overloads let you define multiple type signatures for the same function, with a single implementation that handles all the cases.
Question 4: What is the `protected` access modifier in TypeScript?
- Accessible only within the declaring class
- Accessible within the declaring class and all subclasses (Correct answer)
- Accessible from anywhere in the module
- Accessible only from static methods
Correct answer: Accessible within the declaring class and all subclasses
`protected` members are accessible within the declaring class and any class that extends it, but not from outside the class hierarchy.
Question 5: In TypeScript, what does interface extending multiple interfaces achieve?
- It causes a compile error because an interface can only extend one parent
- It creates an interface that combines all properties from every extended interface (Correct answer)
- It makes all extended interface properties optional
- It only inherits static members
Correct answer: It creates an interface that combines all properties from every extended interface
An interface can extend multiple interfaces (e.g., `interface C extends A, B`), inheriting all their members into one combined contract.
Question 6: What is a static class member in TypeScript?
- A member that belongs to an instance of the class
- A member that belongs to the class itself and is shared across all instances (Correct answer)
- A member that cannot be accessed after declaration
- A member that is only available in abstract classes
Correct answer: A member that belongs to the class itself and is shared across all instances
Static members are declared with the `static` keyword and belong to the class constructor rather than any instance, accessed via `ClassName.member`.
What are TypeScript decorators primarily used for?