Given a specific input to a function, how do I ensure a specific output, instead of a union?
example:
class A {
specifically_for_a () {
return "A"
}
}
class B {
specifically_for_b () {
return "b"
}
}
function USE_TYPE (type : "A" | "B") {
return {
choices : ["A", "B"],
get my_choice () {
if (type === "A") {
return new A();
}
else {
return new B();
}
}
}
}
const a = USE_TYPE("A");
// expecting to have only type A
playground:
CodePudding user response:
Simplest way would be to use overloads:
type UseTypeResult<T> = {
choices : ["A", "B"],
my_choice: T
}
function USE_TYPE (type : "A"): UseTypeResult<A>
function USE_TYPE (type : "B"): UseTypeResult<B>
function USE_TYPE (type : "A" | "B") {
// return ....
}
Another option, if you have a lot of these, would be to use an interface to map from the name to the type and then index into the interface. This would reduce the amounts of overloads you need:
interface TypeMap {
A: A,
B: B
}
function USE_TYPE <K extends keyof TypeMap>(type : K): UseTypeResult<TypeMap[K]>
function USE_TYPE (type : "A" | "B") {
///...
}
const a = USE_TYPE("A");