This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.

AI solution for TypeScript puzzle on
41. implement FindIndex<T, E>

type FindIndex<T extends any[], E> = {    [K in keyof T]: T[K] extends E ? K : never;}[number] | never;


  • We define a generic type T which extends an array of any type T extends any[].
  • We further define a generic type E which represents the element we want to find the index of within array T.
  • We use a mapped type to loop through the indices K of the array T.
  • For each index K of array T, we check whether the element at index K extends type E.
  • If the element at index K extends type E, we return index K. Otherwise, we return never.
  • Finally, we use a union with never to filter out those indices that are not valid (i.e. those indices that returned never).
  • If no index is found which has the element that matches type E then it returns never.

Now, if we run the examples:

type A = [any, never, 1, '2', true]type B = FindIndex<A, 1> // 2type C = FindIndex<A, 3> // never

type B should be 2 and type C should be never.