Object Types and Interfaces in TypeScript Quiz (original) (raw)
What is the primary purpose of defining object types in TypeScript?
- To ensure proper data encapsulation
- To specify the structure and shape of objects
- To enforce access modifiers
- To dynamically change an object's properties
Which of the following best describes object types in TypeScript?
- They define the behavior of a class.
- They enforce the structure of an object.
- They allow dynamic type creation at runtime.
- They enable asynchronous operations on objects.
What feature is specific to interfaces in TypeScript?
- The ability to define unions of types.
- The ability to extend other interfaces.
- The ability to define dynamic property keys.
- The ability to create conditional types.
Which of the following is a key benefit of type aliases in TypeScript?
- They are faster to compile than interfaces.
- They can define unions and intersections.
- They cannot be reassigned once declared.
- They enforce stricter type checking than interfaces.
What is a primary advantage of using interfaces over type aliases?
- They allow merging of declarations.
- They provide better runtime performance.
- They are easier to convert to JavaScript.
- They allow defining callback functions.
Which of the following situations is best suited for type aliases?
- Defining object shapes with optional properties.
- Representing a union type like number | string
- Extending a reusable object structure.
- Defining an interface for a class implementation.
What is the recommended approach when you need to define a reusable object shape that might need extension in the future?
What happens if an object does not conform to the defined object type in TypeScript?
- The object will still compile but may cause runtime errors.
- The TypeScript compiler throws an error.
- The object is automatically adjusted to fit the type.
- The object type is ignored entirely.
Which of the following can be included in a TypeScript interface?
- Constructor implementations
- Global variable declarations
Why might you prefer a type alias over an interface for certain definitions?
- Type aliases allow merging declarations.
- Type aliases can represent union and intersection types.
- Type aliases are faster to compile than interfaces.
- Type aliases enforce stricter type safety.
There are 10 questions to complete.
Take a part in the ongoing discussion