Skip to main content

2693 - EndsWith

Answer TestCases

实现EndsWith<T, U>,接收两个string类型参数,然后判断T是否以U结尾,根据结果返回truefalse

例如:

type a = EndsWith<'abc', 'bc'> // expected to be true
type b = EndsWith<'abc', 'abc'> // expected to be true
type c = EndsWith<'abc', 'd'> // expected to be false

Solution

type EndsWith<T extends string, U extends string> = T extends `${string}${U}`
? true
: false

直接通过 extends 判断 T 是否可以分成由 U 结尾的两部分,可以则返回 true