TrimLeft
介绍
实现 TrimLeft<T>
,它接收确定的字符串类型并返回一个新的字符串,其中新返回的字符串删除了原字符串开头的空白字符串。
例如
ts
type trimed = TrimLeft<' Hello World '> // 应推导出 'Hello World '
View on GitHubts
type trimed = TrimLeft<' Hello World '> // 应推导出 'Hello World '
起点
ts
/* _____________ Your Code Here _____________ */typeTrimLeft <T > = any/* _____________ Test Cases _____________ */typecases = [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 <' 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'>, ''>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeTrimLeft <T > = any/* _____________ Test Cases _____________ */typecases = [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 <' 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'>, ''>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
//方法1typeWhitespace = ' ' | '\n'| '\t'typeTrimLeft <S extends string> =S extends `${Whitespace }${inferRest }` ?TrimLeft <Rest > :S ;
ts
//方法1typeWhitespace = ' ' | '\n'| '\t'typeTrimLeft <S extends string> =S extends `${Whitespace }${inferRest }` ?TrimLeft <Rest > :S ;
ts
// 潜在问题:/*** TypeScript 中的递归类型确实存在深度限制。TypeScript 默认的递归深度限制约为 45-50 层,具体限制可能因版本而略有不同。当递归嵌套达到这个限制时,TypeScript 编译器会报错。** 思路* 1.尾递归优化* - 减少递归次数* - 优化效果有限* 2.分段处理* -**/// 尾递归优化typeTrimRight <T extends string> =T extends `${inferRest } ` ?TrimRight <Rest > :T extends `${inferRest }\n` ?TrimRight <Rest > :T extends `${inferRest }\t` ?TrimRight <Rest > :T ;
ts
// 潜在问题:/*** TypeScript 中的递归类型确实存在深度限制。TypeScript 默认的递归深度限制约为 45-50 层,具体限制可能因版本而略有不同。当递归嵌套达到这个限制时,TypeScript 编译器会报错。** 思路* 1.尾递归优化* - 减少递归次数* - 优化效果有限* 2.分段处理* -**/// 尾递归优化typeTrimRight <T extends string> =T extends `${inferRest } ` ?TrimRight <Rest > :T extends `${inferRest }\n` ?TrimRight <Rest > :T extends `${inferRest }\t` ?TrimRight <Rest > :T ;
ts
// TrimtypeWhitespace = ' ' | '\n'| '\t'typeTrimLeft <S extends string> =S extends `${Whitespace }${inferRest }` ?TrimLeft <Rest > :S ;typeTrimRight <S extends string> =S extends `${inferF }${Whitespace }` ?TrimRight <F > :S ;typeTrim <S extends string> =TrimLeft <TrimRight <S >>
ts
// TrimtypeWhitespace = ' ' | '\n'| '\t'typeTrimLeft <S extends string> =S extends `${Whitespace }${inferRest }` ?TrimLeft <Rest > :S ;typeTrimRight <S extends string> =S extends `${inferF }${Whitespace }` ?TrimRight <F > :S ;typeTrim <S extends string> =TrimLeft <TrimRight <S >>