Type Narrowing With Similarly Shaped Objects
Let's say we have a type with several properties and a variable of that type.
We can use variables of this type in narrower contexts as long as the properties that are present have aligning types.
For instance, we can pass a User
to this sendNewsletter
function. Even though the types don't match exactly, the type of sendNewsletter
's parameter is a subset of User
.
This is a form of type narrowing through structural subtyping.
Last updated