我試圖調用帶有兩個泛型的函數,但收到類型不可分配的錯誤。如何解決`類型_的參數不能分配給使用多種泛型的'T1'類型參數
abstract class foo<T1, T2> {
protected readonly generic1 : T1;
protected readonly generic2 : T2
constructor(t1: T1, t2: T2) {
this.generic1 = t1;
this.generic2 = t2;
}
}
class bar<T1, T2> extends foo<T1, T2>
{
constructor()
{
super(new someObject(), new otherObject());
}
public myFunction()
{
// I want to retain type information
this.generic1.someMethod();
this.generic2.otherMethod();
}
}
// Objects that have nothing in common, so can't interface them
class someObject {
public someMethod() { };
}
class otherObject {
public otherMethod() { };
}
這是因爲「」是類型'字符串',T1和T2不一定。酒吧是否實現了foo的字符串版本? –
您已修改了編輯中的代碼,但問題沒有解決。 'T1'可以是任何東西,所以'someObject'不一定是'T1'。 – cartant
爲什麼'bar'是通用的?爲什麼不把它做成'class bar extends foo'? –
artem