Let's consider the following scenario:
export function bar(bar?: string) {
return bar ? { bar } : {};
}
const B1 = bar();
const B2 = bar("z");
Upon compilation, the types inferred for both B1 and B2 are:
{
bar: string;
} | {
bar?: never;
}
Is there a way to persuade the compiler to infer B1
as {bar:string}
and B2
as {}
?