Trim Right
介绍
实现 TrimRight<T>
,它接收确定的字符串类型并返回一个新的字符串,其中新返回的字符串删除了原字符串结尾的空白字符串。
ts
type Trimed = TrimRight<' Hello World '> // 应推导出 ' Hello World'
View on GitHubts
type Trimed = TrimRight<' Hello World '> // 应推导出 ' Hello World'
起点
ts
/* _____________ Your Code Here _____________ */typeTrimRight <S extends string> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'str'>, 'str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'str '>, 'str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'str '>, 'str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <' str '>, ' str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <' foo bar \n\t '>, ' foo bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <''>, ''>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'\n\t '>, ''>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeTrimRight <S extends string> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'str'>, 'str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'str '>, 'str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'str '>, 'str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <' str '>, ' str'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <' foo bar \n\t '>, ' foo bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <''>, ''>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <TrimRight <'\n\t '>, ''>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeTrimRight <S extends string> =S extends `${inferF }${' '|'\n'|'\t'}` ?TrimRight <F > :S ;
ts
typeTrimRight <S extends string> =S extends `${inferF }${' '|'\n'|'\t'}` ?TrimRight <F > :S ;