TypeScript

Definition

TypeScript is a statically typed superset of JavaScript that compiles to plain JS. It adds a structural type system (compatibility by shape, not by name), catching a class of errors at build time.


Core Ideas

type vs interface

The most common design question. Practical guidance:

  • type aliases naturally extend JavaScript’s variable-declaration syntax and can describe anything — objects, unions, intersections, primitives, tuples, mapped/conditional types.
  • interfaces have their own syntax and only describe object shapes; using them for non-object types adds needless complexity.
  • Mutability — an interface is open/mergeable (declaration merging lets you re-open and extend it), whereas a type alias is fixed once declared.

Rule of thumb: reach for interface for public object contracts you may want to extend/merge; reach for type for unions, tuples, and anything not a plain object.

Language features

Familiar JS ergonomics carry over with types — e.g. rest parameters (剩余参数, ...args) collect trailing arguments into a typed array. Generics, enums, and utility types round out the system.


Relationships


References

  • TypeScript: type vs interface (dev.to)
  • 一份不可多得的 TypeScript 学习指南