Typescript类型体操 - EndsWith
题目
中文
实现EndsWith<T, U>,接收两个 string 类型参数,然后判断T是否以U结尾,根据结果返回true或false
例如:
type a = EndsWith<'abc', 'bc'>; // expected to be false
type b = EndsWith<'abc', 'abc'>; // expected to be true
type c = EndsWith<'abc', 'd'>; // expected to be false
English
Implement EndsWith<T, U> which takes two exact string types and returns whether T ends with U
For example:
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
答案
type EndsWith<T extends string, U extends string> = T extends `${any}${U}`
? true
: false;
浙公网安备 33010602011771号