This is similar to #40796374 but that is around types, while I am using interfaces.
Given the code below:
interface Foo {
name: string;
}
function go() {
let instance: Foo | null = null;
let mutator = () => {
instance = {
name: 'string'
};
};
mutator();
if (instance == null) {
console.log('Instance is null or undefined');
} else {
console.log(instance.name);
}
}
I have an error saying 'Property 'name' does not exist on type 'never'.
I don't understand how instance could ever be a 'never'. Can anyone shed some light on this?
else
would indeed never get evaluated. The compiler is smart enough to see it.It's pretty clear from your code that the else would indeed never get evaluated.
It isn't obvious at all nor there is a smart compiler. There is a dumb transpiler which deviates from the established practices. In C#, for instance, one can assignnull
to any object...