Share E-Book

TypeScript 教程 (阮一峰)(Z-Library)

Author 阮一峰

TypeScript
Language Chinese

No Description

Format EPUB
Size 224.1 KB
123
Views
0
Downloads
0.00
Total Donations

AI Guide

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

Full assistant
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.

Passage locations

Excerpt 1
利于代码的静态分析。 有了静态类型,不必运行代码,就可以确定变量的类型,从而推断代码有没有错误。这就叫做代码的静态分析。 这对于大型项目非常重要,单单在开发阶段运行静态检查,就可以发现很多问题,避免交付有问题的代码,大大降低了线上风险。 (2)有利于发现错误。 由于每个值、每个变量、每个运算符都有严格的类型约束,...
View in text
Excerpt 2
s-node,也可以通过 npx 调用它来运行 TypeScript 脚本。 $ npx ts-node script.ts 上面命令中, npx 会在线调用 ts-node,从而在不安装的情况下,运行 script.ts 。 如果执行 ts-node 命令不带有任何参数,它会提供一个 TypeScript 的命...
View in text
Excerpt 3
er = 1 ; const n2 : Number = 1 ; Math . abs (n1) // 1 Math . abs (n2) // 报错 上面示例中, Math.abs() 方法的参数类型被定义成小写的 number ,传入大写的 Number 类型就会报错。 上一小节说过, Symbol() 和...
View in text
Excerpt 4
typeof 的操作数是一个值。 JavaScript 里面, typeof 运算符只可能返回八种结果,而且都是字符串。 typeof undefined ; // &quot;undefined&quot; typeof true ; // &quot;boolean&quot; typeof 1337 ; /...
View in text

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
Back to List