StartsWith
约 154 字小于 1 分钟
2026-02-11
题目
实现StartsWith<T, U>,接收两个string类型参数,然后判断T是否以U开头,根据结果返回true或false
例如:
type a = StartsWith<'abc', 'ac'> // expected to be false
type b = StartsWith<'abc', 'ab'> // expected to be true
type c = StartsWith<'abc', 'abcd'> // expected to be false解题思路
待补充
答案
type StartsWith<T extends string, U extends string> = any验证
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<StartsWith<'abc', 'ac'>, false>>,
Expect<Equal<StartsWith<'abc', 'ab'>, true>>,
Expect<Equal<StartsWith<'abc', 'abc'>, true>>,
Expect<Equal<StartsWith<'abc', 'abcd'>, false>>,
Expect<Equal<StartsWith<'abc', ''>, true>>,
Expect<Equal<StartsWith<'abc', ' '>, false>>,
Expect<Equal<StartsWith<'', ''>, true>>,
]参考
无
