I have the following code where the return type of the function a is inferred to be {a?: undefined} | {a: number}, and the return type of the function b is inferred to be {}. Can someone explain why, and if I can get TypeScript to infer what I believe is the more correct type in the second case?
function a() {
if (Math.random()) {
return {};
}
return {a: 0};
}
function b() {
if (Math.random()) {
return {};
}
const t: {a: number} = {a: 0};
return t;
}