惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
量子位
S
SegmentFault 最新的问题
V
Visual Studio Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
D
Docker
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
V
V2EX

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
type vs interface in TypeScript - What You Really Need to...
Rafał Dziędz · 2026-05-06 · via DEV Community

You've definitely come across this debate more than once: type or interface? Most answers online boil down to "it depends" or "use interface for objects, type for everything else." That's not wrong, but it's incomplete. This article breaks the topic down properly.


What is type, what is interface

interface is a contract describing the shape of an object. It's been in TypeScript from the beginning and for years was the way to describe data structures. Semantically it says: "every value of this type must look like this."

type is an alias (you give a name to any type expression). That can be an object, a union, an intersection, a primitive, a tuple, a function, anything.

The core mental distinction: interface defines a shape. type names any type expression.


Where they're identical

Before getting to the differences, let's be clear about what is not a difference, because the internet is full of myths here.

Describing object shapes

interface User {
  id: number;
  name: string;
}

type User = {
  id: number;
  name: string;
};

Enter fullscreen mode Exit fullscreen mode

Identical result. Neither is "better" for describing objects.

Generics

interface Box<T> {
  value: T;
}

type Box<T> = {
  value: T;
};

Enter fullscreen mode Exit fullscreen mode

Both support generics. No difference here.

Extension

// interface extends interface
interface Animal {
  name: string;
}
interface Dog extends Animal {
  breed: string;
}

// type & type
type Animal = { name: string };
type Dog = Animal & { breed: string };

// interface extends type (yes, this works)
type Animal = { name: string };
interface Dog extends Animal {
  breed: string;
}

// type & interface (this works too)
interface Animal {
  name: string;
}
type Dog = Animal & { breed: string };

Enter fullscreen mode Exit fullscreen mode

All four variants compile without issues. extends and & are practically equivalent in most cases, with one exception covered below.


Where they difference

1. Unions - type only

type Status = "active" | "inactive" | "pending"; // ✅

interface Status = "active" | "inactive" | "pending"; // ❌ syntax error

Enter fullscreen mode Exit fullscreen mode

interface cannot describe a union. Its syntax requires curly braces and a list of properties. If you need a union, then you have to use type.

Same goes for conditional types, mapped types, and tuple types:

type MaybeString = string | null;
type Readonly<T> = { readonly [K in keyof T]: T[K] };
type Pair = [string, number];

Enter fullscreen mode Exit fullscreen mode

None of the above can be expressed with interface.

2. Declaration merging - interface only

This is the difference that has real practical consequences.

An interface with the same name declared in two places does not cause an error, it merges:

interface Config {
  timeout: number;
}

interface Config {
  retries: number;
}

// Result: Config = { timeout: number; retries: number }
const c: Config = {
  timeout: 3000,
  retries: 3,
};

Enter fullscreen mode Exit fullscreen mode

With type, this is a compile error:

type Config = { timeout: number };
type Config = { retries: number }; // ❌ Duplicate identifier 'Config'

Enter fullscreen mode Exit fullscreen mode

Type conflict during merging

What if both interfaces declare the same field with conflicting types?

interface A {
  x: number;
}

interface A {
  x: string; // ❌ error - conflict during merging
}

Enter fullscreen mode Exit fullscreen mode

The compiler reports the error at the declaration site, not at the point of use. The merge attempts to combine number and string on field x which is impossible.


3. extends vs & - difference in error reporting

Technically you get the same result, but the compiler behaves differently when something goes wrong.

interface A {
  x: number;
}

interface B extends A {
  x: string; // ❌ immediate error: "Interface 'B' incorrectly extends interface 'A'"
}

Enter fullscreen mode Exit fullscreen mode

type A = { x: number };
type B = A & { x: string };
// No error at declaration!
// But B.x has type: never

Enter fullscreen mode Exit fullscreen mode

With &, the compiler won't complain at the definition, you'll get never on field x, which only surfaces when you try to use it. This is a subtle trap: never instead of an error can slip through code review unnoticed.


When declaration merging is actually needed

There are two specific, intentional use cases of declaration merging.

Augmenting global objects

You want to add a field to the built-in Window:

// globals.d.ts

declare global {
  interface Window {
    analytics: Analytics;
  }
}

Enter fullscreen mode Exit fullscreen mode

Why doesn't type work here?

type Window = { analytics: Analytics }; // ❌

Enter fullscreen mode Exit fullscreen mode

This creates a new type named Window that conflicts with the built-in Window. You're trying to overwrite, instead of extending the original.

interface uses declaration merging. Your declaration gets merged into the existing one. The original Window still exists, analytics is added onto it.

Augmenting types from external libraries

A library exports an interface you want to extend without touching node_modules:

// types/tailwindcss-vite.d.ts

import "@tailwindcss/vite";

declare module "@tailwindcss/vite" {
  interface Theme {
    borderRadius: number;
  }
}

Enter fullscreen mode Exit fullscreen mode

The mechanism: declare module tells the compiler "this is an augmentation of an existing module." Inside, you use declaration merging on interface Theme adding a field to what the library already exports.

Note: you import the module (import "@tailwindcss/vite") before augmenting it. Without this, the compiler doesn't know what to look for.

Using type instead of interface here won't work. type is not subject to augmentation.


Summary

There's no single rule every dev follows, but there are solid heuristics:

Use type when:

  • you need unions, intersections, tuples, mapped types, or conditional types
  • you're describing data (API response, payload, DTO), things that shouldn't be extended by external code
  • you want a duplicate name to fail fast with a compiler error

Use interface when:

  • you're deliberately using declaration merging (augmenting globals, extending library types)
  • you're writing a library and want to give consumers the ability to extend your types via augmentation