這是超出了TypeScript能夠做的能力還是我做了一些非常錯誤的事情?下面是一些代碼,我會想有工作:打字稿子類型推斷
interface ISquare {
area: number;
}
interface ICircle {
radius: number;
}
abstract class Morph<T1, T2> {
firstOption: T1;
secondOption: T2;
}
class CircleAndSquare extends Morph<ICircle, ISquare> { }
function returnSecondOption<TMorph extends Morph<T1, T2>, T1, T2> (morph: TMorph): T2 {
return morph.secondOption;
}
returnSecondOption(new CircleAndSquare()) // adding a . here should give me intellisense for a square
智能感知應顯示ISquare
當我把一個點在函數調用returnSecondOption
結束的成員,但它沒有,它似乎將T1和T2視爲{}
。
'function returnSecondOption(morph:Morph ):T2' is correct。我沒有看到任何更復雜的事情是由OP中的情況所保證的。 –