We are currently in the process of implementing new contracts for our icons system, and we have encountered a debate on which approach is more preferable. Both options result in the same interface:
Using Generics -> Although the interface may be less clear, we benefit from auto-complete and compilation errors with zero effort once the type property is defined.Not Using Generics -> Developers will need to manually specify 'as AccountIcon' (or another type) for any object that expects to receive an Icon.
Example of Inheritance
export interface Icon {
type: 'account' | 'img' | 'font-icon';
}
export interface AccountIcon extends Icon {
readonly type: 'account';
value: LinkIcon<LinkIconType>;
}
export interface ImgIcon extends Icon {
readonly type: 'img';
value: Icon;
}
export interface FontIcon extends Icon {
readonly type: 'font-icon';
value: string;
}
Example of Generics
export interface FilterIcon<T extends IconType> {
readonly type: T;
value: T extends 'account' ? LinkIcon<LinkIconType> : T extends 'font-icon' ? string : Icon;
}
export type IconType = 'font-icon' | 'img' | 'account';