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
Interface vs Types ~型推論が過多推論~
Search
Hiroki Omote
May 26, 2025
Programming
1
290
Interface vs Types ~型推論が過多推論~
tskaigi 2025 LT Day1で発表したスライドです。
「推論のパフォーマンスで困ったらInterafaceを使いましょう」
Hiroki Omote
May 26, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
AIエージェント開発、DevOps and LLMOps
ymd65536
1
360
オープンセミナー2025@広島「君はどこで動かすか?」アンケート結果
satoshi256kbyte
0
220
Claude Codeで挑むOSSコントリビュート
eycjur
0
180
AI OCR API on Lambdaを Datadogで可視化してみた
nealle
0
210
RDoc meets YARD
okuramasafumi
3
150
STUNMESH-go: Wireguard NAT穿隧工具的源起與介紹
tjjh89017
0
390
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
200
開発チーム・開発組織の設計改善スキルの向上
masuda220
PRO
17
9.1k
Vue・React マルチプロダクト開発を支える Vite
andpad
0
100
デザインシステムが必須の時代に
yosuke_furukawa
PRO
2
130
🔨 小さなビルドシステムを作る
momeemt
2
610
Zendeskのチケットを Amazon Bedrockで 解析した
ryokosuge
2
130
Featured
See All Featured
Speed Design
sergeychernyshev
32
1.1k
The World Runs on Bad Software
bkeepers
PRO
70
11k
How to Ace a Technical Interview
jacobian
279
23k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Optimizing for Happiness
mojombo
379
70k
What's in a price? How to price your products and services
michaelherold
246
12k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
It's Worth the Effort
3n
187
28k
Writing Fast Ruby
sferik
628
62k
Facilitating Awesome Meetings
lara
55
6.5k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Transcript
None
はじめに どちらかの派閥を決めたい方 vsとかついてるけれど、決着をつけたいわけではないです。 きのこvsたけのこ戦争ほど、不毛な戦いはないと思います。 ちなみに、私はたけのこ派です。 笑いに厳しい方 タイトルからスベってるって思った人はお帰りください。 傷つくので。
omote 表 洋樹 マラソ ガンダム 趣味 Design Engineer
Manager Tech Lead Freelancer 2012 2019 2022 Scheeme BUBO
ポリモーフィック
as=”button” Prop 社内のデザインシステム配信するため、 Buttonコンポーネントを作っていた時のお話。
実際の実装
そして結果
ん?
見間違いかな?
もう一回確認しよう
何度見返しても結果は同じ(それはそう)
いやいやいや
焦りは最高潮に達する ・開発時は推論できたのに裏切られた恐怖 ・そもそもデザインシステムで配信している、anyとか曖昧なのヤバい ・慣れないライブラリを使いつつ、型を組み合わせた努力が…!?
エニーは型を知らない キラキラ
なぜ、anyになったのか 複雑な交差型 特定のケースで最終的に any 型に落ち る。 TypeScriptの型システムの計算の限界 と推論の妥協に起因。 Mantineの型 ×
自前の型 これにより計算量が爆増した。 ライブラリ内にあった型を流用し、交 差型として使用した。 落とし穴 開発時は推論できていた。 ビルド時に内部的に複雑化し、最終結 果がanyになった。 エニーは型を知らない キラキラ
計算量が増えるケース 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 type type extends infer const const typeof DeepArray DeepArray []; Flatten ( )[] Flatten ; deeplyNested DeepArray DeepArray DeepArray [ [[ ], [ ]], [[[ ]]] ]; flattened Flatten deeplyNested ; < > = | < > < > = ? < > : : < < <number>>> = : < > = T T T T T U U T // 再帰的にすべてのネストを展開する型 // 型推論が限界を超えると、ここで `any` として扱われる可能性あり 1 2 3 1 Recursive Type 再帰的にネストが展開され、無限に近 い計算が発生します。 TypeScriptはこのようなケースで型の 推論を放棄し、any にフォールバック することがあります。 ※ver3.7でRecursive typeの扱いは改善 エニーは型を知らない キラキラ
型推論が過多推論やん----------------------
推論のリベリオン
宣言をinterfaceに変えただけです。 型自体は何も編集していません。 Interfaceを使え、とAIが言っている type → interface 1 2 3 4
5 type keyof Props Omit ComponentPropsWithoutRef , ButtonProps Omit ButtonProps, { variant ; color ; }; = < < > > & < > & ?: | | ?: | | 'button' 'unstyled' 'filled' 'outlined' 'text' 'default' 'subdued' 'caution' 1 2 3 4 interface extends keyof Props Omit ComponentPropsWithoutRef , ButtonProps , Omit ButtonProps, { variant ; color ; } < < > > < > ?: | | ?: | | 'button' 'unstyled' 'filled' 'outlined' 'text' 'default' 'subdued' 'caution'
なぜこれで解決できるか typeにおける交差型は型演算の対象 型レベルで演算されて解決される必要がある。 型の深さや複雑さによって any に落ちる可能性が高くなる。 interface はマージベースで解決される interface はマージされるため、TypeScriptの型解決の負荷が
下がる。 interface 同士は「拡張」に近い 「構造の統合」ではなく、型計算を遅延させた構文にる。 TypeScriptの型システムとしては「負担が少ない」=any に落 ちにくくなるという特性がある。
type interaface 条件型 ・使用可 不可 拡張(継承) H & を使って交差型として合8 H
同名のtypeは許可しない H extends で継承可能(複数継承も可y H 同名の interface を自動で統合 パフォーマンス(型計算) H 計算量によっては型爆発の可能性 H 遅延評価するので、パフォーマンス高 typeとInterfaceの比較表
遅延評価、なるほど!と思ってたが
いや、書いてあったわ ライブラリのドキュメントはinterface使用してい るし、素直に従えばよかっただけ! “Polymorphic components have performance overhead for tsserver
(no impact on runtime performance)”
まとめ 計算量次第ではinterface使う interfaceは遅延評価 typeとinterface どちらかに寄せるなど、流派とかもあるかもしれないが、対立 構造ではない。 使い分けするシーンはしっかりある。 ドキュメント読もう それはそう それはそう
それはそう
Thank You When the going gets tough, the tough get
going.