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
320
Interface vs Types ~型推論が過多推論~
tskaigi 2025 LT Day1で発表したスライドです。
「推論のパフォーマンスで困ったらInterafaceを使いましょう」
Hiroki Omote
May 26, 2025
Tweet
Share
Other Decks in Programming
See All in Programming
Eloquentを使ってどこまでコードの治安を保てるのか?を新人が考察してみた
itokoh0405
0
3.2k
connect-python: convenient protobuf RPC for Python
anuraaga
0
110
社内オペレーション改善のためのTypeScript / TSKaigi Hokuriku 2025
dachi023
1
120
Flutterアプリ運用の現場で役立った監視Tips 5選
ostk0069
1
500
Phronetic Team with AI - Agile Japan 2025 closing
hiranabe
2
670
AIを駆使して新しい技術を効率的に理解する方法
nogu66
1
660
OSS開発者の憂鬱
yusukebe
12
5.7k
Promise.tryで実現する新しいエラーハンドリング New error handling with Promise try
bicstone
3
1k
競馬で学ぶ機械学習の基本と実践 / Machine Learning with Horse Racing
shoheimitani
14
13k
「正規表現をつくる」をつくる / make "make regex"
makenowjust
1
720
Tangible Code
chobishiba
3
700
Atomics APIを知る / Understanding Atomics API
ssssota
1
180
Featured
See All Featured
Visualization
eitanlees
150
16k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
680
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.8k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
It's Worth the Effort
3n
187
29k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.3k
Making the Leap to Tech Lead
cromwellryan
135
9.6k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Mobile First: as difficult as doing things right
swwweet
225
10k
Statistics for Hackers
jakevdp
799
230k
Automating Front-end Workflow
addyosmani
1371
200k
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.