kakasoo

FindAll 특정 문자열이 나오는 인덱스를 검색하는 타입 본문

프로그래밍/TypeScript

FindAll 특정 문자열이 나오는 인덱스를 검색하는 타입

카카수(kakasoo) 2024. 3. 23. 23:12
반응형
type Split<T extends string> = T extends `${infer FirstLetter}${infer Rest}` ? [FirstLetter, ...Split<Rest>] : [];
type StringLength<T extends string> = Split<T>['length'];
type Push<T extends any[], P = any> = [...T, P];
type NTuple<N extends number ,P extends any[] = []> = P['length'] extends N ? P : NTuple<N, Push<P, any>>;
type Add<T extends number, P extends number> = [...NTuple<T>, ...NTuple<P>]['length'];
type ToNumber<T> = T extends number ? T : never;

type RemoveFirstLetter<T extends string> = T extends `${infer F}${infer Rest}` ? Rest : '';

type FindAll<T extends string, P extends string, N extends number = 0> = 
  P extends ''
  ? []
  : T extends `${infer Front}${P}${infer Rest}` 
    ? [Add<StringLength<Front>, N>, ...FindAll<`${RemoveFirstLetter<P>}${Rest}`, P, ToNumber<Add<ToNumber<Add<N, 1>>, StringLength<Front>>>>] 
    : [];

 

반응형