跳到主要内容

TrimLeft

介绍

实现 TrimLeft<T> ,它接收确定的字符串类型并返回一个新的字符串,其中新返回的字符串删除了原字符串开头的空白字符串。

例如

ts
type trimed = TrimLeft<' Hello World '> // 应推导出 'Hello World '
ts
type trimed = TrimLeft<' Hello World '> // 应推导出 'Hello World '
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type TrimLeft<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<TrimLeft<'str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' str '>, 'str '>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' \n\t foo bar '>, 'foo bar '>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<''>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' \n\t'>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type TrimLeft<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<TrimLeft<'str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' str'>, 'str'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' str '>, 'str '>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' \n\t foo bar '>, 'foo bar '>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<''>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TrimLeft<' \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;
 
ts
//方法1
type Whitespace = ' ' | '\n'| '\t'
type TrimLeft<S extends string> = S extends `${Whitespace}${infer Rest}` ? TrimLeft<Rest> : S;
 
ts
// 潜在问题:
/**
* TypeScript 中的递归类型确实存在深度限制。TypeScript 默认的递归深度限制约为 45-50 层,具体限制可能因版本而略有不同。当递归嵌套达到这个限制时,TypeScript 编译器会报错。
*
* 思路
* 1.尾递归优化
* - 减少递归次数
* - 优化效果有限
* 2.分段处理
* -
*
*/
// 尾递归优化
 
type TrimRight<T extends string> =
T extends `${infer Rest} ` ? TrimRight<Rest> :
T extends `${infer Rest}\n` ? TrimRight<Rest> :
T extends `${infer Rest}\t` ? TrimRight<Rest> :
T;
 
ts
// 潜在问题:
/**
* TypeScript 中的递归类型确实存在深度限制。TypeScript 默认的递归深度限制约为 45-50 层,具体限制可能因版本而略有不同。当递归嵌套达到这个限制时,TypeScript 编译器会报错。
*
* 思路
* 1.尾递归优化
* - 减少递归次数
* - 优化效果有限
* 2.分段处理
* -
*
*/
// 尾递归优化
 
type TrimRight<T extends string> =
T extends `${infer Rest} ` ? TrimRight<Rest> :
T extends `${infer Rest}\n` ? TrimRight<Rest> :
T extends `${infer Rest}\t` ? TrimRight<Rest> :
T;
 
ts
// Trim
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
// Trim
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>>
 
view more solutions