Skip to Content
TypeScript 编程
book

TypeScript 编程

by Boris Cherny
May 2025
Intermediate to advanced
324 pages
3h 50m
Chinese
O'Reilly Media, Inc.
Content preview from TypeScript 编程

第 3 章. 关于类型

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在上一章中,我介绍了类型系统的概念,但我从未定义过类型系统中的类型的真正含义。

如果听起来令人困惑,让我举几个熟悉的例子:

  • boolean 类型是所有布尔类型的集合(只有两个:truefalse ),以及可以对它们执行的操作(如||,&&, 和! )。

  • number 类型是所有数字的集合,以及可以对它们执行的操作(如+,-, *,/,%,||,&&, 和? ),包括可以对它们调用的方法,如.toFixed,.toPrecision,.toString, 等等。

  • string 类型是所有字符串的集合,以及可以对它们执行的操作(如+,||, 和&& ),包括可以对它们调用的方法,如 .concat.toUpperCase 等方法。

当你看到某个东西的类型是T 时,你不仅知道它是一个T ,而且还清楚知道你能T 做什么(以及不能什么)。记住,使用类型检查器的目的就是阻止你做无效的事情。 而类型检查程序知道哪些是有效的,哪些是无效的,就是通过查看你正在使用的类型以及你是如何使用它们的。

在本章中,我们将介绍 TypeScript 中可用的类型,并介绍每种类型的基本功能。图 3-1给出了一个概览。

prts 0301
图 3-1. Typescript 的类型层次结构

谈类型

当程序员谈论类型时,他们使用一个精确、通用的词汇来描述其含义。 我们将在本书中通篇使用这个词汇。

假设有一个函数,它接收某个值,然后返回 该值乘以自身的值:

function squareOf(n) {
  return n * n
}
squareOf(2)     // evaluates to 4
squareOf('z')   // evaluates to NaN

显然,这个函数只对数字有效--如果向squareOf 传递的不是数字,结果将是无效的。 因此,我们要做的就是明确注释参数的 类型:

function squareOf(n: number) {
  return n * n
}
squareOf(2)     // evaluates to 4
squareOf('z')   // Error TS2345: Argument of type '"z"' is not assignable to
                // parameter of type 'number'.

现在,如果我们在调用squareOf 时使用的不是数字,Typescript 就会立即抱怨。这是一个微不足道的示例(我们将在下一章详细讨论函数),但它足以介绍一些概念,这些概念是在 TypeScript 中讨论类型的关键。关于最后一个代码示例,我们可以说以下几点:

  1. squareOf的参数n 受限于 number

  2. 2 值的类型可分配给(等同于:number

在没有类型注解的情况下,squareOf 的参数是不受约束的,你可以向它传递任何类型的参数。 一旦我们对它进行了约束,TypeScript 就会为我们工作,验证我们在调用函数时是否每次都使用了兼容的参数。在这个例子中,2 的类型是number ,它可以赋值给squareOf ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

TypeScript 烹饪书

TypeScript 烹饪书

Stefan Baumgartner
JavaScript 经典实例:第三版

JavaScript 经典实例:第三版

Adam D. Scott, Matthew MacDonald, Shelley Powers

Publisher Resources

ISBN: 9798341658080