I'm trying to DRY up some of my redux action creators but am having a hard time with coming up with the proper types. Take this simplified example:
export interface SortCollectionAction {
type: string
payload: {
field: string
direction: string
}
}
export function sortCollection(type: string, field: string, direction: string): SortCollectionAction {
return {
type: type,
payload: {
field: field,
direction: direction
}
}
}
const LIST_SORT = 'LIST_SORT'
interface SortListAction extends SortCollectionAction {
type: typeof LIST_SORT
}
function sortList(field: string, direction: string): SortListAction {
return sortCollection(LIST_SORT, field, direction)
}
The compiler complains that the type 'string' is not assignable to '"LIST_SORT"'. What would be the right way to express the above?
Should I be using generics or casting the return value or is there a more straight-forward way to accomplish this?