Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A practical, example-driven introduction to TypeScript for JavaScript developers who want static typing for safer, more maintainable code — ideal for team projects and anyone moving from JS to TS.
【Book Arc】
- **Opening (~0%–10%)**: Explains what TypeScript is — a superset of JavaScript with a static type system — and why it matters. Covers the benefits (early error detection, IDE support, refactoring) and drawbacks (less flexibility, extra work), plus a brief history and how to get started with the TypeScript Playground and the `tsc` compiler.
- **Early (~10%–23%)**: Walks through the essential tooling: compiling with `tsc`, using `tsconfig.json`, and running TS directly with `ts-node`. Introduces the core type system, including `any` and `unknown`, and explains why `unknown` is the safer choice — it prevents type pollution and requires type narrowing before use.
- **Early (~23%–32%)**: Dives into the type system fundamentals: the eight basic JavaScript types, the difference between primitive values and wrapper objects, and the crucial distinction between uppercase (`String`) and lowercase (`string`) types. Also covers `Object` vs. `object` and the special behavior of `undefined` and `null`.
- **Middle (~32%–48%)**: Explores advanced type concepts: literal (value) types, union types (`|`), intersection types (`&`), and the `type` command for aliases. Emphasizes type narrowing as the standard way to handle union types safely, and introduces the `typeof` operator for type queries.
- **Middle (~48%–end)**: Moves into structured data types, starting with arrays — their fixed member type, dynamic length, and two main syntaxes (`number[]` and `Array<number>`). Sets up the foundation for tuples, interfaces, and generics covered in later chapters.
【Key Takeaways】
- **Static typing catches errors at compile time, not runtime** (Early): TypeScript's type system flags mistakes like typos, invalid operations, and wrong method calls during development, saving debugging time and reducing production bugs.
- **`unknown` is the safer alternative to `any`** (Early): While `any` disables type checking and can "pollute" other variables, `unknown` requires type narrowing before use — making it the recommended choice for values of uncertain type.
- **`never` is the bottom type** (Early): It represents an empty set of values, useful for functions that never return (e.g., always throw) and for exhaustiveness checks in union type handling.
- **Use lowercase types (`string`, `number`) over uppercase (`String`, `Number`)** (Early): Lowercase types match literal values and built-in method signatures; uppercase types include wrapper objects and often cause errors with standard APIs.
- **Union types require type narrowing** (Middle): When a variable can be multiple types (e.g., `string | number`), you must check its actual type (via `typeof`, `switch`, etc.) before using type-specific operations — this is the standard pattern for safe handling.
- **Intersection types combine object shapes** (Middle): Using `&`, you can merge multiple object types into one, which is handy for adding properties to existing types without repetition.
- **Arrays have a fixed member type but dynamic length** (Middle): TypeScript doesn't check array bounds, so accessing out-of-range indices won't error — be mindful of this when working with dynamic data.
【Reading Tips】
- **Skim the history and motivation sections** (Opening): They provide context but aren't essential for coding. Focus instead on the "static vs. dynamic" comparison and the benefits list.
- **Deep-read the `any` vs. `unknown` section** (Early): This is a common source of confusion and a key best practice. Understand why `unknown` prevents pollution and how type narrowing unlocks its use.
- **Pay close attention to the uppercase vs. lowercase type distinction** (Early): This trips up many beginners. Remember: always prefer lowercase for primitives and `object` over `Object`.
- **Practice with the TypeScript Playground** (Opening): The book recommends it, and it's the fastest way to test examples and see compiled JavaScript output without local setup.
- **Take your time with union types and narrowing** (Middle): This is the core pattern you'll use daily. Work through the examples until you're comfortable with `typeof` checks and `switch` statements.
【Coverage Limits】
This guide covers the opening through the array chapter (~48% of the book). Later chapters on tuples, interfaces, generics, and advanced type operations are not included in this summary.
只读元组都可以。 上面示例报错的解决方法,就是使用类型断言,在最后一行将传入的参数断言为普通元组,详见《类型断言》一章。 distanceFromOrigin ( point as [ number , number ] ) 成员数量的推断 如果没有可选成员和扩展运算符,TypeScript 会推断出元组的成员数...
; 上面示例中,函数 f() 本身还有一个属性 version 。这时, f 完全就是一个对象,类型就要使用对象的写法。 let foo : { ( x : number ): void ; version : string } = f; 函数类型也可以使用 Interface 来声明,这种写法就是对象写法的翻版...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
TypeScript 教程 (阮一峰)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share to WeChat Moments
TypeScript 教程 (阮一峰)(Z-Library)
Scan the QR code with WeChat to open this book
Tap the ··· menu in the top-right corner
Choose Share to Moments
You are already in WeChat. Tap ··· in the top-right corner and choose Share to Moments.
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
TypeScript 教程 (阮一峰)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment