TypeScript Type Challenges Medium_配列操作系
Tuple to Union
https://github.com/type-challenges/type-challenges/blob/main/questions/00010-medium-tuple-to-union/README.md
code:typescript
type TupleToUnion<T extends any[]> = Tnumber
Answer
Last of Array
https://github.com/type-challenges/type-challenges/blob/main/questions/00015-medium-last/README.md
code:typescript
type Last<T extends any[]> = T extends ...infer R, infer V ? V : never
// 解2
type Last2<T extends any[]> = any, ...T[T'length']
Answer
Pop
https://github.com/type-challenges/type-challenges/blob/main/questions/00016-medium-pop/README.md
code:typescript
type Pop<T extends any[]> = T extends ...infer R, infer V ? R : never
Answer
Shift
https://github.com/type-challenges/type-challenges/blob/main/questions/03062-medium-shift/README.md
Popとほぼ同じ
Reverse
https://github.com/type-challenges/type-challenges/blob/main/questions/03192-medium-reverse/README.md
Chunk
https://github.com/type-challenges/type-challenges/blob/main/questions/04499-medium-chunk/README.md
Fill
https://github.com/type-challenges/type-challenges/blob/main/questions/04518-medium-fill/README.md
wintyo.icon 範囲指定が難しい
Without
https://github.com/type-challenges/type-challenges/blob/main/questions/05117-medium-without/README.md
omitのタプル版
Flatten
https://github.com/type-challenges/type-challenges/blob/main/questions/00459-medium-flatten/README.md
ネストされた配列を全部無くす
wintyo.icon 再帰で多分なんとかなる
Flatten Depth
https://github.com/type-challenges/type-challenges/blob/main/questions/03243-medium-flattendepth/README.md
flattenに階層を指定する
/icons/hr.icon
タプルから特定のフィールドをpickする
code:typescript
type TabItem = {
value: string;
label: string;
}
const TAB_ITEMS = [
{ value: 'tab1', label: 'タブ1' },
{ value: 'tab2', label: 'タブ2' },
{ value: 'tab3', label: 'タブ3' },
] as const satisfies TabItem[];
type PickTabValues<Arr extends readonly TabItem[]> =
Arr extends readonly [infer Item extends TabItem, ...infer Rest extends TabItem[]]
? [Item'value', ...PickTabValues<Rest>]
: []
const pickTabValues = <Arr extends readonly TabItem[]>(tabItems: Arr): PickTabValues<Arr> => {
return tabItems.map((tabItem) => tabItem.value) as PickTabValues<Arr>
}
const tabValues = pickTabValues(TAB_ITEMS)
コード
#TypeScript_Type_Challenges