[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 '>, ''>>,
]
相关文章
- [Typescript] Be Specific for Better Inference
- [Typescript] @typescript-eslint/unbound-method
- [Typescript] 33. Medium - Flatten
- [Typescript] 32. Medium - Permutation
- [Typescript] 21. Medium - Last of Array
- [Typescript] 17. Medium - Readonly 2
- [Typescript Challenges] 16. Medium - Omit
- [TypeScript] Interface
- [TypeScript] Ensure params not accept certain type
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [Typescript] Identity function pattern - Using Identity function to enforce the type 01
- [Typescript] 96. Hard - Optional Keys
- [Typescript] 74. Medium - LastIndexOf
- [Typescript] 69. Medium - Trim Right
- [Typescript] 57. Medium - FlattenDepth
- [Typescript] 44. Medium - Drop char
- [Typescript] 36. Medium - Merge
- [Typescript] Index access types
- [Typescript] Making TypeScript Stick - 6 - infer, build a ConstructorArg Type
- [Typescript] Nullish Coalescing
- [TypeScript] Transform Existing Types Using Mapped Types in TypeScript
- [VueJS + Typescript] Decouple Dependencies Using IoC Containers in Vue with TypeScript and InversifyJS
- (转载)JavaScript大师Nicholas C. Zakas谈TypeScript