Consider the following TypeScript interface:
interface MyInputInterface {
a: A | null
b: B | null
aString: string | null
}
Currently, we have this function:
const hasOneNonNull = (input: MyInputInterface) =>
input.a !== null || input.b !== null || input.aString !== null
However, this method feels fragile. It requires manual updating every time a new member is added to the interface. Is there a way to dynamically iterate through all interface members and check if at least one of them is non-null?
An ideal solution would look like this (getAllMembers
is pseudo code):
const hasOneNonNull = (input: MyInputInterface) =>
input.getAllMembers().find((elem: any) => elem !== null) !== null