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
3
340
Interface vs Types ~型推論が過多推論~
tskaigi 2025 LT Day1で発表したスライドです。
「推論のパフォーマンスで困ったらInterafaceを使いましょう」
Hiroki Omote
May 26, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
Implementation Patterns
denyspoltorak
0
140
ゲームの物理 剛体編
fadis
0
390
TestingOsaka6_Ozono
o3
0
230
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
8
3.5k
AI時代を生き抜く 新卒エンジニアの生きる道
coconala_engineer
1
480
GoLab2025 Recap
kuro_kurorrr
0
790
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
3.2k
Findy AI+の開発、運用におけるMCP活用事例
starfish719
0
1.9k
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
440
SwiftUIで本格音ゲー実装してみた
hypebeans
0
550
Navigating Dependency Injection with Metro
l2hyunwoo
1
200
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
230
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Git: the NoSQL Database
bkeepers
PRO
432
66k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
200
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
Building Flexible Design Systems
yeseniaperezcruz
330
39k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
53
Six Lessons from altMBA
skipperchong
29
4.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
58
41k
Statistics for Hackers
jakevdp
799
230k
Faster Mobile Websites
deanohume
310
31k
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.