I am looking to implement generics in an Angular service so that users can input an array of any interface/class/type they desire, with the stipulation that the type must extend an interface provided by the service. It may sound complex, but here's a glimpse at how the service is currently structured:
export interface ProvidedInterface {
stepId: number;
parentId: number;
}
@Injectable()
export class ManagerService {
private myModelBs: BehaviorSubject<Array<Array<T extends ProvidedInterface>>> = new BehaviorSubject<Array<Array<T extends ProvidedInterface>>>(null);
constructor() {}
}
The BehaviorSubject
will hold a multi-dimensional array, where each element within the array conforms to the generic type that extends the ProvidedInterface
. However, this setup seems problematic. VS Code is indicating red squiggly lines under the T
, stating it cannot recognize the identifier "T", while also highlighting the first >
following the nested Array
type declaration with a "? expected" message.
While I may be approaching this incorrectly, my intention was to utilize generics that extend the provided interface to ensure the service remains independent of any other fields aside from those outlined in the provided interface.
This concept might be challenging to grasp, but I am puzzled as to why VS Code is encountering issues with using T
for a generic parameter initially. Ideally, this approach should theoretically function even if there are hurdles elsewhere.