I have a type defined like so:
type FieldToAction = {
[key in ApiActions]: ((field: Field) => void) | ((field: Field, fieldValue: FieldValue) => void)
}
where FieldValue
is a union of types:
type FieldValue = string | string[] | AssigneeValue | PriorityValue
I have a variable object where I then declare the functions set by the above types:
const fieldToAction: FieldToAction = {
.
.
.
"SET_DESCRIPTION": (field: Field, fieldValue: string) => field.setDescription(fieldValue),
"SET_LABELS_VALUE": (field: Field, fieldValue: string[]) => field.setValue(fieldValue)
};
This produces an error where the values in the object are not of type FieldToAction
, I kind of understand why since I am now constraining the parameter to be a string
or string[]
.
My question is: Is there a way of still using the union of types' type and constraining the value in the parameter?
Thank you