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

TypeScript 4.7と型レベルプログラミング

uhyo
May 14, 2022

TypeScript 4.7と型レベルプログラミング

2022年5月14日のTechFeed Conference 2022の発表スライドです。

uhyo

May 14, 2022
Tweet

More Decks by uhyo

Other Decks in Technology

Transcript

  1. 型レベルプログラミングに使われる主な道具 –LFZPGܕ – ΦϒδΣΫτܕ͔ΒจࣈྻϦςϥϧܕΛಘΒΕΔك༗ͳଘࡏ –UZQFPGܕ – ஋ͷੈք͔Βܕͷੈք΁ͷڮ౉͠Λͯ͘͠ΕΔ –NBQQFEUZQFT – ΦϒδΣΫτܕͷ૑଄ʹඞཁෆՄܽ

    –DPOEJUJPOBMUZQFT – ϩδοΫͱ͍͑͹৚݅෼ذɻJOGFSʹΑΔܕͷύλʔϯϚον΋ڧྗ –VOJPOEJTUSJCVUJPO – ϢχΦϯܕΛࣗ༝ࣗࡏʹૢ࡞Ͱ͖Δڧྗͳػߏ –UFNQMBUFMJUFSBMUZQFT – ܕϨϕϧจࣈྻૢ࡞Λ͍ͨ͠ͱ͖ʹ͓ੈ࿩ʹͳΔ
  2. 最近業務で書いた実例 type ResolversWhereTypesAreRemoved = Pick< Required<Resolvers>, { [K in keyof

    Resolvers]-?: NonNullable<Resolvers[K]> extends { __isTypeOf?: unknown } ? never : K; }[keyof Resolvers] >;
  3. extends Constraints on infer Type Variables // Before (〜TS 4.6)

    type FirstString<T> = T extends [infer S, ...unknown[]] ? S extends string ? S : never : never; // After (TS 4.7) type FirstString<T> = T extends [infer S extends string, ...unknown[]] ? S : never; Announcing TypeScript 4.7 RC ͔ΒҾ༻
  4. Instantiation Expressions δΣωϦΫεؔ਺ܕͷಛఆͷܕҾ਺ʹ͓͚ΔฦΓ஋ͷܕ ΛಘΔͷָ͕ʹͳͬͨ const func = <T,>(elm: T) =>

    [elm]; // Before (〜TS 4.6) const _f = () => { return func<number>(0) }; type FuncRetForNumber = ReturnType<typeof _f>; // After(TS 4.7) type FuncRetForNumber = ReturnType<typeof func<number>>;
  5. Optional Variance Annotations for Type Parameters ܕʹແཧ΍ΓมੑΛ෇༩͢Δͷ͕ैདྷΑΓָ͔ͭએݴత ʹͳͬͨ // 諸般の事情でTに対して共変にしたい

    // Before(〜TS 4.6) const _s = Symbol(); type Obj1<T> = { [_s]?: T; } // After(TS 4.7) type Obj2<out T> = {};