Why one one of these snippets gives TSC error and other does not give? Both of them create same result. nameParts is an array of strings.
if(nameParts[nameParts.length-1]){
//error: Type 'string | undefined' is not assignable to type 'string'.
// Type 'undefined' is not assignable to type 'string'.
const lastName:string = nameParts[nameParts.length-1];
}
const lastWord = nameParts[nameParts.length-1];
if(lastWord){
const lastName:string = lastWord;
}