Split
约 206 字小于 1 分钟
2026-02-11
题目
The well known split() method splits a string into an array of substrings by looking for a separator, and returns the new array. The goal of this challenge is to split a string, by using a separator, but in the type system!
For example:
type result = Split<'Hi! How are you?', ' '> // should be ['Hi!', 'How', 'are', 'you?']解题思路
待补充
答案
type Split<S extends string, SEP extends string> = any验证
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Split<'Hi! How are you?'>, ['Hi! How are you?']>>,
Expect<Equal<Split<'Hi! How are you?', 'z'>, ['Hi! How are you?']>>,
Expect<Equal<Split<'Hi! How are you?', ' '>, ['Hi!', 'How', 'are', 'you?']>>,
Expect<Equal<Split<'Hi! How are you?', ''>, ['H', 'i', '!', ' ', 'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', '?']>>,
Expect<Equal<Split<'', ''>, []>>,
Expect<Equal<Split<'The sine in cosine', 'in'>, ['The s', 'e ', ' cos', 'e']>>,
Expect<Equal<Split<'Never say never, forever and ever.', 'ver'>, ['Ne', ' say ne', ', fore', ' and e', '.']>>,
Expect<Equal<Split<'', 'z'>, ['']>>,
Expect<Equal<Split<''>, ['']>>,
Expect<Equal<Split<string, 'whatever'>, string[]>>,
]参考
无
