Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TypeScriptでテストコードを徹底的に型推論する / TypeScript Meetup 4

TypeScriptでテストコードを徹底的に型推論する / TypeScript Meetup 4

2020年6月16日 TypeScript Meetup #4 にて発表した資料です。

OKUNOKENTARO

June 16, 2020
Tweet

More Decks by OKUNOKENTARO

Other Decks in Technology

Transcript

  1. ୭ w Ԟ໺ݡଠ࿠!PLVOPLFOUBSP w 5ZQF4DSJQUϑϩϯ τΤϯ υ ɾ όοΫΤϯ υ

    w "OHVMBS೔ຊϢʔβʔձ w OHKBQBO0O"JSύʔιφϦςΟ
  2. ྫ͑͹ describe('TasksService', () => { let spy: any; beforeEach(() =>

    { // 略 spy = jest.spyOn(tasksService, 'fetchTasks'); }); test('テスト', () => { // 略 }); });
  3. describe('TasksService', () => { let spy: any; beforeEach(() => {

    // 略 spy = jest.spyOn(tasksService, 'fetchTasks'); }); test('テスト', () => { // 略 }); }); ྫ͑͹
  4. describe('TasksService', () => { let spy: any; beforeEach(() => {

    // 略 spy = jest.spyOn(tasksService, 'fetchTasks'); }); test('テスト', () => { // 略 }); }); ྫ͑͹
  5. ࣗ࡞ͷ$POEJUJPOBM5ZQFT w +FTUͷܕఆٛͰ͸ArgsType<T>ͱ͍͏ܕ͕ಠࣗʹఆٛ͞Ε͍ͯΔ w  w ΋͠T͕extendsҎ߱ͷܕͱϚονͨ͠Βɺ ͦͷ࣌ͷҾ਺ܕAΛฦ͢ͱ͍͏ҙ ʢϚον͠ͳ͚Ε͹neverΛฦ͢ʣ 

    w inferΛ࢖ͬͯ৚݅ʹϚονͨ͠ͱ͖ͷ֘౰ՕॴΛɺ ໭ΓܕଆͰѻ͑Δ type ArgsType<T> = T extends (...args: infer A) => any ? A : never;
  6. %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]ͱͷޓ׵ܕฦ͢