About TypeScript
TypeScript builds on JavaScript with a type system that helps catch mistakes before code runs. Its types are removed when producing JavaScript, so the result works in browsers and JavaScript runtimes.
- Large web applications
- Shared API types
- Developer tooling
- Created by
- Microsoft, with Anders Hejlsberg as a key designer
- First released
- 2012 · first public release
- Version / standard
- TypeScript 7.0
Current stable release line. Your project may use an earlier compiler.
In the real world
Facts checked 8 September 2026. Links open the sources; examples describe specific products or documented uses.
Syntax
interface Name {
property: type;
}Examples
A small working example
Follow the values through this example, then change one input.
interface Lesson {
title: string;
completed?: boolean;
}
const lesson: Lesson = { title: "Interfaces" };
console.log(lesson.title);Basic Interface
Defining a simple interface for object structure.
interface User {
id: number;
name: string;
email: string;
age: number;
}
const user: User = {
id: 1,
name: "Alice",
email: "alice@example.com",
age: 25
};
function displayUser(user: User) {
console.log(`${user.name} (${user.email})`);
}
displayUser(user);Optional Properties
Using optional properties and readonly modifiers.
interface Product {
id: number;
name: string;
price: number;
description?: string; // Optional
readonly category: string; // Cannot be modified
}
const product: Product = {
id: 101,
name: "Laptop",
price: 999.99,
category: "Electronics"
};
// product.category = "Tech"; // Error: readonly property
console.log(product);
// Using optional property
if (product.description) {
console.log(product.description);
}Extending Interfaces
Creating new interfaces by extending existing ones.
interface Person {
name: string;
age: number;
}
interface Employee extends Person {
employeeId: number;
department: string;
salary: number;
}
const employee: Employee = {
name: "Bob",
age: 30,
employeeId: 12345,
department: "Engineering",
salary: 75000
};
// Multiple inheritance
interface Manager extends Employee, Person {
teamSize: number;
managerId: number;
}
console.log(employee);Function Types
Using interfaces to define function signatures.
interface MathOperation {
(a: number, b: number): number;
}
const add: MathOperation = (a, b) => a + b;
const subtract: MathOperation = (a, b) => a - b;
console.log(add(5, 3)); // 8
console.log(subtract(10, 4)); // 6
// Interface for object with methods
interface Calculator {
add(a: number, b: number): number;
subtract(a: number, b: number): number;
multiply(a: number, b: number): number;
}
const calc: Calculator = {
add: (a, b) => a + b,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b
};Best practices
- Use interfaces for object shapes and type for unions/intersections
- Prefix interface names with "I" only if your team convention requires it
- Mark properties as readonly when they shouldn't be modified after creation
- Use optional properties (?) for truly optional fields
- Extend interfaces when creating specialized versions of base types
- Document interfaces with JSDoc comments for better IDE tooltips
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts · .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
In plain English
An interface describes the shape an object should have. TypeScript checks that shape before execution; the interface does not become a runtime validator.
What you’ll learn
- Describe an object shape.
- Mark an optional property.
- Separate type checking from runtime validation.
Before you start: Basic Types · Variables: var, let, const
Breaking down the syntax
interface- Names an object type.
property: type- Describes a member and its expected type.
?- Marks a property as optional.
How it works
Describe
List the expected members.
Check
The compiler compares values with the shape.
Execute
The resulting JavaScript runs without the interface declaration.
When should I use this?
Name object contracts shared by functions and components.
Common mistakes
A common trap
A required property must be present.
Incorrect
interface User { name: string }
const user: User = {};Corrected
interface User { name: string }
const user: User = { name: "Ada" };Compare approaches
- Interface: Object shapes that may be extended or declaration-merged.
- Type alias: Name a type, including a union or primitive.
Explore deeper
Runtime data still needs checking
Type annotations are erased. Validate untrusted API responses at runtime instead of relying on a type assertion.
Specifications & further reading
Related TypeScript documentation
Both type and interface can describe object shapes, and for that common case they are largely interchangeable. The key differences: interface supports declaration merging (multiple declarations with the same name combine automatically) and can only describe object-like shapes, while type can alias anything - unions, tuples, primitives - but cannot be re-opened once declared.Index Signatures
An index signature lets an interface or type describe an object with dynamic keys of a known type, when you don't know the exact property names in advance - only their pattern. This is common for dictionary-like objects, such as a lookup table keyed by user ID or product SKU.Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.