Easy10 minType Systems
UpdatedAug 1, 2026
Edit

interface vs type

CONCEPTS:Structural vs Nominal Typing

Question Variations

  • "What is the difference between `interface` and `type` in TypeScript?"
  • "When would you prefer using a `type` over an `interface`?"
  • "Explain the concept of 'declaration merging' and which keyword supports it."
  • "How do you extend an `interface` compared to extending a `type` alias?"

Why This Is Asked

This is a quick litmus test for TypeScript experience. Interviewers want to see if you know the practical differences — especially declaration merging — and have a reasoned opinion on when to use each, rather than just a default preference.

Key Concepts

  • interface supports declaration merging (defining the same interface twice merges them), type does not
  • type can represent unions, intersections, tuples, mapped types, and primitives; interface cannot
  • Both can describe object shapes and be extended (interface via extends, type via &)
  • interface is generally preferred for public API contracts and library definitions (mergeability)
  • type is preferred for complex type operations, utility types, and union/intersection patterns

Question Variations

  • “What is the difference between interface and type in TypeScript?”
  • “When would you prefer using a type over an interface?”
  • “Explain the concept of ‘declaration merging’ and which keyword supports it.”
  • “How do you extend an interface compared to extending a type alias?”

Answers by Technology

+ Add Variant

Expected Answer (PHP 8.5)

PHP has a structural difference in how it handles “types” compared to TypeScript:

  1. Interfaces: The primary way to define a contract. A class must explicitly implement an interface.
  2. Type Hinting: PHP uses native types (string, int, bool, array, object) and class/interface names for type hinting.
  3. Union & Intersection Types: PHP 8.0 introduced string|int (Union) and PHP 8.1 introduced Iterator&Countable (Intersection).
  4. DNF Types (PHP 8.2): Disjunctive Normal Form allows combining unions and intersections, e.g., (A&B)|C.

Why It Matters

PHP is a dynamically typed language with an increasingly powerful static type system. Unlike TypeScript, PHP’s types are enforced at runtime. Understanding the difference between an Interface (a contract) and a Type Hint (a constraint) is key to writing modern, safe PHP.

Code Example

interface Logger {
    public function log(string $message): void;
}

// Union Type Example
function process(string|int $input) {
    // ...
}

// Intersection Type Example
function handle(Iterator&Countable $collection) {
    echo count($collection);
}

Common Mistakes

  • Confusing Type Hinting with Type Casting: Type hints enforce the type at the entry point of a function; they don’t automatically convert the data (unless strict_types is off and the data is compatible).
  • Over-using mixed: Using the mixed type hint defeats the purpose of the type system and should be avoided in favor of specific Union types.

Follow-up Questions

  • What does declare(strict_types=1); do? (Answer: It forces strict type checking for function calls, preventing the automatic coercion of types like ‘1’ into 1).
  • Can you define a “Type Alias” in PHP? (Answer: No, PHP does not have a type keyword. You must use interfaces or DTOs to represent complex shapes).

Expected Answer

Both interface and type can describe object shapes, but they differ in capabilities and intended use:

  • interface: Primarily used for defining the “contract” of an object. Its most unique feature is Declaration Merging, where multiple definitions of the same interface are combined into one.
  • type alias: More versatile. It can define unions, intersections, primitives, tuples, and mapped types—none of which an interface can do directly.

Why It Matters

Consistency and extensibility. Using interface for public APIs allows consumers to augment your types (e.g., adding a property to a library’s Request object). Using type is essential for complex type logic (unions, utility types) that makes TypeScript powerful. Mixing them without a strategy can lead to confusing codebases and blocked extension points.

Example Code

Interface: Extension & Merging

interface User {
  id: string;
}

// Declaration Merging (Merged with the above)
interface User {
  name: string;
}

interface Admin extends User {
  role: 'admin';
}

Type: Unions & Intersections

type Result = Success | Error; // Union (Interface can't do this)
type Point = [number, number];  // Tuple

type Admin = User & { role: 'admin' }; // Intersection

Common Mistakes

  • “Always use interface”: A common outdated rule. type is mandatory for unions, mapped types, and conditional types.
  • “Always use type”: This prevents consumers from using declaration merging, which is critical when writing libraries or augmenting global objects (like window or ProcessEnv).
  • Confusing extends with &: While similar, extends provides better error messages by checking for compatibility, whereas & can silently result in never if types conflict.

Follow-up Questions

  • What happens when you intersect two types with conflicting properties? (Answer: The property type becomes never. For example, { x: string } & { x: number } results in x: never).
  • Can a class implement a type alias? (Answer: Yes, as long as the type alias represents an object shape, not a union or a primitive).

References