I am setting up some apis using typescript. I have a particular api that either returns an array of objects or a string.
interface SubmissionDetails {
branchName: string
clientAddress1: string
clientAddress2: string
clientCity: string
clientContactEmail: string
clientContactFullName: string
clientName: string
clientState: string
clientZip: string
id?: string
namedInsured: string
submissionID: string
underwriterDisplayName: string
}
getAllReferralsBySubmissionId: (
submissionId: string
) => Promise<SubmissionDetails[] | { message: string }>
Now I am taking the return value from getAllReferralsBySubmissionId
and trying to run some logic.
if (typeof referralsList === "object") {
referralsList?.forEach((item: SubmissionDetails) => {
item.id = item.submissionID
})
}
I am seeing a TypeScript error on the forEach
function stating:
Property 'forEach' does not exist on type 'SubmissionDetails[] | { message: string; }'.
Property 'forEach' does not exist on type '{ message: string; }'.
I understand that my function can return something that is not an array.
How can I update my function to remove this error?