StartsWith
题目
实现StartsWith<T, U>,接收两个 string 类型参数,然后判断T是否以U开头,根据结果返回true或false
例如:
typescript
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解答
ts
type StartsWith<T extends string, U extends string> = U extends ""
? true
: T extends `${U}${infer _}`
? true
: false;精选
take it a litter change type StartsWith<T extends string, U extends string> = T extends
${U}${infer Other}?true:falseif T extends
${U}${some}, Prove that T contains U, but U not prove that Tso: Expect<Equal<StartsWith<'abc', 'ab'>, true>>, Expect<Equal<StartsWith<'abc', 'abcd'>, false>>,
tstype StartsWith< T extends string, U extends string > = T extends `${U}${string}` ? true : false;