挑战
实现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解答
这道题可以通过条件类型来解决。我们可以通过检查T的前缀是否等于U来实现。
type StartsWith<T extends string, U extends string> = T extends `${U}${infer _}`
? true
: false;