为什么InstanceType
在泛型上使用是错误的?是协变的还是逆变的?
interface Ctor {
new(): Instance;
}
interface Instance {
print(): void;
}
function f1<T extends Ctor>(ctor: T) {
// Error: Type 'Instance' is not assignable to Type 'InstanceType<T>'
const ins: InstanceType<T> = new ctor();
ins.print();
}
function f2(ctor: Ctor) {
// No error
const ins: InstanceType<Ctor> = new ctor();
ins.print();
}
Run Code Online (Sandbox Code Playgroud)