This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |