A common mistake: Types with many optional properties


A common mistake: Types with many optional properties.

Why? Because the property is often required in some cases.

So, optional properties often reduce type safety.

Solution: If properties are optional in some cases, create separate types for those cases.

#typescript

A helpful mindset: "Wait, I just marked this property as optional. Is it truly optional 100% of the time?"

If it's required in some cases, I should make a separate, well-named type.

This avoids lying to the compiler. The more the compiler knows, the better.

View original on X