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.4k
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
5
9.6k
Podcastを継続する技術 / refactoradio-240119
okunokentaro
1
160
Webアプリケーション設計の第一歩は ディレクトリの整理から / Encraft 1
okunokentaro
34
9.9k
JSONとJSON Schemaを改めて理解する / tokyo_study
okunokentaro
9
2.2k
それでもどうしてRecoilを使うのか / Harajuku.ts Meetup Recoil
okunokentaro
19
5.4k
TypeScriptは10年でこんなに進化しました / TechFeed Experts Night 11
okunokentaro
6
1.7k
Hasura.io RDBをサクサク作る方法はARやO/RMだけじゃなくなりました/hasura-io
okunokentaro
5
620
コードには型アノテーションよりも要件アノテーションを増やせ!/harajukuts2
okunokentaro
14
6.2k
10年と3ヶ月でWebサービスを作った話 / Piyogrammer Conference 2021
okunokentaro
2
1k
Other Decks in Technology
See All in Technology
Taming you application's environments
salaboy
0
190
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
680
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
TypeScript、上達の瞬間
sadnessojisan
46
13k
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
130
The Role of Developer Relations in AI Product Success.
giftojabu1
1
130
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.4k
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
330
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
12k
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
73
9.1k
The World Runs on Bad Software
bkeepers
PRO
65
11k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
The Language of Interfaces
destraynor
154
24k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
How STYLIGHT went responsive
nonsquared
95
5.2k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
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Λۦͯ͠ઈରʹܕ͚͍ͯ͜͠͏