enumパラメータでオーバーロードされた関数を使用して型推論を実行できますか?例えば、私はその戻り値の型列挙型の値に依存ファクトリ関数を作成しようとしていると言う:enum paramsでオーバーロードされた関数の型推論?
enum Colors {
Red,
Green
};
abstract class Box { };
class RedBox extends Box { };
class GreenBox extends Box { };
class BoxFactory {
static createBox(color: Colors.Red): RedBox;
static createBox(color: Colors): Box {
switch (color) {
case Colors.Red:
return new RedBox();
case Colors.Green:
return new GreenBox();
}
}
}
function makeMeABox(color: Colors) {
// Argument of type 'Colors' is not assignable to parameter of type 'Colors.Red'
return BoxFactory.createBox(color);
}
私は宣言ファイルを生成する場合、一般的な過負荷にも表示されません。アップ。ただし、オーバーロードを削除すると問題はありませんstatic createBox(color: Colors.Red): RedBox;
。その後
class BoxFactory {
static createBox(color: Colors.Red): RedBox;
static createBox(color: Colors): Box; // <--- THIS ONE
static createBox(color: Colors): Box {
switch (color) {
case Colors.Red:
return new RedBox();
case Colors.Green:
return new GreenBox();
}
}
}
:
とC++ – Dummy