$30 off During Our Annual Pro Sale. View Details »
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
330
Interface vs Types ~型推論が過多推論~
tskaigi 2025 LT Day1で発表したスライドです。
「推論のパフォーマンスで困ったらInterafaceを使いましょう」
Hiroki Omote
May 26, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
宅宅自以為的浪漫:跟 AI 一起為自己辦的研討會寫一個售票系統
eddie
0
500
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
340
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
130
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
10
1.2k
エディターってAIで操作できるんだぜ
kis9a
0
730
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
230
俺流レスポンシブコーディング 2025
tak_dcxi
14
8.8k
chocoZAPサービス予約システムをNuxtで内製化した話
rizap_tech
0
110
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
130
Why Kotlin? 電子カルテを Kotlin で開発する理由 / Why Kotlin? at Henry
agatan
2
7.2k
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
440
React Native New Architecture 移行実践報告
taminif
1
150
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Fireside Chat
paigeccino
41
3.7k
Git: the NoSQL Database
bkeepers
PRO
432
66k
Docker and Python
trallard
47
3.7k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
Mobile First: as difficult as doing things right
swwweet
225
10k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.6k
4 Signs Your Business is Dying
shpigford
186
22k
For a Future-Friendly Web
brad_frost
180
10k
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.