題目
中文
實現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;