跳到内容

TypeScript 入门

🌐 Introduction to TypeScript

什么是 TypeScript

🌐 What is TypeScript

TypeScript 是由微软维护和开发的开源语言。

基本上,TypeScript 为 JavaScript 添加了额外的语法,以便与编辑器实现更紧密的集成。在编辑器或 CI/CD 流水线中提前捕获错误,并编写更易维护的代码。

🌐 Basically, TypeScript adds additional syntax to JavaScript to support a tighter integration with your editor. Catch errors early in your editor or in your CI/CD pipeline, and write more maintainable code.

我们可以稍后讨论 TypeScript 的其他好处,现在让我们看一些例子!

🌐 We can talk about other TypeScript benefits later, let's see some examples now!

第一个 TypeScript 代码

🌐 First TypeScript code

看一下这个代码片段,然后我们可以一起解压它:

🌐 Take a look at this code snippet and then we can unpack it together:

type  = {
  : string;
  : number;
};

function (: ): boolean {
  return . >= 18;
}

const  = {
  : 'Justine',
  : 23,
} satisfies ;

const  = ();

第一部分(使用 type 关键字)负责声明我们表示用户的自定义对象类型。随后,我们利用这个新创建的类型来创建函数 isAdult,该函数接受一个 User 类型的参数并返回一个 boolean 值。之后,我们创建了示例数据 justine,可以用它来调用之前定义的函数。最后,我们创建了一个新变量,用于存储 justine 是否为成年人的信息。

🌐 The first part (with the type keyword) is responsible for declaring our custom object type representing users. Later we utilize this newly created type to create the function isAdult that accepts one argument of type User and returns a boolean. After this, we create justine, our example data that can be used for calling the previously defined function. Finally, we create a new variable with information on whether justine is an adult.

关于这个例子,还有一些额外的内容你应该了解。首先,如果我们不遵守声明的类型,TypeScript 会告诉我们哪里有问题,并防止错误使用。其次,并不是所有内容都必须显式地标注类型 - TypeScript 会为我们推断类型。例如,变量 isJustineAnAdult 的类型是 boolean,即使我们没有显式声明它,而 justine 也会是我们函数的有效参数,尽管我们没有将该变量声明为 User 类型。

🌐 There are additional things about this example that you should know. Firstly, if we do not comply with the declared types, TypeScript will inform us that something is wrong and prevent misuse. Secondly, not everything must be typed explicitly—TypeScript infers types for us. For example, the variable isJustineAnAdult is of type boolean even if we didn't type it explicitly, and justine would be a valid argument for our function even though we didn't declare this variable as of User type.

TypeScript 由什么组成?

🌐 What does TypeScript consist of?

TypeScript 由两个主要部分组成:代码本身和类型定义。

🌐 TypeScript consists of two main components: the code itself and type definitions.

TypeScript 代码

🌐 TypeScript Code

代码部分是常规的 JavaScript,并附加了用于类型注解的 TypeScript 特定语法。当 TypeScript 代码被编译时,所有特定于 TypeScript 的部分都会被移除,从而生成可以在任何环境中运行的干净 JavaScript。例如:

🌐 The code part is regular JavaScript with additional TypeScript-specific syntax for type annotations. When TypeScript code is compiled, all the TypeScript-specific parts are removed, resulting in clean JavaScript that can run in any environment. For example:

function (: string) {
  .(`Hello, ${}!`);
}

类型定义

🌐 Type Definitions

类型定义描述了现有 JavaScript 代码的结构。它们通常存储在 .d.ts 文件中,并且不包含任何实际的实现 - 它们只描述类型。这些定义对于与 JavaScript 的互操作性至关重要:代码通常不会以 TypeScript 的形式分发,而是转译为包含附带类型定义文件的 JavaScript。

🌐 Type definitions describe the shape of existing JavaScript code. They are usually stored in .d.ts files and don't contain any actual implementation—they only describe the types. These definitions are essential for interoperability with JavaScript: code is not usually distributed as TypeScript, but instead transpiled to JavaScript that includes sidecar type definition files.

例如,当你在 Node.js 中使用 TypeScript 时,你需要 Node.js API 的类型定义。这可以通过 @types/node 获取。使用以下命令安装:

🌐 For example, when you use Node.js with TypeScript, you'll need type definitions for Node.js APIs. This is available via @types/node. Install it using:

npm add --save-dev @types/node

这些类型定义允许 TypeScript 理解 Node.js 的 API,并在你使用像 fs.readFilehttp.createServer 这样的函数时提供正确的类型检查和自动补全。例如:

🌐 These type definitions allow TypeScript to understand Node.js APIs and provide proper type checking and autocompletion when you use functions like fs.readFile or http.createServer. For example:

import {  } from 'node:path';

(123, 456);
Argument of type 'number' is not assignable to parameter of type 'string'.

许多流行的 JavaScript 库都在 @types 命名空间下提供类型定义,由 DefinitelyTyped 社区维护。这使得现有的 JavaScript 库能够与 TypeScript 项目无缝集成。

🌐 Many popular JavaScript libraries have their type definitions available under the @types namespace, maintained by the DefinitelyTyped community. This enables seamless integration of existing JavaScript libraries with TypeScript projects.

转换功能

🌐 Transform Capabilities

TypeScript 还包括强大的转换功能,特别是针对 JSX(用于 React 和类似框架)。TypeScript 编译器可以将 JSX 语法转换为普通的 JavaScript,类似于 Babel 的工作方式。虽然我们在这些文章中不会介绍这些转换功能,但值得注意的是,TypeScript 不仅仅是一个类型检查工具 - 它也是一个构建工具,用于将现代 JavaScript 语法转换为适用于不同环境的兼容版本。

🌐 TypeScript also includes powerful transformation capabilities, particularly for JSX (used in React and similar frameworks). The TypeScript compiler can transform JSX syntax into regular JavaScript, similar to how Babel works. While we won't cover these transformation features in these articles, it's worth noting that TypeScript isn't only a tool for type checking—it's also a build tool for transforming modern JavaScript syntax into compatible versions for different environments.

如何运行 TypeScript 代码

🌐 How to run TypeScript code

好的,所以我们有一些 TypeScript 代码。那我们该如何运行它呢? 运行 TypeScript 代码有几种可能的方法,我们将在接下来的文章中介绍所有方法。

🌐 Okay, so we have some TypeScript code. Now how do we run it? There are few possible ways to run TypeScript code, we will cover all of them in the next articles.