What TypeScript Does
- Static types — interfaces, type aliases, unions, intersections
- Generics — parameterized types for reusable code
- Type inference — infer types from values and signatures
- Type narrowing — refine types via checks (typeof, instanceof, discriminators)
- Utility types — Partial, Required, Pick, Omit, Record, Awaited
- Declaration files —
.d.tsfor typing existing JS libraries - Module resolution — classic, node, bundler strategies
- JSX support — TSX for React, Vue, Solid
- Project references — incremental builds across packages
- Strict mode — strict null checks, no implicit any
Architecture
TSC compiler written in TypeScript itself. Parses source to AST, performs type checking using a structural type system, then emits JavaScript. Language Server (tsserver) powers IDE features via LSP.
Self-Hosting
Language tool, ships via npm.
Key Features
- Structural type system
- Type inference
- Generics
- Discriminated unions
- Utility types
- Declaration files for JS libraries
- JSX/TSX support
- Project references
- Incremental builds
- Rich LSP integration
Comparison
| Language | Types | Compiles To | Runtime |
|---|---|---|---|
| TypeScript | Static + inference | JavaScript | Node/Browser/Bun/Deno |
| Flow | Static | JavaScript | Same |
| Hegel | Static | JavaScript | Same |
| ReScript | Static | JavaScript | Same |
| PureScript | Static | JavaScript | Same |
| Elm | Static | JavaScript | Browser |
常见问题 FAQ
Q: 什么时候用 TypeScript? A: 任何 JS 项目超过 1000 行、团队规模 > 1、长期维护。小脚本可以不用。
Q: Strict 模式必须开吗?
A: 强烈建议。关掉严格模式等于放弃 TypeScript 的核心价值。可以用 @ts-expect-error 标注临时例外。
Q: 编译慢?
A: v5+ 引入 project references 和增量构建。大项目用 tsc -b 构建模式。也可用 swc、esbuild 只做转换跳过类型检查(配合 tsc --noEmit 单独检查)。
来源与致谢 Sources
- Docs: https://www.typescriptlang.org/docs
- GitHub: https://github.com/microsoft/TypeScript
- License: Apache 2.0