Загрузка…
Загрузка…
TypeScript · middle · сложность 5
Только interfaces merge — так augmentят third-party/global types:
// Only interfaces merge — this is how you augment third-party/global types
interface Window { myAnalytics: (e: string) => void; }
interface Window { featureFlags: Record<string, boolean>; } // merges, both exist
window.myAnalytics('signup'); // ✅ typed
// Only type aliases can name non-object types
type ID = string | number; // union — impossible as an interface
type Point = [number, number]; // tuple
type Handler = (e: Event) => void; // function type (reads cleanly)
type Nullable<T> = T | null; // generic alias over anything
// Extension: both work for objects, different machinery
interface Animal { name: string; }
interface Dog extends Animal { bark(): void; } // named inheritance
type Cat = Animal & { meow(): void; }; // intersection
// ❌ can't do this — alias for a shape can't be reopened
type Cfg = { a: number };
type Cfg = { b: number }; // Error: Duplicate identifier 'Cfg'// Subtle: interface extends enforces compatibility; intersection silently narrows to never
interface A { x: number }
// interface B extends A { x: string } // ❌ Error: incompatible — caught early
type C = A & { x: string }; // no error; C['x'] is `never` — a silent trapInterface — open, merge, ранние ошибки на extends. Type — unions/tuples; intersection может тихо дать never.