Skip to content

PathLength

自 1.1.0 起

获取路径的段数。

签名

typescript
type PathLength<P extends string> = P extends ''
  ? 0
  : P extends `${string}.${infer Rest}`
    ? Increment<PathLength<Rest>>
    : 1

参数

参数描述
P路径字符串

示例

基本用法

typescript
import type { PathLength } from 'uni-types'

type A = PathLength<'a.b.c'>   // 3
type B = PathLength<'single'>  // 1
type C = PathLength<''>        // 0

条件逻辑

typescript
type ShallowPath<P extends string> = PathLength<P> extends 1 | 2
  ? P
  : never

相关

基于 MIT 许可发布