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. 2022-05-14 uhyo
    5ZQF4DSJQUͱ
    ܕϨϕϧϓϩάϥϛϯά

    View full-size slide

  2. About Speaker
    VIZP
    –-*/&ͷϑϩϯτΤϯυΤϯδχΞ
    –5ZQF4DSJQUͱ3FBDU͕ઐ໳
    5ZQF4DSJQUͷೖ໳ॻൃചதʂ

    View full-size slide

  3. 型レベルプログラミングとは?
    ܕͰΑΓਖ਼֬ͳϩδοΫΛهड़͠ɺ
    ίʔυͷ҆શੑΛߴΊΔͨΊʹ
    طଘͷܕ͔Β৽ͨͳܕΛ૑ग़͢Δख๏ͷ૯শ
    ʢVIZPͷݟղͰ͢ʣ

    View full-size slide

  4. 型レベルプログラミングに使われる主な道具
    –LFZPGܕ
    – ΦϒδΣΫτܕ͔ΒจࣈྻϦςϥϧܕΛಘΒΕΔك༗ͳଘࡏ
    –UZQFPGܕ
    – ஋ͷੈք͔Βܕͷੈք΁ͷڮ౉͠Λͯ͘͠ΕΔ
    –NBQQFEUZQFT
    – ΦϒδΣΫτܕͷ૑଄ʹඞཁෆՄܽ
    –DPOEJUJPOBMUZQFT
    – ϩδοΫͱ͍͑͹৚݅෼ذɻJOGFSʹΑΔܕͷύλʔϯϚον΋ڧྗ
    –VOJPOEJTUSJCVUJPO
    – ϢχΦϯܕΛࣗ༝ࣗࡏʹૢ࡞Ͱ͖Δڧྗͳػߏ
    –UFNQMBUFMJUFSBMUZQFT
    – ܕϨϕϧจࣈྻૢ࡞Λ͍ͨ͠ͱ͖ʹ͓ੈ࿩ʹͳΔ

    View full-size slide

  5. 最近業務で書いた実例
    type ResolversWhereTypesAreRemoved = Pick<
    Required,
    {
    [K in keyof Resolvers]-?:
    NonNullable extends
    { __isTypeOf?: unknown }
    ? never
    : K;
    }[keyof Resolvers]
    >;

    View full-size slide

  6. TypeScript 4.7の新機能たち
    ৭ʑ৽ػೳ͕͋Δ͕ɺܕϨϕϧϓϩάϥϛϯάʹ໾ཱͭػೳ
    ΋௥Ճ͞Εͨ
    –FYUFOET $POTUSBJOUTPO JOGFS 5ZQF7BSJBCMFT
    –*OTUBOUJBUJPO&YQSFTTJPOT
    –0QUJPOBM7BSJBODF"OOPUBUJPOTGPS5ZQF1BSBNFUFST

    View full-size slide

  7. extends Constraints on infer Type Variables
    // Before (〜TS 4.6)
    type FirstString =
    T extends [infer S, ...unknown[]]
    ? S extends string
    ? S
    : never
    : never;
    // After (TS 4.7)
    type FirstString =
    T extends [infer S extends string, ...unknown[]]
    ? S
    : never;
    Announcing TypeScript 4.7 RC ͔ΒҾ༻

    View full-size slide

  8. Instantiation Expressions
    δΣωϦΫεؔ਺ܕͷಛఆͷܕҾ਺ʹ͓͚ΔฦΓ஋ͷܕ
    ΛಘΔͷָ͕ʹͳͬͨ
    const func = (elm: T) => [elm];
    // Before (〜TS 4.6)
    const _f = () => { return func(0) };
    type FuncRetForNumber = ReturnType;
    // After(TS 4.7)
    type FuncRetForNumber = ReturnType>;

    View full-size slide

  9. Optional Variance Annotations for Type
    Parameters
    ܕʹແཧ΍ΓมੑΛ෇༩͢Δͷ͕ैདྷΑΓָ͔ͭએݴత
    ʹͳͬͨ
    // 諸般の事情でTに対して共変にしたい
    // Before(〜TS 4.6)
    const _s = Symbol();
    type Obj1 = {
    [_s]?: T;
    }
    // After(TS 4.7)
    type Obj2 = {};

    View full-size slide

  10. まとめ
    օ͞Μ΋ָ͍͠ܕϨϕϧϓϩάϥϛϯάϥΠϑΛʂ

    View full-size slide