Comments (9)
type RepeatString<
T extends string,
C extends number,
A extends any[] = []
> = A['length'] extends C ? '' : `${T}${RepeatString<T, C, [...A, T]>}`
type S0 = RepeatString<"a", 0>; // ''
type S1 = RepeatString<"a", 2>; // 'aa'
type S2 = RepeatString<"ab", 3>; // 'ababab'
from awesome-typescript.
type RepeatString<T extends string, C extends number, A extends any[] = [], R extends string = ""> = A["length"] extends C
? R
: RepeatString<T, C, [...A, ''], `${R}${T}`;
type S0 = RepeatString<"a", 0>; // ''
type S1 = RepeatString<"a", 2>; // 'aa'
type S2 = RepeatString<"ab", 3>; // 'ababab'
思路: 数字的比较只能利用 数组的 lenght属性来实现,字符串的 length属性在tsc阶段无法获取到真实长度,只能得到number类型。
from awesome-typescript.
type Repeat<T, C extends number, A extends any[] = []> = A["length"] extends C
? A
: Repeat<T, C, [...A, T]>;
type Join<T extends string[], splitor extends string = ""> = T extends [infer F, ...infer R]
? R extends string[]
? F extends string
? `${F}${splitor}${Join<R, splitor>}`
: ""
: F
: "";
type RepeatString<T extends string, C extends number> = Join<Repeat<T, C>, "">;
type S0 = RepeatString<"a", 0>; // ''
type S1 = RepeatString<"a", 2>; // 'aa'
type S2 = RepeatString<"ab", 3>; // 'ababab'
from awesome-typescript.
type Push<T extends any[], V> = [...T, V];
type RepeatString<
T extends string,
C extends number,
R extends string = '',
A extends any[] = [],
> = A['length'] extends C ? R: (RepeatString<T, C, `${R}${T}`, Push<A, T>>)
type S01 = RepeatString<"a", 0>; // ''
type S11 = RepeatString<"a", 2>; // 'aa'
type S21 = RepeatString<"ab", 3>; // 'ababab'
type S22 = RepeatString<"abc", 3>; // 'abcabcabc'
from awesome-typescript.
用模板字符串进行递归就行了
type RepeatString<
T extends string,
C extends number,
S extends any[] = [], // 用于判断是否递归完毕
U extends string = '' // 用于累加记录已遍历过的字符串
> = S['length'] extends C ? U : RepeatString<T, C, [...S, 1], `${U}${T}`>
from awesome-typescript.
直接使用第九题的JoinStrArray操作Repeat后的元组
type RepeatString<T extends string, C extends number> = JoinStrArray<
Repeat<T, C>,
""
>;
type S02 = RepeatString<"a", 0>; // ''
type S12 = RepeatString<"a", 2>; // 'aa'
type S22 = RepeatString<"ab", 3>; // 'ababab'
from awesome-typescript.
type _RepeatString<
T extends string,
C extends number,
S extends string,
Count extends string[]
> = Count["length"] extends C
? S
: _RepeatString<T, C, `${S}${T}`, [...Count, T]>;
type RepeatString<T extends string, C extends number> = _RepeatString<
T,
C,
"",
[]
>;
from awesome-typescript.
// 将RepeatString拆分成两步: 1. 将其转变成目标数组 2. 将数组拼接成为字符串
// 1. 通过RepeatStr将字符串和数量转变成为数组
// 如: <'ab', 2> => ['ab', 'ab']
type RepeatStr<S extends string, N extends number, R extends string[] = []> = R['length'] extends N ? R : RepeatStr<S, N, [...R, S]>
// 2. 创建一个Join帮助类型, 将重复的数组变成字符串
// 如: ['ab', 'ab'] => 'abab'
type Join<T extends string[]> = T extends [f: infer F, ...r: infer R] ? R extends string[] ? F extends string ? `${F}${Join<R>}` : '' : '' : ''
// 3. 组合上面的两个类型即可
type RepeatString<
T extends string,
C extends number,
> = Join<RepeatStr<T, C>>
from awesome-typescript.
type RepeatString<T extends string, C extends number, S extends string = '', R extends T[] = []> = R['length'] extends C
? S
: RepeatString<T, C, `${S}${T}`, [...R, T]>; // 你的实现代码
type S0 = RepeatString<'a', 0>; // ''
type S1 = RepeatString<'a', 2>; // 'aa'
type S2 = RepeatString<'ab', 3>; // 'ababab'
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.