Head
自 1.0.0 起
获取元组的第一个元素。
签名
typescript
type Head<T extends readonly unknown[]> = T extends readonly [infer H, ...unknown[]]
? H
: T extends readonly (infer E)[]
? E
: never参数
| 参数 | 描述 |
|---|---|
T | 元组类型 |
示例
基本用法
typescript
import type { Head } from 'uni-types'
type First = Head<[1, 2, 3]> // 1
type FirstStr = Head<['a', 'b', 'c']> // 'a'单元素
typescript
type Single = Head<[string]> // string数组
typescript
type FromArray = Head<string[]> // string
type FromNumberArray = Head<number[]> // number空元组
typescript
type Empty = Head<[]> // never