I am attempting to create a type where it is keyed off of the type field of a union, so I can cast it (I know that sucks), but I think necessary due to my need to loop through a discriminated Union and act on the different specific discriminated instance. So, I have this mapping type working, but I want to make it more flexible for my helper lib:
// this works
type UnionMap<T extends { 'type': V }, V extends PropertyKey, F = 'type'> = {
[K in T['type']]: Extract<T, { 'type': K }>
};
//Would like this to work: F cannot be used to index T it says.
type UnionMap<T extends { F: V }, V extends PropertyKey, F extends PropertyKey = 'type'> = {
[K in T[F]]: Extract<T, { F: K }>
};
Would like to replace 'type' with some Generic type var F. I could not figure out how, it keeps thinking T[F] does not return a PropertyKey, even though above I have T extending { F: V }