Check out this code snippet featuring conditional types:
class X {
public x: number;
}
class Y {
public y: number;
}
type DataCategory = "x" | "y";
type TData<T extends DataCategory> =
T extends "x" ? X :
T extends "y" ? Y :
never;
I'm trying to link a function parameter with its return type using conditional types. However, my attempts have been unsuccessful so far:
function RetrieveData<T extends DataCategory>(category: T): TData<T> {
if (category == "x")
return new X();
else if (category == "y")
return new Y();
}
What is the correct syntax for achieving this? Is it feasible in TypeScript 2.8?
Update
There's an ongoing issue on GitHub that addresses a similar example. At the moment, the answer seems to be "No, but there might be potential in the future."