Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
inferと仲良くなる10分間
Search
ryokatsuse
May 22, 2026
Programming
500
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
inferと仲良くなる10分間
ryokatsuse
May 22, 2026
More Decks by ryokatsuse
See All by ryokatsuse
ARIA Notifyについて
ryokatsuse
1
230
アクセシビリティの自動テストはどのように行われているのか? axe-coreの処理を巡る旅
ryokatsuse
0
750
友達ではなく仲間とはなにか? 〜『映像研には手を出すな!』から学ぶ仕事の取り組み方〜
ryokatsuse
0
860
shadcn/uiで考えるコンポーネント設計
ryokatsuse
7
2.3k
ゆめみのアクセシビリティの現在地と今後
ryokatsuse
4
2.7k
Other Decks in Programming
See All in Programming
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
490
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
240
AIを紡ぐPMのお話
swdtkuy
0
100
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
510
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
180
170k Jobs a Day on GKE: Scaling Mercari's CI Platform - and What's Next for AI-Native Development
junyaokabe
0
110
Google Apps Script で Ruby を動かす
kawahara
0
230
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
180
Flow は今どうなっているか
mizdra
PRO
0
540
FDEが実現するAI駆動経営の現在地
gonta
2
280
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.7k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.3k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
390
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.3k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
430
Tell your own story through comics
letsgokoyo
1
1k
Claude Code のすすめ
schroneko
67
230k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
390
Technical Leadership for Architectural Decision Making
baasie
3
480
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
530
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
760
Transcript
inferと仲良くなる10分間 TSKaigi 2026 2026/05/24
自己紹介 Infixer(インフィクサー) 株式会社タイミー フロントエンドエンジニア HTMLとCSSが大好きなマークアップおじさんです。 X(Twitter) GitHub Blog Bluesky Cosense
こんなお話をします inferキーワードの読み方、考え方、使い方を理解する Utility Types Conditional Types infer 束縛変数 パターンマッチング
Utility Types
Utility Types 型変換を容易にするためのユーティリティ型としてTypeScriptにはUtility Typesが 用意されています。 Utility Typesは、グローバルに利用することが可能
ReturnTypeについて 型パラメータ`T`に与えた関数の戻り値の型を得ることができるもの 1 2 3 4 5 6 7 8
9 // 型に対して使う場合 type Fn = () => string; type R1 = ReturnType<Fn>; // string // 値(関数)に対して使う場合は typeof が必要 function getUser() { return { name: "infixer", age: 37 }; } type R2 = ReturnType<typeof getUser>; // { name: string; age: number }
ReturnTypeの型をみる `T extends (...args: any) => any> = T extends
(...args: any)` って何? 🤔 `infer R ? R`って何?🤔
Conditional Types
Conditional Types 型の条件分岐のこと。JavaScriptの三項演算子のように使う。 入出力の型の関係を記述するのに便利 1 2 3 4 type IsString<T>
= T extends string ? true : false; type A = IsString<"hello">; // true type B = IsString<123>; // false `T extends U ? X : Y` 「T が U を満たすなら X、そうでなければ Y」
もう一度ReturnTypeの型をみる 1 2 3 4 type ReturnType<T extends (...args: any)
=> any> = T extends (...args: any) => infer R ? R : any; Conditional Typesで条件を記述していることがわかる inferって何?
infer
inferとは? Conditional Typesで使用できる一時的な型変数の宣言 変数の型を推論(infer)して型変数の中身を決定するも の inferの宣言は、extendsの右辺でしか使えない 宣言した型変数は、true分岐の中ならどこでも使える
inferの読み方 1 2 3 4 5 type Example<T> = T
extends { data: infer U } ? U : never; type T1 = Example<{ data: string }>; // string type T2 = Example<{ data: User[] }>; // User[] type T3 = Example<{ message: string }>; // never もしT が { data: ... } の形をしているなら、 その data の中身の型に U と名前をつけて取り出す(束縛する) → そして U を返す そうでなければ → never を返す
再度ReturnTypeの型をみる 1 2 3 4 type ReturnType<T extends (...args: any)
=> any> = T extends (...args: any) => infer R ? R : any; もしT型が (...args: any) => any を形をしているなら、 その関数の戻り型として割り当てられている型を`R`と名前をつけて型パラメー タとして取り出す(束縛する) → そしてR型を返す そうでなければ → any型を返す
inferは型の世界に束縛変数を導入したもの 束縛変数 名前と値 (型) が結びついた変数のこと `infer U` Uに結びつく型が入力に応じて動的に決まる スコープ true分岐の中のみ有効である
Conditional Types + inferは型のパターンマッチング Conditional Typesで型レベルの条件分岐ができる 特定のパターンに合致した中身の型に名前をつけて取 り出せる つまりパターンマッチング パターンマッチング(英:
Pattern matching、パターン照合)とは、データを検索する場合 に特定のパターンが出現するかどうか、またどこに出現するかを特定する手法のことであ る。 wikipedia:パターンマッチング
Conditional Typesで型レベルの条件分岐ができる inferを使うことで中身の型に名前をつけて取り出せる つまりパターンマッチっぽい
inferの使用例
弊社の例)配列の中身の文字列が重複しているかチェックする 1 2 3 4 5 6 7 8 9
// やりたくない:npmパッケージをそのまま読み込むと、約3000個分のアイコン分ダウンロードすることになる import 'material-symbols'; // 型だけimport import type { MaterialSymbol } from 'material-symbols'; const _availableMaterialSymbols = [ 'call', 'check', 'arrow_back', ] as const satisfies MaterialSymbol[]; プロダクト全体で使うアイコンを、 Material Symbolsで管理している。 直接npmから使うと、本来使うことのないアイコンもすべて含まれるのでパ フォーマンスが悪い。 Material Symbolsの型だけを使い、実際に使うアイコン名だけを格納した配列 で管理。 同じアイコン名を記述できないように文字列の重複チェックをしたい
文字列の重複チェックにinferを使う 1 2 3 4 5 6 7 8 9
10 type ArrayToUnion<T extends readonly unknown[]> = T[number]; type IsUnique<T extends readonly unknown[]> = T extends readonly [infer First, ...infer Rest] ? First extends ArrayToUnion<Rest> ? `"${First & string}"` : IsUnique<Rest> : never; type ValidateUniqueArray<T extends readonly AllMaterialSymbols[]> = IsUnique<T> extends never ? T : IsUnique<T>; `infer`を使って配列の先頭とそれ以外に分割する 先頭の要素が分割した配列内の要素にあるか調べる 要素があれば重複しているので文字列リテラル型で返す 残りの要素を再帰的に処理する
実際の型エラー
まとめ
まとめ inferは型の世界に束縛変数を導入した Conditional Types + inferはパターンマッチしてい るんだなと捉える 自分で書く機会は少なくても、読めるようになる と見え方が変わる
https://github.com/microsoft/TypeScript/blob/4076ff8fd6c91c07f6baefa0843e22e33168e164/tests/cases/conformance/types/conditional/inferTypes1.ts
ご清聴ありがとうございました
参照 Utility Types: https://www.typescriptlang.org/docs/handbook/utility-types.html Conditional Types: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html inferと実例 UnArray<T> /
TypeScript一人カレンダー: https://zenn.dev/okunokentaro/ articles/01gm6sbe97g0jnzj2ed24va0vv Type inference in conditional types: https://github.com/microsoft/TypeScript/pull/21496 Announcing TypeScript 4.1: https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/ オーバーロードされた関数型から引数の型や返り値の型を取り出す方法: https://zenn.dev/uhyo/articles/ typescript-overload-infer パターンマッチング: https://ja.wikipedia.org/wiki/ %E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%83%9E%E3%83%83%E3%83%81%E3%83%B3% E3%82%B0