0

I can do this:

export function changeParameter(parameter: Parameters.booleanParameter, value: boolean): ChangeParameterAction
export function changeParameter(parameter: Parameters.stringParameter, value: boolean): ChangeParameterAction
// ... more types

export function changeParameter(parameter: Parameters, value: any): ChangeParameterAction {
  return { type: PARAMETER_CHANGED, parameter, value };
};

But I can't seem to figure out how to do the following without getting any errors (including changing the declarations to similar arrow functions but I so don't know how to do that right that I'm not pasting any examples.

const changeParameter = (parameter: Parameters, value: any): ChangeParameterAction => ({ type: PARAMETER_CHANGED, parameter, value })

Is there a way to do this? (Also is there any shorter way to construct this overload without listing all the combinations separately?)

VillageTech
  • 1,968
  • 8
  • 18
Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
  • 1
    Does this answer your question? [Typescript overload arrow functions](https://stackoverflow.com/questions/39187614/typescript-overload-arrow-functions) – ford04 Jan 13 '20 at 16:15
  • It seems your return type is always the same `ChangeParameterAction`, so you could just combine your function argument types as union like `const changeParameter = (parameter: Parameters.booleanParameter | Parameters.stringParameter, value: boolean): ChangeParameterAction => {...}`. If that still doesn't help, please provide all types in the example, so we can help better. – ford04 Jan 13 '20 at 16:18

0 Answers0