I have an array of values:
const array = ['b', 'a', 'b', 'a', 'c'] as const;
So the type is:
const array: readonly ["b", "a", "b", "a", "c"]
Now I want to create a type that filters the array, and returns a type of array with only the filtered elements. I've created a FilterArray type, that does the trick, however, the indexes on the array are taken from the first array and not restarted.
type FilterArray<T extends readonly any[], V> = {
[I in keyof T as [Extract<T[I], V>] extends [never] ? never : I]: T[I];
}
If I filter now on typeof array, I get these results:
filteredArray[3] is also of type 'a', which is correct.
Is it possible to restart the index from 0? Or should I take another approach?