bitmovin-player-ui icon indicating copy to clipboard operation
bitmovin-player-ui copied to clipboard

Subclassing button requires casting of config in subclasses

Open rbokel opened this issue 6 years ago • 3 comments

When you extend the button class

export class MyButton extends Button<MyButtonConfig>

the type of config is ButtonConfig inside your subclass, which requires you to cast it. I would expect config to be of type MyButtonConfig.

That can be achieved by changing the button declaration

//from   
export class Button<Config extends ButtonConfig> extends Component<ButtonConfig> {   
//to    
export class Button<Config extends ButtonConfig> extends Component<Config> {   

wdyt?

rbokel avatar Nov 16 '17 11:11 rbokel

Yeah I'd expect that as well but unfortunately it seems to me that TypeScript does not support this kind of type inference with generics yet :/

If you change the Button declaration as in your suggestion above, then the type of this.config within Button is Config instead of ButtonConfig. I have not found a way to get the typing work correctly in all cases, but I'd be very happy if someone came along who figured it out.

protyposis avatar Nov 16 '17 11:11 protyposis

Y, that's true. The one advantage of doing like suggested would be, that you have to cast once only, when you assign this.config in the constructor. Without it, you have to cast in the subclasses everytime you want to use this.config with your own Config subclass. But y, maybe the next versions of typescript will help.

rbokel avatar Nov 16 '17 11:11 rbokel

Ah, disregard the latter, i can see now, how it causes problems further down the line, when you start using mandatory properties instead of optional, like the target in the CloseButtonConfig. Bummer! :D

rbokel avatar Nov 16 '17 12:11 rbokel