Slide 25
Slide 25 text
type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (
k: infer I
) => void
? I
: never;
type LastOf = UnionToIntersection<
T extends any ? () => T : never
> extends () => infer R
? R
: never;
type Push = [...T, V];
type UnionToTuple<
T,
L = LastOf,
N = [T] extends [never] ? true : false
> = true extends N ? [] : Push>, L>;
type Tuple = [TItem, ...TItem[]] & {
length: TLength;
};
/**
* Tuple [string, string, string] Λฦ͢
* ͦΕΛԠ༻ͯ͠ 'a' | 'b' Λ ['a' | 'b', 'a' | 'b'] ʹ͢Δ
*
* 'a' | 'b'
* => ['a' | 'b', 'a' | 'b']
* 'a' | 'b' | 'c'
* => ['a' | 'b' | 'c', 'a' | 'b' | 'c', 'a' | 'b' | 'c']
*/
type UnionToUnionTuple = Tuple["length"]>;
Ṗͷຐ๏
w PQUJPOBMͳϓϩύςΟ
Λͯ͢
আڈͨ͠ͷͰɺ
ΓͷඞਢϓϩύςΟ
Λ
ྻڍ͠λϓϧͰड͚औΓ͍ͨ
w UnionToTupleΛ͑keyof Tͷ
݁ՌΛλϓϧͱͯ͠ѻ͑Δ
w ͔͠͠ॱෆಉͰ͋Δ
ʢఆ͕ٛهड़ॱʹ
ͳΒͳ͍ʣ
͜ͱͰײతͰͳ͍ͨΊ
UnionToUnionTupleͰهड़ॱΛ
ࣗ༝ʹ͢Δ
w ϥϯλΠϜͰnew Set()Λͨ͠ͷ
͜Εରࡦ