Closed
Description
Bug Report
π Search Terms
unification, variable type, conditional inference
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ
β― Playground Link
Playground link with relevant code
π» Code
const
foo = { a: 1 },
bar = { a: 1, b: 2 };
const result = Math.random() > 0.5 ?
foo : bar;
const resolve = () => {
if (Math.random() > 0.5)
return foo;
else
return bar;
};
π Actual behavior
result
type and resolve
return type inferred as foo
only ({ a: number; }
).
π Expected behavior
result
type and resolve
return type obviously should be inferred as foo | bar
(in some form of { a: number; }
and { a: number; b: number; }
union).
More info
This only happens when:
foo
andbar
share at least 1 property.- Objects used as variables. Using inline objects, types are inferred as intended.
const result = Math.random() > 0.5 ?
{ a: 1 } : { a: 1, b: 2 };
const resolve = () => {
if (Math.random() > 0.5)
return { a: 1 };
else
return { a: 1, b: 2 };
};
Here types are inferred as expected:
{
a: number;
b?: undefined;
} | {
a: number;
b: number;
}