type-challenges / type-challenges

Collection of TypeScript type challenges with online judge

Home Page:https://tsch.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

9286 - FirstUniqueCharIndex

sunupupup opened this issue · comments

key point is find the ${Head}${Tail} extends ${any}${Char}${any}

Head + Char + Tail = T

type FirstUniqueCharIndex<
  T extends string,
  Head extends string = '',
  Count extends any[] = []
> = T extends `${infer Char}${infer Tail}`
  ? `${Head}${Tail}` extends `${any}${Char}${any}`
    ? FirstUniqueCharIndex<Tail, `${Head}${Char}`, [...Count, 1]>
    : Count['length']
  : -1;