題目
中文
實現 TrimRight<T>
,它接收確定的字符串類型並返回一個新的字符串,其中新返回的字符串刪除了原字符串結尾的空白字符串。
例如
type Trimed = TrimRight<' Hello World '>; // 應推導出 ' Hello World'
English
Implement TrimRight<T>
which takes an exact string type and returns a new string with the whitespace ending removed.
For example:
type Trimed = TrimRight<' Hello World '>; // expected to be ' Hello World'
答案
type TrimRight<S extends string> = S extends `${infer L}${'\n' | '\t' | ' '}`
? TrimRight<L>
: S;