E
介绍
实现EndsWith<T, U>
,接收两个string类型参数,然后判断T
是否以U
结尾,根据结果返回true
或false
例如:
ts
type a = EndsWith<'abc', 'bc'> // expected to be truetype b = EndsWith<'abc', 'abc'> // expected to be truetype c = EndsWith<'abc', 'd'> // expected to be false
View on GitHubts
type a = EndsWith<'abc', 'bc'> // expected to be truetype b = EndsWith<'abc', 'abc'> // expected to be truetype c = EndsWith<'abc', 'd'> // expected to be false
起点
ts
/* _____________ Your Code Here _____________ */typeEndsWith <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 <EndsWith <'abc', 'bc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', 'abc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', 'd'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', 'ac'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', ''>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', ' '>, false>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeEndsWith <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 <EndsWith <'abc', 'bc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', 'abc'>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', 'd'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', 'ac'>, false>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', ''>, true>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <EndsWith <'abc', ' '>, false>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeEndsWith <T extends string,U extends string> =T extends `${infer_ }${U }` ? true : false;
ts
typeEndsWith <T extends string,U extends string> =T extends `${infer_ }${U }` ? true : false;