Trim
介绍
实现Trim<T>
,它接受一个明确的字符串类型,并返回一个新字符串,其中两端的空白符都已被删除。
例如
ts
type trimed = Trim<' Hello World '> // expected to be 'Hello World'
View on GitHubts
type trimed = Trim<' Hello World '> // expected to be 'Hello World'
起点
ts
/* _____________ Your Code Here _____________ */typeTrim <T > = any/* _____________ Test Cases _____________ */typecases = [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 <' 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 '>, ''>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeTrim <T > = any/* _____________ Test Cases _____________ */typecases = [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 <' 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 '>, ''>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
//方法1typeWhitespace = ' ' | '\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
//方法1typeWhitespace = ' ' | '\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
// most populartypeSpace = ' ' | '\t' | '\n';typeTrim <S extends string> =S extends `${Space }${inferT }` | `${inferT }${Space }` ?Trim <T > :S ;
ts
// most populartypeSpace = ' ' | '\t' | '\n';typeTrim <S extends string> =S extends `${Space }${inferT }` | `${inferT }${Space }` ?Trim <T > :S ;