I'm trying to understand how to write a function that would refine my type and then return it.
It works for the passed object itself and within the function body but not outside. Furthermore what I find odd is that even if it works within the body of the function, it stops working on the return statement itself.
My expectation would be that TypeScript correctly returns a refined / narrowed down type, checked properties included not just the main object.
What am I doing wrong? Does TypeScript require more help, maybe some generic return type?
Example:
type Referrer = { id: string }
type User = {
id: string;
referrer: Referrer | null;
} | null;
function checkUserHasReferrer(user: User) {
if (!user) {
throw Error('No User');
}
if (!user.referrer) {
console.log(user.referrer) // referrer correctly narrowed to `referrer: null`
throw Error('No Referrer');
}
const referrer = user.referrer; // referrer correctly narrowed to `referrer: Referrer`
console.log(user.referrer). // same here, `referrer: Referrer`
return user; // <- Here however it's back to `referrer: Referrer | null`
}
let initialUser: User = null;
const user = checkUserHasReferrer(initialUser);
console.log(user.id); // <-- knows User is not `null`
console.log(user.referrer.id); // However it doesn't know referrer is not `null`