有可能有这样的东西吗?
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 }
我希望那controlElement
必须是一个FilterBoxControlSuggestions
或一个FilterBoxControlDropDown
.但是现在我可以把所有东西放进去.有没有办法实现这个目标?
您可以使用联合类型执行此操作:
export interface FilterBoxDataProps {
controlElement: FilterBoxControlSuggestions | FilterBoxControlDropDown
}
或者如果你想要FilterBoxElement的所有子类,那么使用泛型:
export interface FilterBoxDataProps {
controlElement: T
}