Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript How to declare a subclass type?

Is it possible to have something like this?

export abstract class FilterBoxElement {
    abstract getEntities: any;
}
export interface FilterBoxControlSuggestions extends FilterBoxElement {
    getEntities: // some implementation with different parameters
}
export interface FilterBoxControlDropDown extends FilterBoxElement {
    getEntities: // some implementation with different parameters
}

export interface FilterBoxDataProps {
    controlElement: FilterBoxElement // FilterBoxControlSuggestions or FilterBoxControlDropDown 
}

I want that controlElement has to be a FilterBoxControlSuggestions or a FilterBoxControlDropDown. But right now I can put everything in it. Is there a way to achieve this?

like image 333
Murat Karagöz Avatar asked Jan 05 '23 18:01

Murat Karagöz


1 Answers

You could do it with a union type:

export interface FilterBoxDataProps {
    controlElement: FilterBoxControlSuggestions | FilterBoxControlDropDown 
}

Or with generics if you want all subclasses of FilterBoxElement:

export interface FilterBoxDataProps<T extends FilterBoxElement> {
    controlElement: T
}
like image 64
toskv Avatar answered Jan 07 '23 19:01

toskv