题目来源(type-challenges)
问题
实现 StartsWith<T, U> 它接受两种精确的字符串类型,并返回 T 是否以 U 结尾
type case1 = EndsWith<'abc', 'bc'> // true
type case2 = EndsWith<'abc', 'abc'> // true
type case3 = EndsWith<'abc', 'd'> // false
解答
type EndsWith<T extends string, U extends string> = T extends `${infer _F}${U}` ? true : false
拆分
- 直接用
ts 字符串分配特性解决