Skip to content

Instantly share code, notes, and snippets.

View trofivan's full-sized avatar

Ivan Trofimov trofivan

View GitHub Profile
type First<T extends readonly unknown[]> = T extends readonly [infer F, ...unknown[]] ? F : never
type Last<T extends readonly unknown[]> = T extends readonly [...unknown[], infer L] ? L : never
type Rest<T extends readonly unknown[]> = T extends readonly [unknown, ...infer R] ? R : never
const arr = [1, '2', Symbol(3), undefined, null] as const
type F = First<typeof arr> // 1
type L = Last<typeof arr> // null
type R = Rest<typeof arr> // ['2', symbol, undefined, null]
@trofivan
trofivan / getValue.ts
Last active November 19, 2022 15:01
TypeScript: Generics, Keyof, Index Access Type
const getValue = <T, K1 extends keyof T, K2 extends keyof T[K1]>(obj: T, key1: K1, key2?: K2) =>
key2 ? obj[key1][key2] : obj[key1]
const response = {
type: 'get',
result: {
status: 200
},
article: {
id: 1,