Skip to Content
有效的 TypeScript
book

有效的 TypeScript

by Dan Vanderkam
July 2025
Intermediate to advanced
264 pages
3h 32m
Chinese
O'Reilly Media, Inc.
Content preview from 有效的 TypeScript

第 3 章 类型推断

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

对于 行业中使用的编程语言,"静态类型 "和 "显式类型 "在 传统上是同义词。C、C++、Java:它们都让你写出类型。但学术语言从未将这两件事混为一谈:像 ML 和 Haskell 这样的语言早就有了复杂的类型推断系统,而在过去十年中,这种系统也开始进入工业语言。C++ 增加了auto ,Java 增加了 var

TypeScript 广泛使用了类型推断。如果使用得当,这可以大大减少代码所需的类型注解数量,从而获得完全的类型安全。区分 TypeScript 初学者和经验丰富的用户最简单的方法之一就是类型注解的数量。经验丰富的 Typescript 开发者会使用相对较少的注解(但使用它们会产生很好的效果),而初学者可能会将代码淹没在冗余的类型注解中。

本章将向您展示类型推断可能出现的一些问题,以及如何解决这些问题。阅读完本章后,您应该能够很好地理解 TypeScript 是如何推断类型的,什么时候仍然需要编写类型声明,以及即使可以推断出类型,什么时候编写类型声明也是一个好主意。

第 19 项:避免使用可推断类型使代码杂乱无章

许多新的 TypeScript 开发人员在从 JavaScript 转换代码库时做的第一件事就是用类型注解填充代码。毕竟,TypeScript 是关于类型的!但在 TypeScript 中,许多注解都是不必要的。为所有变量声明类型会适得其反,而且被认为是糟糕的风格。

不要写

let x: number = 12;

相反,只管写:

let x = 12;

如果你在编辑器中将鼠标移到x 上,你会看到它的类型已被推断为number (如图 3-1 所示)。

efts 03in01
图 3-1. 文本编辑器显示 x 的推断类型是数字。

显式类型注释是多余的。写出来只会增加噪音。如果不确定类型,可以在编辑器中查看。

TypeScript 也会推断出更复杂对象的类型。您可以不写

const person: {
  name: string;
  born: {
    where: string;
    when: string;
  };
  died: {
    where: string;
    when: string;
  }
} = {
  name: 'Sojourner Truth',
  born: {
    where: 'Swartekill, NY',
    when: 'c.1797',
  },
  died: {
    where: 'Battle Creek, MI',
    when: 'Nov. 26, 1883'
  }
};

可以直接写

const person = {
  name: 'Sojourner Truth',
  born: {
    where: 'Swartekill, NY',
    when: 'c.1797',
  },
  died: {
    where: 'Battle Creek, MI',
    when: 'Nov. 26, 1883'
  }
};

同样,类型是完全相同的。在值之外再写类型只会增加噪音。(第 21 项将详细介绍对象字面类型的推断)。

对象的情况同样适用于数组。根据输入和操作,Typescript 可以很容易地推断出这个函数的返回类型:

function ...
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

Java 9 Modularity

Java 9 Modularity

Sander Mak, Paul Bakker

Publisher Resources

ISBN: 9798341663190