Struggling with the assignment of a type.
In this scenario, the goal is to manipulate a type passed to the createMock_UserService
function. It functions correctly when the desired type is provided during the function call, but encounters an error if no Type is given, and an attempt to manually assign it as Child
results in failure as indicated in the code snippet below.
export class Parent {
}
export class Child extends Parent {
}
export function createMock_UserService<T extends Parent>(type?: Type<T>) {
if(!type) {
// encounter error -> Type 'Child' is not assignable to type 'T'
type = Child;
}
// perform actions with type
injector.get(type);
}
// function call with specific type works
createMock_UserService(Child);
// function call without specific type fails
createMock_UserService();
The definition of Type<T>
derives from angular's code and typescript core.
export declare const Type: FunctionConstructor;
interface FunctionConstructor {
/**
* Creates a new function.
* @param args A list of arguments the function accepts.
*/
new(...args: string[]): Function;
(...args: string[]): Function;
readonly prototype: Function;
}
To resolve this issue, what steps can be taken? It seems like there might be something obvious that I am overlooking.