TypeScript の Extract ユーティリティ型
レベル 4261 打鍵
Extract ユーティリティ型は別の型に割り当て可能な型を抽出します。
TypeScript のコード
// nullとundefinedを含む5つの型のユニオン1type Mixed = string | number | boolean | null | undefined;// Extract は string | number | boolean に代入できるメンバーだけを残し、nullとundefinedは除かれる2type Primitives = Extract<Mixed, string | number | boolean>;// 各定数には文字列・数値・真偽値のどれかを入れられる3const str: Primitives = "hello";4const num: Primitives = 42;5const bool: Primitives = true;// typeof で実行時の型を調べ、「string number boolean」と表示する6console.log(typeof str, typeof num, typeof bool);実行結果
string number boolean
このコードでタイピング練習ができます。記録はランキングに反映されません。
このコードで使う記号
この TypeScript のコードで打つ記号と、出てくる回数です。
;× 6|× 6=× 5,× 3:× 3"× 2.× 1(× 1)× 1<× 1