Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
inferと仲良くなる10分間
Search
ryokatsuse
May 22, 2026
Programming
560
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
Web Haptics APIを語りたい
ryokatsuse
0
240
ARIA Notifyについて
ryokatsuse
1
250
アクセシビリティの自動テストはどのように行われているのか? axe-coreの処理を巡る旅
ryokatsuse
0
770
友達ではなく仲間とはなにか? 〜『映像研には手を出すな!』から学ぶ仕事の取り組み方〜
ryokatsuse
0
900
shadcn/uiで考えるコンポーネント設計
ryokatsuse
7
2.4k
ゆめみのアクセシビリティの現在地と今後
ryokatsuse
4
2.7k
Other Decks in Programming
See All in Programming
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
250
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
4
12k
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
170
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
390
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.2k
スマート反転とウェブアクセシビリティ
camiha
0
210
FreeBSDでZabbixを動かす
kenkino
0
320
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
3.2k
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
260
JRuby: Past, Present, and Future
headius
0
190
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
100
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
2.4k
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
240
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
2k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
510
Speed Design
sergeychernyshev
33
2.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
700
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
540
Making Projects Easy
brettharned
120
6.8k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
270
Six Lessons from altMBA
skipperchong
29
4.5k
GitHub's CSS Performance
jonrohan
1033
470k
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