Mastering Interfaces and Type Aliases Quiz Quiz

Explore the differences and practical uses of interfaces and type aliases in TypeScript. This quiz helps you reinforce core concepts, such as declaration, extension, implementation, and compatibility, essential for mastering advanced type definitions.

  1. Distinguishing Between Interfaces and Type Aliases

    Which of the following statements is true about the differences between interfaces and type aliases in TypeScript?

    1. Interfaces can be used to describe unions, but type aliases cannot.
    2. Type aliases support method signatures directly, while interfaces do not.
    3. Type aliases can extend multiple interfaces simultaneously, but interfaces cannot extend type aliases.
    4. Interfaces can be merged using declaration merging, while type aliases cannot.

    Explanation: The correct answer is that interfaces allow for declaration merging, enabling their definitions to be combined across multiple blocks, while type aliases do not support this feature. Distractors are incorrect because type aliases can extend interfaces, but so can interfaces; interfaces cannot be used to describe union types, but type aliases can; and both interfaces and type aliases support method signatures. Therefore, declaration merging is the main distinctive feature tested here.

  2. Extending and Implementing Types

    Given an interface Animal and a type alias Mammal, which is the correct way to make a class Dog implement both in TypeScript?

    1. class Dog inherit Animal, Mammal {}
    2. class Dog implements Animal extends Mammal {}
    3. class Dog extends Animal, Mammal {}
    4. class Dog implements Animal, Mammal {}

    Explanation: Classes in TypeScript can implement multiple interfaces or type aliases by separating them with commas after the 'implements' keyword. The 'extends' keyword is used for class inheritance, not for implementing interfaces or types, making 'extends' and 'inherit' options incorrect. Using both 'implements' and 'extends' together in a single clause is not syntactically supported. Only the correct option demonstrates the proper usage.

  3. Composing Types with Unions and Intersections

    If you want to define a type that represents either a Square or a Circle, which construct must you use?

    1. An interface with an intersection type
    2. A type alias with a union type
    3. A type alias with a literal type
    4. An interface with declaration merging

    Explanation: To represent a value that can be one of several types, such as a Square or a Circle, you must use a union type, which is possible with type aliases. Interfaces do not support union types or declaration merging for this use case. Intersections combine types rather than provide alternatives, and literal types define specific values rather than broad type options. Therefore, only the first option is correct.

  4. Interface Extension Limitations

    Which statement correctly describes the limitations of interfaces extending other types in TypeScript?

    1. Interfaces cannot extend type aliases that are unions or intersections.
    2. Interfaces always require implementation by a class.
    3. Interfaces can extend any type alias, including those representing primitive types.
    4. Interfaces are not allowed to extend other interfaces.

    Explanation: Interfaces can only extend type aliases if those type aliases resolve to object types, but not unions or intersections directly. Type aliases can represent primitive, union, or intersection types, but interfaces cannot extend type aliases that don't represent objects. Interfaces can certainly extend other interfaces. Interfaces do not always need to be implemented by a class; they can be used for structural typing alone, making options 2, 3, and 4 incorrect.

  5. Structural Compatibility of Interfaces vs. Type Aliases

    Given two identically structured object types defined with an interface and a type alias, what happens when you assign one to the other in TypeScript?

    1. The assignment is only valid if explicit casting is used.
    2. The assignment throws an error about incompatible types.
    3. The assignment requires a type assertion keyword.
    4. The assignment is allowed due to structural typing.

    Explanation: TypeScript uses structural typing, meaning types are compatible if their structures match, regardless of whether they are defined with an interface or a type alias. Assigning values between two identical structures is valid and does not require type assertions or explicit casting. Errors only occur if the shapes differ. Thus, only the first option accurately describes this behavior.