I’m using a selectorFamily to access and process data from a shared edit state. The selector uses the field name as it’s parameter, so to access the data if present on the edit object/atom.
The problem is that I can’t find a way to use the parameter (input) type (keyof EditTypes) as a generic to determine the type that the selector returns. I’m only able to say:
selectorFamily<EditTypes[keyof EditTypes] | null, keyof EditTypes>
Typescript does not know that the parameter type, ‘keyof EditTypes’, is the same key used to determine the value on the edit object that will be returned.
I tried using an indexed access type and keyof operator to by explicit about this, stating that the paramter K is the input value (lookup type) of the resulting EditType.
export const FieldEditSelector = selectorFamily<EditTypes[K] | null, K extends keyof EditTypes>({
key: "fieldEditSelector",
get:
field =>
({ get }) => {
const edit = get(WorkInProgress)
return edit && field in edit ? edit[field] : null
},
This fails since I’m not able to define K this way, but is there any other way of doing this?