Skip to Content
Effective TypeScript 第2版 ―型システムの力を最大限に引き出す83項目
book

Effective TypeScript 第2版 ―型システムの力を最大限に引き出す83項目

by Dan Vanderkam, 今村 謙士
April 2025
Intermediate to advanced
448 pages
6h 50m
Japanese
O'Reilly Japan, Inc.
Content preview from Effective TypeScript 第2版 ―型システムの力を最大限に引き出す83項目

5章不健全性とany

プログラミング言語における型は伝統的には、完全に静的か完全に動的かの二択でした。オプション的かつ漸進的な型システムを持つTypeScriptは、この境界線を曖昧にしました。TypeScriptでは、プログラムの一部にだけ型を追加し、他の部分には追加しなくても、まったく問題ありません。

この性質は、既存のJavaScriptコードベースを少しずつTypeScriptに移行していくためには不可欠です(「10章 コードのモダン化とTypeScriptへの移行」)。その鍵となるのが、コードの一部の型チェックを効果的に無効化するany型です。これは強力であると同時に悪用されやすいです。any型の賢い使い方を学ぶことは、効果的なTypeScriptを書くためにきわめて重要です。この章では、anyの恩恵を享受しつつ、そのデメリットを抑える方法を説明します。

より一般的な問題として、シンボルの静的な型と実行時の型に互換性がないことを指す不健全性があり、any型はそのもっとも極端な例にすぎません。コードからすべてのany型を取り除いたとしても、健全性の罠にはまる危険性は残ります。「項目48 健全性の罠を回避する」では、そのような罠をいくつか紹介し、それを避ける方法を示します。

項目43 可能なかぎり狭いスコープでany型を使う

次のようなコードがあるとします。

declare function getPizza(): Pizza;
function eatSalad(salad: Salad) { /* ... */ }

function eatDinner() {
  const pizza = getPizza();
  eatSalad(pizza);
  //       ~~~~~
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

LLMのプロンプトエンジニアリング ―GitHub Copilotを生んだ開発者が教える生成AIアプリケーション開発

LLMのプロンプトエンジニアリング ―GitHub Copilotを生んだ開発者が教える生成AIアプリケーション開発

John Berryman, Albert Ziegler, 服部 佑樹, 佐藤 直生
JavaScript 第7版

JavaScript 第7版

David Flanagan, 村上 列
入門 Kubernetes

入門 Kubernetes

Kelsey Hightower, Brendan Burns, Joe Beda, 松浦 隼人

Publisher Resources

ISBN: 9784814401093Publisher Website