[Typescript] TS-toolbelt Split

Example 1

import { S } from "ts-toolbelt";
import { Equal, Expect } from "../helpers/type-utils";

type Names = [
  "Matt Pocock",
  "Jimi Hendrix",
  "Eric Clapton",
  "John Mayer",
  "BB King",
];

/**
 * This is an alternative way of doing it, using S.Split
 */
type GetSurname<T extends string> = S.Split<T, " ">[1];

type tests = [
  Expect<Equal<GetSurname<Names[0]>, "Pocock">>,
  Expect<Equal<GetSurname<Names[1]>, "Hendrix">>,
  Expect<Equal<GetSurname<Names[2]>, "Clapton">>,
  Expect<Equal<GetSurname<Names[3]>, "Mayer">>,
  Expect<Equal<GetSurname<Names[4]>, "King">>,
];

 

Example 2

import { S } from "ts-toolbelt";
import { Equal, Expect } from "../helpers/type-utils";

type Path = "Users/John/Documents/notes.txt";

type SplitPath = S.Split<Path, "/">;

type tests = [
  Expect<Equal<SplitPath, ["Users", "John", "Documents", "notes.txt"]>>,
];

 

posted @ 2022-12-12 15:20  Zhentiw  阅读(95)  评论(0)    收藏  举报