I'm currently working on a function that, when given an enum value, should return a specific type. I am facing an issue where Typescript does not seem to recognize the properties inside switch and if statements.
interface X {
x: string;
}
interface Y {
y: string;
}
enum DataType {
x = 'x',
y = 'y',
}
interface Type {
[DataType.x]: X;
[DataType.y]: Y;
}
function customFunction<T extends keyof Type>(type: T): Type[T] {
switch (type) {
case DataType.x:
return {x: 'example'}; // <--- Although there's no error, TS fails to recognize the property
}
}
Developed with Typescript version 4.2.3
.