My output varies based on the type of input provided. I have a custom guard in place to protect the input, but I'm still having trouble assigning it to the declared output:
type InputType<Sub extends SubType> = { a: Sub, b: string }
type SubType = Sub1 | Sub2
type Sub1 = { a: string }
type Sub2 = { a: string, b: string }
type OutputType<Sub extends SubType> =
Sub extends Sub2 ?
{ c: string, d: string } :
{ c: string }
function handle<Sub extends SubType>(mainType: InputType<Sub>): OutputType<Sub> {
if (hasSub2(mainType)) {
return {c: '', d: ''};
} else {
return {c: ''};
}
}
function hasSub2(a: InputType<SubType>): a is InputType<Sub2> {
return 'b' in a.a;
}
Explore Playground Here