What is an interface in TypeScript?
1. What an interface is
An interface in TypeScript is a data structure contract that describes which properties and methods an object must have, and what their types are.
In other words: an interface says "what data is expected", but does not describe "how it is implemented".
A simple example
interface User {
id: number;
name: string;
isAdmin: boolean;
}
const user: User = {
id: 1,
name: "Tim",
isAdmin: true,
};The compiler checks that the
userobject contains all the fields (id,name,isAdmin) and that their types match the interface.
2. Interfaces are needed only at compile time
Interfaces do not exist in JavaScript code - they are completely removed after compilation. TypeScript uses them only for type checking and IDE hints.
interface Product {
id: number;
title: string;
}After compilation, this code turns into:
// (The interface is gone)3. Optional and read-only properties
Optional properties - the ? mark
interface Product {
id: number;
title: string;
description?: string; // can be omitted
}Read-only - readonly
interface User {
readonly id: number;
name: string;
}
const u: User = { id: 1, name: "Tim" };
u.id = 2; // Error: id cannot be changed4. Interfaces can describe functions
interface Greeter {
(name: string): string;
}
const greet: Greeter = (name) => `Hello, ${name}!`;Here the interface defines the function signature - what arguments it takes and what it returns.
5. Interfaces and classes
Classes can implement interfaces via implements.
interface Person {
name: string;
sayHello(): void;
}
class User implements Person {
constructor(public name: string) {}
sayHello() {
console.log(`Hello, ${this.name}!`);
}
}TypeScript guarantees that the
Userclass implements everythingPersonrequires.
6. Interface inheritance (extends)
Interfaces can be extended, creating a hierarchy of types.
interface Animal {
name: string;
}
interface Dog extends Animal {
bark(): void;
}
const rex: Dog = {
name: "Rex",
bark() {
console.log("Woof!");
},
};
Dogincludes all the fields ofAnimal, plus its own.
7. Index signatures
If the property names are not known in advance, their shape can be described via an index:
interface Dictionary {
[key: string]: string;
}
const colors: Dictionary = {
red: "#ff0000",
green: "#00ff00",
};8. Merging interfaces (Declaration Merging)
If you declare two interfaces with the same name, TypeScript merges them:
interface User {
id: number;
}
interface User {
name: string;
}
const u: User = { id: 1, name: "Tim" }; // merged structureThis is a feature of interfaces -
typecannot do this.
9. Difference between interface and type
| Capability | interface | type |
|---|---|---|
| Describing objects and classes | Yes | Yes |
Extension (extends) | Yes | Yes (&) |
| Union / Intersection | No | Yes |
| Tuples, functions, primitives | Partially | Yes |
| Declaration merging | Yes | No |
Implementation in a class (implements) | Yes | Yes |
The rule is simple:
interface- for objects and classes,type- for combinations and advanced types.
Summary
An interface in TypeScript is a mechanism for describing a data structure (contract): which properties, methods, and data types are expected.
It:
- helps the IDE hint and check code;
- guarantees that objects and classes match the given structure;
- can be inherited and merged;
- does not exist in the resulting JavaScript - it is needed only for typing.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.