Can type inference be performed using an overloaded function with an enum parameter? For instance, I am attempting to create a factory function where the return type is based on an enum value:
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);
}
If I generate a declarations file, the general overload does not appear. However, everything works as expected if the overload
static createBox(color: Colors.Red): RedBox;
is removed.