zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Typescript] 69. Medium - Trim Right

typescript Medium right trim 69
2023-09-14 08:59:11 时间

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'

 

/* _____________ Your Code Here _____________ */
type Space = ' ' | '\t' | '\n';
type TrimRight<S extends string> = S extends `${infer Left}${Space}` 
  ? TrimRight<Left>
  : S;
  
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<TrimRight<'str'>, 'str'>>,
  Expect<Equal<TrimRight<'str  '>, 'str'>>,
  Expect<Equal<TrimRight<'str     '>, 'str'>>,
  Expect<Equal<TrimRight<'     str     '>, '     str'>>,
  Expect<Equal<TrimRight<'   foo bar  \n\t '>, '   foo bar'>>,
  Expect<Equal<TrimRight<''>, ''>>,
  Expect<Equal<TrimRight<'\n\t '>, ''>>,
]