Currently, I am attempting to create a function where the return type is determined by a generic argument. Let me share a code snippet to illustrate:
type ABCDE = 'a' | 'b';
function newFunc<U extends ABCDE>(input: U): U extends 'a' ? 'result1' : 'result2' {
if (input === 'a') {
return 'result1';
}
return 'result2';
}
const responseOne = newFunc('a'); // this should be of type 'result1'
const responseTwo = newFunc('b'); // this should be of type 'result2'
While inside the function, I encountered the error
Type '"result1"' is not assignable to type 'U extends "a" ? "result1" : "result2"'.
. I'm uncertain how to address this issue.
I have come across similar questions, but none specifically tackle the combination of dependent return types with union types. The closest reference I found was in this post, which suggests that it may currently be an insurmountable obstacle, though I cannot say for certain.