题目
中文
实现Trim<T>
,它是一个字符串类型,并返回一个新字符串,其中两端的空白符都已被删除。
例如
type trimed = Trim<' Hello World '> // expected to be 'Hello World'
English
Implement Trim<T>
which takes an exact string type and returns a new string with the whitespace from both ends removed.
For example
type trimmed = Trim<' Hello World '> // expected to be 'Hello World'
答案
type Trim<S extends string> = S extends `${' ' | '\n' | '\t'}${infer U}` ? Trim<U> : (S extends `${infer U}${' ' | '\n' | '\t'}` ? Trim<U> : S)
标签:Trim,Typescript,extends,体操,World,type,infer
From: https://www.cnblogs.com/laggage/p/type-challenge-trim.html