TypeScript Interfaces, From Syntax to interface vs type
An interface names the shape of an object: which properties it has and what type each holds. Mark a property optional with ? and unchangeable with readonly, build on another shape with extends, and reopen the same name to merge more members in. Interfaces leave no trace in the compiled JavaScript; they only guide the checker.
| You want | Write |
|---|---|
| A required property | name: string; |
| An optional property | nickname?: string; |
| A property that cannot be reassigned | readonly id: number; |
| A method | greet(other: User): string; |
| Any number of string keys | [key: string]: number; |
| Reuse another shape | interface Admin extends User { ... } |
| Combine several shapes | interface A extends B, C { ... } |
Updated
Add fields in the builder below, tick optional or readonly, and copy the interface or the equivalent type alias. The sections underneath cover extending, merging and the few cases where the choice between interface and type really matters.
Interface builder
interface Product {
readonly id: number;
name: string;
price: number;
tags?: string[];
}interface vs type alias
| Feature | interface | type alias |
|---|---|---|
| Describe an object shape | Yes | Yes |
| Add members by redeclaring the name | Yes (merging) | No, duplicate identifier error |
| Build on another shape | extends | Intersection with & |
| Union of shapes or literals | No | Yes |
| Name a primitive or tuple | No | Yes |
| Mapped and conditional types | No | Yes |
Patterns you will write often
Extending one and several interfaces
Admin gets every User member plus its own; Card combines two unrelated shapes.
interface User {
readonly id: number;
name: string;
email?: string;
}
interface Admin extends User {
permissions: string[];
}
interface Sized { width: number; height: number }
interface Labelled { label: string }
interface Card extends Sized, Labelled {}
const card: Card = { width: 320, height: 180, label: "Plan" };Declaration merging on Window
Placed in a .d.ts file, this lets window.appVersion type check everywhere.
// globals.d.ts
export {};
declare global {
interface Window {
appVersion: string;
}
}Index signature and call signature
Scores accepts any string key; Validator describes a callable value.
interface Scores {
[player: string]: number;
}
interface Validator {
(value: string): boolean;
}
const scores: Scores = { ana: 12, ben: 9 };
const notEmpty: Validator = (v) => v.trim().length > 0;
extends versus intersections
interface Bear extends Animal copies every member of Animal and lets you add more. A type alias does the same job with an intersection, type Bear = Animal & { honey: boolean }.
The handbook notes that extends can be faster for the compiler and that it reports conflicting members right where they are declared. Intersections are handy for gluing together shapes you do not own.
Optional, readonly and index signatures
A question mark after the name makes a property optional, and its read type includes undefined. readonly blocks assignment to that property after creation but is a compile-time check only.
An index signature, [key: string]: number, describes dictionaries whose keys you cannot list in advance. Named properties in the same interface must fit the index signature's value type.
Merging to extend global and library types
Because interfaces merge, a declaration file can add your own fields to built-in shapes. A common case is giving Window a property that a script sets, so reading it type checks without a cast.
How it works
- Name the interface and, if it builds on another one, type that name in the extends box.
- Add a row per property, choosing its type and whether it is optional or readonly.
- Read the interface and the type alias the builder writes from the same rows.
- Copy either version into your code.
Tips
- Start with interface for object shapes and reach for type when you need a union, a tuple or a mapped type. That is the heuristic the TypeScript handbook itself suggests.
- readonly only stops reassignment of that one property. Objects nested inside it can still change, and nothing is enforced at runtime.
- An optional property reads as its type or undefined, so check it before use or give it a default when destructuring.
- Keep interface names singular and descriptive, such as Invoice or CartItem. A prefix like IInvoice adds nothing the editor does not already tell you.
Frequently asked questions
Should I use interface or type?
For object shapes either works and the handbook calls it mostly personal preference, suggesting interface until you need a feature only type has. Use type for unions, tuples, primitives and mapped or conditional types; use interface when you want declaration merging.
What is declaration merging?
Declaring an interface with a name that already exists adds members to it instead of failing. Libraries rely on it so you can extend their types, for example adding a property to Window. A type alias with a repeated name is an error.
What happens if two merged interfaces give a property different types?
The compiler reports an error. With an intersection of two types the conflict is not reported at the declaration; the property becomes never, which surfaces later and is harder to trace.
Can an interface describe a function or an array?
Yes. A call signature such as (value: string): boolean describes a function, and an index signature such as [index: number]: string describes an array-like object. For simple cases a type alias like type Check = (v: string) => boolean reads more clearly.
Do interfaces exist at runtime?
No. They compile to nothing, so you cannot use instanceof with an interface or list its keys. Check values with a runtime guard and annotate its return type with the interface.
Related: TypeScript vs JavaScript, TypeScript enums, React with TypeScript, JSON to TypeScript converter.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $20 a month.
More in Guides
- Loops in TypeScript and the Types They Give You
- TypeScript Map: A Typed Key-Value Collection
- The TypeScript Record Type, Explained With Your Own Keys
- TypeScript vs JavaScript: The Real Difference
- University Web Governance: A Federated Model That Works for Faculties, Labs and Central Teams | How-to Guide
- User Flow Diagrams and Website Wireframes, With Free SVG Downloads
- UUID v4 vs v7: Random IDs or Time-Ordered IDs
- How to View Page Source
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App