Type Narrowing With Similarly Shaped Objects
type User = {
firstName: string
lastName: string
age: number
email: string
}
const liz: User = {
firstName: 'Liz',
lastName: 'Lemon',
age: 38,
email: 'liz@example.com'
}const sendNewsletter = ({
firstName,
email,
}: {
firstName: string;
email: string;
}) => {
console.log(`Sending newsletter to ${firstName} at ${email}`);
};
sendNewsletter(liz);
// "Sending newsletter to Liz at liz@example.com"Last updated