Interfaces, Type Aliases and Structural Typing — Concept Check
Questions on Interfaces, Type Aliases and Structural Typing. TypeScript compares shapes, not names. Two unrelated types with the same members are interchangeable — the opposite of how Java works.
Questions in this quiz
8 questions on Interfaces, Type Aliases and Structural Typing — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer defines an interface `Point` with `x` and `y` properties. They also have a class `Vector` with the same properties. What happens when they pass an instance of `Vector` to a function expecting a `Point`?
- AThe function throws a runtime error due to type mismatch.
- BThe function rejects the instance because `Vector` does not explicitly implement `Point`.
- CThe function requires a type assertion to accept the instance.
- DThe function accepts the instance because TypeScript uses structural typing.
2.A developer creates a type alias `User2` and an interface `User` both with `id` and `name` properties. How do these compare in TypeScript?
- AThe interface can only be used for object shapes, not the type alias.
- BThey are equivalent in describing object shapes.
- CThe type alias requires explicit implementation, unlike the interface.
- DThe type alias is more restrictive than the interface.
3.A developer tries to extend an interface `User` with a new interface `Admin`. Which of the following is true?
- A`Admin` can extend `User` using `extends`.
- B`Admin` must redefine all properties of `User`.
- C`Admin` must use intersection types to extend `User`.
- D`Admin` cannot extend `User` as interfaces are not extendable.
4.A developer uses a fresh object literal `{ x: 3, y: 4, z: 5 }` with a function expecting a `Point`. What happens?
- ATypeScript throws an error due to excess property check.
- BThe object is accepted after a type assertion.
- CThe function throws a runtime error for type mismatch.
- DThe function accepts the object without issues.
5.A developer wants to use a union type for a variable that can be a `string` or `number`. Which should they use?
- AUse a record type with dynamic keys.
- BUse an interface with multiple properties.
- CUse a type alias with union types.
- DUse a class with conditional properties.
6.A developer defines a `readonly` property in an interface. What is true about this property?
- AIt requires a type assertion to modify.
- BIt can be modified anytime after creation.
- CIt can only be assigned a value at creation.
- DIt behaves like a constant in all contexts.
7.A developer uses an index signature in an interface for dynamic keys. What is a correct alternative?
- AUse a class with dynamic properties.
- BUse an interface with explicit keys.
- CUse a union type for dynamic keys.
- DUse a `Record` type for more readability.
8.A developer defines `Counts` using `Record<Level, number>`. What happens if a key is missing?
- ATypeScript throws an error for the missing key.
- BA runtime error occurs due to the missing key.
- CThe missing key is automatically set to `undefined`.
- DThe code runs without issues.