Here is some code with conditional type
class A {
public a: number;
}
class B {
public b: number;
}
type DataType = "a" | "b";
type TData<T extends DataType> =
T extends "a" ? A :
T extends "b" ? B :
never;
Now I want to use conditional type as a link from function parameter to its return type. I tried to achieve this in different ways with no result:
function GetData<T extends DataType>(dataType: T): TData<T> {
if (dataType == "a")
return new A();
else if (dataType == "b")
return new B();
}
What is the proper syntax? Is it possible with TypeScript 2.8?
Update
There is already an opened issue on github that covers my example. So current answer is "No, but may be possible in future".
DataType
transforms into a large enum. – Olivas