I am facing an issue with an interface property that can be null. I need to ensure it's not null before passing the object into a typesafe object.
While narrowing works when assigning the property to a variable, it seems to fail when trying to use the object as a whole.
Using casting is not an option as it goes against design time type checking principles.
interface Person
{
middle:string | null
}
interface MiddleNamePerson
{
middle:string
}
function DoWork(person:Person) {
if(person.middle)
{
const middleName:string = person.middle; // works
const middle : MiddleNamePerson = person // Error: Type of 'Person' not Assignable to 'MiddleNamePerson'
DoStuff(person) // Error: the argument of 'Person' is not Assignable to parameter
}
}
function DoStuff(value:{middle:string}) {}