0

I am looking at some legacy code and they have created all the redux reducers as instance methods of a class:

@Injectable()
export class PeopleActions {
    constructor(private ngRedux: NgRedux<any>) {}

    add() {
      this.ngRedux.dispatch({ADD, payload: {foo: 'bar;});
    }

    remove() {
      this.ngRedux.dispatch({Remove, payload: {foo: 'bar;});
    }
    // etc.

I would normally create these as sepreate functions

export function add { // etc.}
export function remove { // etc.}

And then create a union:

type MyActions = add | remove;

Can I somehow create a union of the class instance methods?

dagda1
  • 26,856
  • 59
  • 237
  • 450
  • You want to create the `MyActions` type? Should it be a union of the names of the methods or the signatures of the methods ? Because the syntax is not valid as is in the question – Titian Cernicova-Dragomir Jan 08 '19 at 12:30

1 Answers1

1

If you want a union of all keys in the type you can use keyof

type MyActions = keyof PeopleActions; // "add" | "remove"

If the class also has public fields that are not methods and you want to filter those out you can use a conditional type:

type ExtractFunctionKeys<T> = { [P in keyof T]-?: T[P] extends Function ? P : never}[keyof T]
type MyActions = ExtractFunctionKeys<PeopleActions>; // "add" | "remove"
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357