Skip to content

Length of String

题目

计算字符串的长度,类似于 String#length

例如

ts
type Str1 = "";
type Length1 = LengthOfString<Str1>; // expected to be 0

type Str2 = "kumiko";
type Length2 = LengthOfString<Str2>; // expected to be 6

type Str3 = "Sound! Euphonium";
type Length3 = LengthOfString<Str3>; // expected to be 16

解答

ts
namespace t00298 {
  // 原始类型
  type S = "kumiko";

  // 字符串转换为数据
  type StringToArray<S extends string> = S extends `${infer First}${infer Rest}`
    ? [First, ...StringToArray<Rest>]
    : [];

  // type S1 = ["k", "u", "m", "i", "k", "o"]
  type S1 = StringToArray<S>;

  // 通过数组的 length 属性获取长度
  // type S2 = 6
  type S2 = S1["length"];

  export type LengthOfString<T extends string> = StringToArray<T>["length"];
}

Released under the MIT License.