Take a look at this TypeScript code snippet:
const test = Math.random() < 0.5 ? { a: 1, b: 2 } : {};
Based on the code above, I would assume the type of object 'test' to be:
const test: {
a: number;
b: number;
} | {}
This is the most strict type description for the object - either it has two properties if the conditional is true, or no properties if it's false.
However, upon checking the actual type, I see:
const test: {
a: number;
b: number;
} | {
a?: undefined;
b?: undefined;
}
This seems incorrect as there is no case where properties can exist with undefined values.
Another scenario:
https://i.stack.imgur.com/j39iX.png
In this example, I define a type where the 'items' property may not exist or may have a defined value. But upon inspection, the type allows 'items' to exist as a property with a value of undefined.
What am I missing in terms of TypeScript's assurances about typesafety?