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
TypeScriptでテストコードを徹底的に型推論する / TypeScript Meetup 4
Search
OKUNOKENTARO
June 16, 2020
Technology
8
3.3k
TypeScriptでテストコードを徹底的に型推論する / TypeScript Meetup 4
2020年6月16日 TypeScript Meetup #4 にて発表した資料です。
OKUNOKENTARO
June 16, 2020
Tweet
Share
More Decks by OKUNOKENTARO
See All by OKUNOKENTARO
トレタO/X アーキテクチャ移行記 Next.js App Router化への道のり / TORETA TECH UPDATE 1
okunokentaro
4
2.2k
Podcastを継続する技術 / refactoradio-240119
okunokentaro
1
150
Webアプリケーション設計の第一歩は ディレクトリの整理から / Encraft 1
okunokentaro
33
9.8k
JSONとJSON Schemaを改めて理解する / tokyo_study
okunokentaro
9
2.1k
それでもどうしてRecoilを使うのか / Harajuku.ts Meetup Recoil
okunokentaro
19
5.3k
TypeScriptは10年でこんなに進化しました / TechFeed Experts Night 11
okunokentaro
6
1.6k
Hasura.io RDBをサクサク作る方法はARやO/RMだけじゃなくなりました/hasura-io
okunokentaro
5
610
コードには型アノテーションよりも要件アノテーションを増やせ!/harajukuts2
okunokentaro
13
6.1k
10年と3ヶ月でWebサービスを作った話 / Piyogrammer Conference 2021
okunokentaro
2
980
Other Decks in Technology
See All in Technology
Privacy Sandbox on Android / DroidKaigi 2024
7pairs
1
270
グイグイ系QAマネージャーの仕事
sadonosake
0
330
The XZ Backdoor Story
fr0gger
0
3.6k
チームビルディングは"感性"で向き合おう / Team Building with Awareness
kohzas
0
250
Mocking in Rust Applications
taiki45
1
410
不動産売買取引におけるAIの可能性とプロダクトでのAI活用
zabio3
0
270
Segment Anything Model 2
tenten0727
3
700
watsonx.ai Dojo 環境準備について
oniak3ibm
PRO
0
300
OSTという文化を組織に根付かせてみた
sansantech
PRO
2
380
20240911_New_Relicダッシュボード活用例
speakerdeckfk
0
110
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
5
46k
言葉は感情の近似値である。その感情と言葉の誤差を最小化しよう ~コミュニケーションにおけるアナログ/デジタル変換の課題に立ち向かう~
nktamago
0
220
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
The Pragmatic Product Professional
lauravandoore
31
6.2k
Bash Introduction
62gerente
608
210k
10 Git Anti Patterns You Should be Aware of
lemiorhan
653
58k
Become a Pro
speakerdeck
PRO
22
4.9k
Building Adaptive Systems
keathley
36
2.1k
Automating Front-end Workflow
addyosmani
1365
200k
Unsuck your backbone
ammeep
667
57k
Ruby is Unlike a Banana
tanoku
96
11k
The Straight Up "How To Draw Better" Workshop
denniskardys
230
130k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
89
16k
Transcript
5ZQF4DSJQUͰ ςε τίʔ υΛపఈతʹܕਪ͢Δ +VO 5ZQF4DSJQU.FFUVQ !PLVOPLFOUBSP
୭ w Ԟݡଠ!PLVOPLFOUBSP w 5ZQF4DSJQUϑϩϯ τΤϯ υ ɾ όοΫΤϯ υ
w "OHVMBSຊϢʔβʔձ w OHKBQBO0O"JSύʔιφϦςΟ
ςε τίʔ υ BOZͰఘΊ͍ͯ·ͤΜ͔
ྫ͑ describe('TasksService', () => { let spy: any; beforeEach(() =>
{ // 略 spy = jest.spyOn(tasksService, 'fetchTasks'); }); test('テスト', () => { // 略 }); });
describe('TasksService', () => { let spy: any; beforeEach(() => {
// 略 spy = jest.spyOn(tasksService, 'fetchTasks'); }); test('テスト', () => { // 略 }); }); ྫ͑
describe('TasksService', () => { let spy: any; beforeEach(() => {
// 略 spy = jest.spyOn(tasksService, 'fetchTasks'); }); test('テスト', () => { // 略 }); }); ྫ͑
ͳͥBOZʹͯ͠͠· ͏ͷ͔ w ܕఆٛΛ͍͍͚ͨͲɺ .d.tsΛಡΜͰҙຯ͕͔Βͳ͔ͬͨ w γϯϓϧͳܕఆٛ͡Όͳ͘ ͯδΣωϦΫεͷࢦఆ͕ඞཁ w ͦ͜ʹԿΛࢦఆͨ͠Β͍͍͔͔Βͣɺ
͍ͭ·ͰίϯύΠϧΤϥʔʹͳΔ w ఘΊͯanyʹ͓ͯ͘͠
KFTUTQZ0Oͷܕఆٛ https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f9f8e5ac0f73721b8ec393767a8caeffe93beeb1/types/jest/index.d.ts#L273-L294
None
ͲΕΛ͍͍͑ͷ ʁ Ͳͬͪ ʁ ʁ ͜ͷ͍ͷԿ ʁ ʁ
ͭ͋Δͷ0WFSMPBET https://www.typescriptlang.org/docs/handbook/functions.html#overloads ্͔ΒॱʹධՁ ͯ͠߹கͨ͠ͷ͕ ΘΕΔ
5ԿΒ͔ͷΦϒδΣΫ τ ٸʹ\^ͬͯॻ͔ΕΔͱ ͼͬ͘ Γ͢Δ͚Ͳɺ ͨͩͷ ۭ0CKFDU-JUFSBM VOEFpOFE OVMMҎ֎ͷ ͍ͣΕ\^Λຬͨ͢
.͕ॏཁ
ͱ Γ͋͑ͣԿΒ͔ͷจࣈྻͩ https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f9f8e5ac0f73721b8ec393767a8caeffe93beeb1/types/jest/index.d.ts#L319-L324
.BQQFE5ZQFT $POEJUJPOBM5ZQFT https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f9f8e5ac0f73721b8ec393767a8caeffe93beeb1/types/jest/index.d.ts#L319-L324 ࠓͲ͖ͷͷڽΒ͞Εͨ5ZQF4DSJQUܕఆٛ άάΓʹ͍͘ߏจ͕ͨ͘ ͞Μ ͍Ζ͍Ζग़͖ͯͯϫέΘ͔ΒΜ͔͠Εͳ͍͚Ͳ ͻͱͭͣͭղܾ͍ͯ͜͠͏ .BQQFE5ZQFT $POEJUJPOBM5ZQFT
-PPLVQ5ZQFT ͜Ε"SSBZBOZͷTVHBS
.BQQFE5ZQFT https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f9f8e5ac0f73721b8ec393767a8caeffe93beeb1/types/jest/index.d.ts#L319-L324 5͕࣋ͭશͯͷϓϩύςΟ Λʜ Α Γӈʹهड़͞Εͨܕʹ͢Δ
$POEJUJPOBM5ZQFT https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f9f8e5ac0f73721b8ec393767a8caeffe93beeb1/types/jest/index.d.ts#L319-L324 ͦͷܕ͕ࢦఆͷ݅Λʜ ຬͨ͢ͳΒ ຬͨ͞ͳ͍ͳΒҎ߱ ͷܕͱͳΔ
͏ҰಡΜͰΈΔͱ ୈҾ͕ʜ ΦϒδΣΫ τ5ͷ࣋ͭ ϓϩύςΟ໊ͳΒ ୈҾ͕HFU͔TFU͔ʹԠͯ͡ৼΓ͚ ΦϒδΣΫ τ5ͷ࣋ͭ ϝ ιο
υ໊ͳΒͭΊͷ0WFSMPBETΛద༻ ͭ· Γωε τͨ͠JGจͷΑ ͏ʹಡΊΔ
Ͳͷ0WFSMPBET͔͔ΕͦͷΓܕΛݟΔ ୈҾʹΠϯελϯεɺ ୈҾʹͦͷΠϯελϯε͕࣋ͭϝ ιο υ໊ ΛͤSpyInstance<T, Y>͕ฦΔ ͰReturnType ArgsTypeͱ
ʁ ʁ
Ϗϧ τΠϯͷ$POEJUJPOBM5ZQFT w lib.d.tsʹ5ZQF4DSJQUʹඪ४ͰΈࠐ·Ε͍ͯΔ $POEJUJPOBM5ZQFT͕ଘࡏ͢Δ w ReturnType<T>ͦͷͻͱͭ wʮstringΛฦؔ͢ʯ ΛTʹͤɺ ReturnType<T>stringͱͳΔ
ࣗ࡞ͷ$POEJUJPOBM5ZQFT w +FTUͷܕఆٛͰArgsType<T>ͱ͍͏ܕ͕ಠࣗʹఆٛ͞Ε͍ͯΔ w w ͠T͕extendsҎ߱ͷܕͱϚονͨ͠Βɺ ͦͷ࣌ͷҾܕAΛฦ͢ͱ͍͏ҙ ʢϚον͠ͳ͚ΕneverΛฦ͢ʣ
w inferΛͬͯ݅ʹϚονͨ͠ͱ͖ͷ֘ՕॴΛɺ ΓܕଆͰѻ͑Δ type ArgsType<T> = T extends (...args: infer A) => any ? A : never;
"OHVMBSͷ5FTU#FEʹ ຊؾͰܕΛ͚ͯΈΔ
ઈରʹܕ͚ΛఘΊͳ͍ w "OHVMBS%*ίϯςφΛඋ͑Δ w ςε τ࣮ࢪ࣌ʹಛఆͷαʔϏεΛϞοΫʹࠩ͠ସ࣮͑ͯߦͰ͖Δ w ΦϑΟ γϟϧͷܕఆ͍͍ٛͩͨanyͱॻ͔ΕͯΔ w
ώϡʔϚϯΤϥʔ͕ى͖͍͢ w anyͷ··ͰఘΊͳ͍
%FQFOEFODJFT5ܕ import { Type } from '@angular/core'; type MapToConstructorType<T extends
any[]> = { [P in keyof T]: Type<T[P]> }; export type Dependencies< T extends new (...args: any) => any > = MapToConstructorType<ConstructorParameters<T>>; w Router, TranslateService, UsersServiceΛ%*͢Δ MyComponent͕͋ͬͨͱ͢Δ w Dependencies<typeof MyComponent> [typeof Router, typeof TranslateService, typeof UsersService]ͱͷޓܕฦ͢
·ͱΊ w ࠷ۙͷϞμϯͳܕఆٛϑΝΠϧɺ ͻͨ͢Β܁Γฦ͠ͱ݅ذ w .BQQFE5ZQFT $POEJUJPOBM5ZQFTΛۦͯ͠ઈରʹܕ͚͍ͯ͜͠͏