• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

401
Views
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?

over 3 years ago · Santiago Trujillo
2 answers
Answer question

0

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
}
over 3 years ago · Santiago Trujillo Report

0

If it can be one type or the other, use a union type (|):

export interface FilterBoxDataProps {
    controlElement: FilterBoxControlSuggestions | FilterBoxControlDropDown;
}
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error