关于本手册
🌐 About this Handbook
自从JavaScript引入编程社区已经过去20多年,它现在已成为有史以来最广泛使用的跨平台语言之一。JavaScript最初作为一种用于为网页添加简单交互功能的小型脚本语言,现已发展成为前端和后端各种规模应用的首选语言。随着用JavaScript编写的程序的规模、范围和复杂性呈指数增长,JavaScript语言表达不同代码单元之间关系的能力却没有相应提高。再加上JavaScript相当特殊的运行时语义,这种语言与程序复杂性之间的不匹配,使得在大规模开发中管理JavaScript变得十分困难。
🌐 Over 20 years after its introduction to the programming community, JavaScript is now one of the most widespread cross-platform languages ever created. Starting as a small scripting language for adding trivial interactivity to webpages, JavaScript has grown to be a language of choice for both frontend and backend applications of every size. While the size, scope, and complexity of programs written in JavaScript has grown exponentially, the ability of the JavaScript language to express the relationships between different units of code has not. Combined with JavaScript’s rather peculiar runtime semantics, this mismatch between language and program complexity has made JavaScript development a difficult task to manage at scale.
程序员最常写出的错误可以被描述为类型错误:在需要某种类型的值时使用了不同类型的值。这可能是由于简单的打字错误、对库的 API 不了解、对运行时行为的错误假设或其他错误造成的。TypeScript 的目标是为 JavaScript 程序提供静态类型检查——换句话说,它是在代码运行之前运行的工具(静态的),并确保程序的类型是正确的(类型检查)。
🌐 The most common kinds of errors that programmers write can be described as type errors: a certain kind of value was used where a different kind of value was expected. This could be due to simple typos, a failure to understand the API surface of a library, incorrect assumptions about runtime behavior, or other errors. The goal of TypeScript is to be a static typechecker for JavaScript programs - in other words, a tool that runs before your code runs (static) and ensures that the types of the program are correct (typechecked).
如果你是在没有 JavaScript 背景的情况下接触 TypeScript,并打算让 TypeScript 成为你的第一语言,我们建议你先阅读 Microsoft Learn JavaScript 教程 或 Mozilla Web Docs 上的 JavaScript 文档。 如果你有其他语言的经验,你应该能够通过阅读手册很快掌握 JavaScript 语法。
🌐 If you are coming to TypeScript without a JavaScript background, with the intention of TypeScript being your first language, we recommend you first start reading the documentation on either the Microsoft Learn JavaScript tutorial or read JavaScript at the Mozilla Web Docs. If you have experience in other languages, you should be able to pick up JavaScript syntax quite quickly by reading the handbook.
本手册的结构
🌐 How is this Handbook Structured
手册分为两部分:
🌐 The handbook is split into two sections:
-
手册
TypeScript 手册旨在成为一份全面的文档,向日常程序员解释 TypeScript。你可以通过左侧导航从上到下阅读手册。
你应当期望每一章或每一页都能让你深入理解所讲的概念。《TypeScript 手册》并不是完整的语言规范,但它旨在成为对语言所有特性和行为的全面指南。
完成演练的读者应该能够:
- 阅读并理解常用的 TypeScript 语法和模式
- 解释重要编译器选项的影响
- 在大多数情况下正确预测类型系统行为
为了清晰和简明起见,本手册的主要内容不会探讨所涵盖功能的每一个边缘情况或细节。你可以在参考文章中找到有关特定概念的更多详细信息。
-
参考文件
导航中手册下方的参考部分旨在提供对 TypeScript 某个特定部分工作原理的更深入理解。你可以从头到尾阅读,但每个部分都旨在对单个概念进行更深入的解释——也就是说,并不追求内容的连续性。
非目标性
🌐 Non-Goals
该手册还旨在成为一本简明的文档,可以在几小时内轻松阅读。为了保持内容简短,某些主题将不会被涵盖。
🌐 The Handbook is also intended to be a concise document that can be comfortably read in a few hours. Certain topics won’t be covered in order to keep things short.
具体来说,本手册并未全面介绍 JavaScript 的核心基础知识,如函数、类和闭包。在适当的情况下,我们会提供相关的背景阅读链接,供你深入了解这些概念。
🌐 Specifically, the Handbook does not fully introduce core JavaScript basics like functions, classes, and closures. Where appropriate, we’ll include links to background reading that you can use to read up on those concepts.
本手册也并不打算替代语言规范。在某些情况下,为了提供更高级、易于理解的解释,可能会忽略一些边缘情况或行为的正式描述。相反,有单独的参考页面更精确且正式地描述了 TypeScript 行为的许多方面。参考页面并不适合不熟悉 TypeScript 的读者,因此可能使用高级术语或引用你尚未阅读的主题。
🌐 The Handbook also isn’t intended to be a replacement for a language specification. In some cases, edge cases or formal descriptions of behavior will be skipped in favor of high-level, easier-to-understand explanations. Instead, there are separate reference pages that more precisely and formally describe many aspects of TypeScript’s behavior. The reference pages are not intended for readers unfamiliar with TypeScript, so they may use advanced terminology or reference topics you haven’t read about yet.
最后,本手册不会涉及 TypeScript 如何与其他工具交互,除非在必要情况下涉及。诸如如何将 TypeScript 与 webpack、rollup、parcel、react、babel、closure、lerna、rush、bazel、preact、vue、angular、svelte、jquery、yarn 或 npm 配置的主题不在本手册范围内——你可以在网上找到这些资源。
🌐 Finally, the Handbook won’t cover how TypeScript interacts with other tools, except where necessary. Topics like how to configure TypeScript with webpack, rollup, parcel, react, babel, closure, lerna, rush, bazel, preact, vue, angular, svelte, jquery, yarn, or npm are out of scope - you can find these resources elsewhere on the web.
开始使用
🌐 Get Started
在开始学习 基础知识 之前,我们建议先阅读以下任意一篇入门页面。这些介绍旨在突出 TypeScript 与你喜欢的编程语言之间的主要相似点和差异,并澄清这些语言特有的常见误解。
🌐 Before getting started with The Basics, we recommend reading one of the following introductory pages. These introductions are intended to highlight key similarities and differences between TypeScript and your favored programming language, and clear up common misconceptions specific to those languages.
否则,请跳转到 基础知识。
🌐 Otherwise, jump to The Basics.