StartsWith
介绍
实现StartsWith<T, U>
,接收两个string类型参数,然后判断T
是否以U
开头,根据结果返回true
或false
例如:
ts
type a = StartsWith<'abc', 'ac'> // expected to be falsetype b = StartsWith<'abc', 'ab'> // expected to be truetype c = StartsWith<'abc', 'abcd'> // expected to be false
View on GitHubts
type a = StartsWith<'abc', 'ac'> // expected to be falsetype b = StartsWith<'abc', 'ab'> // expected to be truetype c = StartsWith<'abc', 'abcd'> // expected to be false
起点
ts
/* _____________ Your Code Here _____________ */typeStartsWith <T extends string,U extends string> = any;/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'ac'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'ab'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'abc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'abcd'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', ''>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', ' '>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'', ''>, true>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeStartsWith <T extends string,U extends string> = any;/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'ac'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'ab'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'abc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', 'abcd'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', ''>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'abc', ' '>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <StartsWith <'', ''>, true>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeStartsWith <T extends string,U extends string> =T extends `${U }${string}` ? true : false;
ts
typeStartsWith <T extends string,U extends string> =T extends `${U }${string}` ? true : false;