Skeptor Skeptor - 3 years ago 97
TypeScript Question

Parameter of generic type extends a class

There is a class of type

Action
.
I need to declare a function such that it takes a parameter of a class which extends Action along with additional parameter . For ex:

class Abc extends Action {
payload: any;
}


My actual function :

function xyz(action: Abc){
// ...
}


But
action: Abc
should be generic something like

action: {payload: string} extends Action


Scenario (Optional):
I am struggling with this as ngrx 4 removed payload from Action but my older code expects it to have payload .

Answer Source

You had it almost right. Change it to this

private xyz = (action: { payload: string } & Action) => {
}

Look here for Typescript Intersection types

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download