Skip to Content
効果的な TypeScript
book

効果的な TypeScript

by Dan Vanderkam
May 2025
Intermediate to advanced
264 pages
4h 4m
Japanese
O'Reilly Media, Inc.
Content preview from 効果的な TypeScript

第3章 タイプ推論 型推論

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

、産業界で使われているプログラミング言語では、「静的に型付けされた」と「明示的に型付けされた」は、 、伝統的に同義語であった。C、C++、Java......これらはすべて、型を書き出させるものだった。MLやHaskellのような言語には長い間、洗練された型推論システムがあり、過去10年の間に、このシステムは産業用言語にも導入され始めた。C++はauto 、 Javaはvar を追加した。

Typescriptは型推論を多用する。うまく使えば、完全な型安全性を得るために必要な型注釈の数を劇的に減らすことができる。TypeScriptの初心者と経験豊富なユーザを見分ける最も簡単な方法の1つは、型アノテーションの数である。経験豊富な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 ...
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

Learning TypeScript

Learning TypeScript

Josh Goldberg
Fluent React

Fluent React

Tejas Kumar

Publisher Resources

ISBN: 9798341651050