I have the following code:
private extractInitials(fullname: string): string {
const initials = fullname
.replace(/[^a-zA-Z- ]/g, '')
.match(/\b\w/g)
.join('')
.toUpperCase();
return initials.substring(0, 2);
}
[ts] Object is possibly 'null'. [2531]
So I tried
if fullname { const initials .... return ... } else return '';
Turns out typescript was complaining about this guy
fullname.replace(/[^a-zA-Z- ]/g, '')
Which makes sense because this might end up being an empty string
So I did
const t = fullname.replace(/[^a-zA-Z- ]/g, '')
if(t) { /* do the rest */ } else return ''
and it still gave me the object is possibly null error. I know it's not. How do I fix?