其他分享
首页 > 其他分享> > [Typescript] 31. Medium - Length of String

[Typescript] 31. Medium - Length of String

作者:互联网

Compute the length of a string literal, which behaves like String#length.

/* _____________ Your Code Here _____________ */

type LengthOfString<S extends string, ACC extends any[] = []> = S extends '' ? ACC['length']: S extends `${infer First}${infer RT}` ? LengthOfString<RT, [...ACC, First]> : never;


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<LengthOfString<''>, 0>>,
  Expect<Equal<LengthOfString<'kumiko'>, 6>>,
  Expect<Equal<LengthOfString<'reina'>, 5>>,
  Expect<Equal<LengthOfString<'Sound! Euphonium'>, 16>>,
]

 

标签:Typescript,String,_____________,31,length,Length,extends,Expect,type
来源: https://www.cnblogs.com/Answer1215/p/16693180.html