Comments (13)
type TrimLeft<V extends string> = V extends ` ${infer R}` ? TrimLeft<R> : V;
type TrimRight<V extends string> = V extends `${infer R} ` ? TrimRight<R> : V;
type Trim<V extends string> = TrimLeft<TrimRight<V>>;
// 测试用例
Trim<' semlinker '>
//=> 'semlinker'
from awesome-typescript.
// 实现一个 Trim 工具类型,用于对字符串字面量类型进行去空格处理。具体的使用示例如下所示:
type TrimLeft<T extends string> = T extends ` ${infer A}` ? TrimLeft<A> : T;
type TrimRight<T extends string> = T extends `${infer A} ` ? TrimRight<A> : T;
type Trim<V extends string> = TrimLeft<TrimRight<V>>;
// 测试用例
type T1 = Trim<' semlinker '>
type T2 = Trim<' semlinker '>
type T3 = Trim<' semlinker '>
//=> 'semlinker'
这题需要掌握的是 extends 配合 infer 在字符串中的使用,之后就是映射类型可以递归。
另外注意Trim分解成TrimLeft 和 TrimRight是分治的**。分而治之,有效的简化了问题
from awesome-typescript.
type Trim<V extends string> = V extends ` ${infer R}` | `${infer R} ` ? Trim<R> : V
type foo = Trim<' semlinker '>
from awesome-typescript.
type Trim<T extends string> = T extends `${infer F}${infer Rest}`
? F extends ' '
? Trim<Rest>
: `${F}${Trim<Rest>}`
: ''
type TTestTrim = Trim<' s em link er '>
// semlinker
from awesome-typescript.
type Trim<V extends string> = V extends ` ${infer R} ` ? R : V; // 你的实现代码
// 测试用例
type R = Trim<' semlinker '>
from awesome-typescript.
type TrimLeft<V extends string> = V extends ` ${infer U}` ? TrimLeft<U> : V
type TrimRight<V extends string> = V extends `${infer U} ` ? TrimRight<U> : V
type Trim<V extends string> = TrimLeft<TrimRight<V>>
// 测试用例
type Result = Trim<' semlinker '>
//=> 'semlinker'
from awesome-typescript.
type TrimLeft<V extends string> = V extends ` ${infer C}` ? TrimLeft<C> : V;
type TrimRight<V extends string> = V extends `${infer C} ` ? TrimRight<C> : V;
type Trim<V extends string> = TrimLeft<TrimRight<V>>;
from awesome-typescript.
// 去除左侧空格
type TrimLeft<V extends string> = V extends ` ${infer R}` ? TrimLeft<R> : V
// 去除右侧空格
type TrimRight<V extends string> = V extends `${infer L} ` ? TrimRight<L> : V
type Trim<V extends string> = TrimRight<TrimLeft<V>>
from awesome-typescript.
好多题目都融合了巧妙的算法啊
from awesome-typescript.
// 测试用例
type R = Trim<' semlinker '>
//=> 'semlinker'
type Trim<V extends string, Str extends string = ''> =
V extends `${infer First}${infer Rest}`
? First extends ' '
? Trim<Rest, Str>
: Trim<Rest, `${Str}${First}`>
: Str
from awesome-typescript.
type TrimL<V extends string> = V extends `${infer P}` ? TrimL<P> : V
type TrimR<V extends string> = V extends `${infer P}` ? TrimR<P> : V
type Trim<V extends string> = TrimL<TrimR<V>>// 你的实现代码
// 测试用例
Trim<' semlinker '>
//=> 'semlinker'
from awesome-typescript.
这题妙啊
from awesome-typescript.
type Trim<V extends string> = V extends ` ${infer S} ` ? Trim<S> : V
from awesome-typescript.
Related Issues (20)
- typescript练习第二题
- 报错的原因是返回值类型不匹配,返回值的类型是User,但T比User的范围更大
- ...
- #### 7.1 EmptyObject
- 「重学TS 2.0 」TS 练习题第四十一题 HOT 9
- 「重学TS 2.0 」TS 练习题第四十二题 HOT 12
- 「重学TS 2.0 」TS 练习题第四十三题 HOT 10
- 「重学TS 2.0 」TS 练习题第四十四题 HOT 4
- 「重学TS 2.0 」TS 练习题第四十五题 HOT 4
- 「重学TS 2.0 」TS 练习题第四十六题 HOT 5
- 「重学TS 2.0 」TS 练习题第四十七题 HOT 7
- 「重学TS 2.0 」TS 练习题第四十八题 HOT 4
- readme里的电子书下载地址403了 HOT 2
- 疑惑返回类型加Partial为什么不行 HOT 1
- 第一题
- 收藏学习!!!
- 抱歉,点击错了
- 「重学TS 2.0 」TS 练习题第四十九题 HOT 4
- > ```ts
- 练习第一题 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from awesome-typescript.