Advertisement
Advanced TypeScript, Namespaces & Declaration Merging

Why can't 'type' aliases participate in declaration merging the way 'interface' declarations can, and what design implication does this have?

Short answer

Type aliases are meant to bind a NAME to a single, specific type expression once — allowing the same name to be redeclared with a different type expression would be ambiguous and error-prone (which expression 'wins'?), so TypeScript intentionally disallows it and raises a…

Save TypeScript mock interview
Advertisement
AModel answer
Type aliases are meant to bind a NAME to a single, specific type expression once — allowing the same name to be redeclared with a different type expression would be ambiguous and error-prone (which expression 'wins'?), so TypeScript intentionally disallows it and raises a 'Duplicate identifier' error, unlike interfaces which are explicitly designed around incremental, additive extension.
type Config = { name: string };
// type Config = { retries: number }; // Error: Duplicate identifier 'Config'

interface IConfig { name: string; }
interface IConfig { retries: number; } // OK: interfaces merge
Real-world example
Deciding to use 'interface' instead of 'type' specifically because a type needs to support later extension via declaration merging, like a global augmentation target.
Likely follow-ups
  • Given this difference, why do many style guides still recommend 'type' as the DEFAULT choice for most non-extensible type definitions?
Related topics