My Angular component serves as a superclass and is never instantiated on its own. There are multiple components that extend this one, each operating on different data types, so the superclass utilizes generic typing.
In addition to this, the superclass must be able to create objects of the provided type. Therefore, you can see that the constructor of the component takes a parameter to define a constructor for the generic type.
@Component({
selector: 'my-component',
templateUrl: './my-component.component.html'
})
export class MyComponent<Class1> implements OnInit {
public object!:Class1;
constructor(protected type1 : new () => Class1) {
this.object = new this.type1();
}
}
It's important to note that when the subclass component calls super(), it always provides the actual type (e.g. MyClass), ensuring the component has access to it.
An essential point to highlight is that this setup works perfectly fine. When I use `ng serve`, the generic type is passed from the subclass component, and the entire page functions correctly as intended.
I'm now working on transforming this generic component into a library package for broader usage. However, I face an issue during the build process where I receive the error message: "error NG2003: No suitable injection token for parameter 'type1' of class 'MyComponent'."
The Angular compiler mistakenly believes it needs to inject something into that constructor parameter, even though I have no need for this. How can I disable this behavior so that the compiler allows me to build and pass the constructor argument as I already know how to do?