type LengthOfString<S extends string, AsArray extends string[] = ['']> = S extends `${infer First}${infer Rest}`
? LengthOfString<Rest, [...AsArray, First]>
: { [K in keyof AsArray]: K extends `${infer Index extends number}` ? Index : never } extends [...unknown[], infer Last]
? Last
: never
Solution by stackoverfloweth #35258
type LengthOfString<S extends string, ACC extends string[] = []> = S extends `${string}${infer Rest}` ? LengthOfString<Rest, [...ACC, string]> : ACC['length']
Solution by eunsukimme #35229
// 여기 풀이를 입력하세요
type LengthOfString<
S extends string,
T extends string[] = []
> = S extends `${infer F}${infer R}`
? LengthOfString<R, [...T, F]>
: T['length'];
Solution by LeeKangHyun #35012
Use recursive type to make a split array. Then return its length.
// your answers
type SplittedArray<S extends string> = S extends `${S[0]}${infer Rest}` ? [S[0], ...SplittedArray<Rest>] : [];
type LengthOfString<S extends string> = SplittedArray<S>['length'];
Solution by dev-jaemin #35008
// 你的答案
type StringToTuple<S extends string> = S extends `${infer A}${infer Rest}`
? [A, ...StringToTuple<Rest>]
: [];
type LengthOfString<S extends string> = StringToTuple<S>["length"];
kill two birds with one stone.
Solution by shx123qwe #34966
type LengthOfString<
S extends string,
T extends string[] = []
> = S extends `${infer F}${infer R}`
? LengthOfString<R, [...T, F]>
: T['length'];
Solution by devshinthant #34581
type StringToArr<S extends string> = S extends `${infer p}${infer rest}` ? [p, ...StringToArr<rest>] : []
type LengthOfString<S extends string> = StringToArr<S>['length']
Solution by ouzexi #34011
type LengthOfString<S extends string, N extends any[] = []> = S extends ''
? N['length']
: S extends `${infer A}${infer Rest}`
? LengthOfString<Rest, [...N, A]>
: N['length']
Solution by laplace1009 #33805
type Len<T extends string, L extends string[] = []> = T extends ${infer K}${infer P}
? Len<P, [...L, K]> :L["length"]
Solution by rookiewxy #33733
type StringToArray<S extends string> = S extends `${infer a}${infer b}` ? [a, ...StringToArray<b>] : []
type LengthOfString<S extends string> = StringToArray<S>["length"]
Solution by mosey-san #33727
// 先转个数组类型
type stringToArray<S extends string> = S extends `${infer F}${infer Rest}`
? [F, ...stringToArray<Rest>]
: [];
type LengthOfString<S extends string> = S extends ""
? 0
: stringToArray<S>["length"];
Solution by fyuanz #33686
// your answers
type LengthOfString<S extends string, Result extends string[] = []> = S extends `${infer F}${infer R}`
? LengthOfString<R, [...Result, F]>
: Result['length']
Solution by KeithChou #33219
type LengthOfString<S extends string, L extends string[] = []> = S extends `${infer F}${infer Last}` ? LengthOfString<Last, [...L, F]> : L['length']
Solution by ZhipengYang0605 #32822
type StringLength<T extends string, Acc extends 0[] = []> = T extends `${string}${infer Rest}`
? StringLength<Rest, [...Acc, 0]>
: Acc['length']
Solution by ZhulinskiiDanil #32684
type StringToTuple<S extends string> = S extends `${infer F}${infer Rest}`
? [F,...StringToTuple<Rest>] : []
type LengthOfString<S extends string> = StringToTuple<S>["length"]
Solution by avidang #32530
type LengthOfString<S extends string, A extends string[] = []> =
S extends `${infer F}${infer R}`
? LengthOfString<R, [...A, F]>
: A['length']
Solution by dev-hobin #32402
// 解答をここに記入
type LengthOfString<S extends string, T extends string[]=[]> = S extends `${infer F}${infer R}`
? LengthOfString<R, [...T, F]>
: T['length'];
Solution by pea-sys #32121
// 你的答案
type LengthOfString<S extends string, Strs extends any[] = []> = S extends `${infer F}${infer Rest}`
? F extends ''
? 0
: LengthOfString<Rest, [F, ...Strs]>
: Strs["length"]
Solution by Codec-k #32081
type LengthOfString<S extends string, U extends string[] = []> = S extends `${infer F}${infer L}`
? L extends ""
? [L, ...U]["length"]
: LengthOfString<L, [...U, F]>
: 0;
Solution by gasmg #32014
type LengthOfString<S extends string, T extends string[] = []> = S extends `${infer F}${infer R}`
? LengthOfString<R, [...T, F]>
: T['length'];
Solution by jinyoung234 #31736
type LengthOfString<S extends string, A extends any[] = []> = S extends `${infer F}${infer Rest}` ? LengthOfString<Rest, [...A, F]> : A['length'];
Solution by kai-phan #31641
type LengthOfString<
S extends string,
Letters extends string[] = [],
> = S extends ''
? Letters['length']
: S extends `${infer _L}${infer R}`
? LengthOfString<R, [...Letters, R]>
: unkown;
Solution by jakubjereczek #31536
type LengthOfString<
S extends string,
C extends any[] = []
> = S extends `${infer H}${infer T}`
? LengthOfString<T, [...C, H]>
: C["length"];
Solution by vipulpathak113 #31475
I have looked through the different solutions and realized my solution is quite quirky so I wanted to share it.
type LengthOfString<S extends string, T = S> = S extends T ?
LengthOfString<S, never>['length'] :
S extends `${infer ST}${infer Rest}` ?
readonly [ST, ...LengthOfString<Rest, never>] :
readonly []
Solution by roykoren10 #31458
type LengthOfString<
S extends string,
T extends string[] = []
> = S extends `${infer F}${infer R}`
? LengthOfString<R, [...T, F]>
: T['length'];
Solution by MyeonghoonNam #31418
type LengthOfString<S extends string, R extends readonly string[] = []> = S extends `${infer A}${infer B}` ? LengthOfString<B, readonly [...R, A]> : R['length'];
Solution by eward957 #31244
type LengthList<S extends string> = S extends `${infer F}${infer Rest}` ? [F, ...LengthList<Rest>] : []
type LengthOfString<S extends string> = LengthList<S>['length']
Solution by moonshadow-miao #30727
type StringToArray<S extends string> = S extends `${infer A}${infer B}` ? [A, ...StringToArray<B>] : []
type LengthOfString<S extends string> = StringToArray<S>['length']
string or array has 'length',so,we can convert it to array.
Solution by MrRabbit1993 #30442
type Split<S extends string> = S extends '' ? [] : S extends `${infer F}${infer R}` ? [F, ...Split<R>] : [];
type LengthOfString<S extends string> = Split<S>['length'];```
Solution by kai-phan #30349
type LengthOfString<S extends string, L extends string[] = []> = S extends `${infer A}${infer B}` ? LengthOfString<B, [...L, A]> : L['length']
Solution by zhangqiangzgz #30177