跳到主要内容

Trim

介绍

实现Trim<T>,它接受一个明确的字符串类型,并返回一个新字符串,其中两端的空白符都已被删除。

例如

ts
type trimed = Trim<' Hello World '> // expected to be 'Hello World'
ts
type trimed = Trim<' Hello World '> // expected to be 'Hello World'
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type Trim<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Trim<'str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<'str '>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' str '>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' \n\t foo bar \t'>, 'foo bar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<''>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' \n\t '>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type Trim<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Trim<'str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<'str '>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' str '>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' \n\t foo bar \t'>, 'foo bar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<''>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Trim<' \n\t '>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
//方法1
 
type Whitespace = ' ' | '\n'| '\t'
type TrimLeft<S extends string> = S extends `${Whitespace}${infer Rest}` ? TrimLeft<Rest> : S;
type TrimRight<S extends string> = S extends `${infer F}${Whitespace}` ? TrimRight<F> : S;
type Trim<S extends string> = TrimLeft<TrimRight<S>>
 
ts
//方法1
 
type Whitespace = ' ' | '\n'| '\t'
type TrimLeft<S extends string> = S extends `${Whitespace}${infer Rest}` ? TrimLeft<Rest> : S;
type TrimRight<S extends string> = S extends `${infer F}${Whitespace}` ? TrimRight<F> : S;
type Trim<S extends string> = TrimLeft<TrimRight<S>>
 
ts
// most popular
type Space = ' ' | '\t' | '\n';
 
type Trim<S extends string> = S extends `${Space}${infer T}` | `${infer T}${Space}` ? Trim<T> : S;
 
ts
// most popular
type Space = ' ' | '\t' | '\n';
 
type Trim<S extends string> = S extends `${Space}${infer T}` | `${infer T}${Space}` ? Trim<T> : S;
 
view more solutions