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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
ryokatsuse
May 22, 2026
Programming
460
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
220
アクセシビリティの自動テストはどのように行われているのか? axe-coreの処理を巡る旅
ryokatsuse
0
720
友達ではなく仲間とはなにか? 〜『映像研には手を出すな!』から学ぶ仕事の取り組み方〜
ryokatsuse
0
840
shadcn/uiで考えるコンポーネント設計
ryokatsuse
7
2.3k
ゆめみのアクセシビリティの現在地と今後
ryokatsuse
4
2.7k
Other Decks in Programming
See All in Programming
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
260
初めてのKubernetes 本番運用でハマった話
oku053
0
130
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
430
どこまでゆるくて許されるのか
tk3fftk
0
490
霧の中の代数的エフェクト
funnyycat
1
390
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
2.6k
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
660
関数型プログラミングのメリットって何だろう?
wanko_it
0
180
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
220
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.9k
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.1k
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
190
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
1.1k
Building Applications with DynamoDB
mza
96
7.1k
Building Adaptive Systems
keathley
44
3.1k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
390
Facilitating Awesome Meetings
lara
57
7k
The agentic SEO stack - context over prompts
schlessera
0
850
Evolving SEO for Evolving Search Engines
ryanjones
0
240
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
350
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
150
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