mailparser
mailparser copied to clipboard
Can't access email.to.text - Property 'text' does not exist on type 'AddressObject | AddressObject[]'
Hi, how can I get the text value of an email recipient?
I did it successfuly for a sender:
const email = await mailparser.simpleParser(stream);
const from = email.from?.text ?? "";
but when I try to do that for "to":
const toText = email.to?.text ?? "";
I get this error:
Property 'text' does not exist on type 'AddressObject | AddressObject[]'.
Property 'text' does not exist on type 'AddressObject[]'.
After displaying a whole email object (console.log(email)
) i can see that I should be able to access to
, just as from
:
. . .
to: {
value: [ [Object] ],
html: '<span class="mp_address_group"><a href="mailto:abc@localhost" class="mp_address_email">abc@localhost</a></span>',
text: 'abc@localhost'
},
from: {
value: [ [Object] ],
html: '<span class="mp_address_group"><span class="mp_address_name">Test</span> <<a href="mailto:[email protected]" class="mp_address_email">[email protected]</a>></span>',
text: 'Test <[email protected]>'
},
html: false
. . .
What might be the problem here? Am I using wrong keywords?
same probleme
The issue is that the types here for to
, cc
, and bcc
are incorrect. They should be AddressObject | undefined
.
AddressObject[]
type is is what is giving you the type error, because it is an array of AddressObjects
. To satisfy typescript, you have to handle both the case where to
could be AddressObject[]
or the case where to
could be AddressObject
, even though the former case should never occur. The relevant code for the address parsing is here and here.
Did we get any resolution on this?
I search solution too
Deal both cases where addressOjbect is array or string
like this
Array.isArray(parsed.to)
? parsed.to
.flatMap((addrObj) =>
addrObj.value.map((addr) => addr.address),
)
.join(', ')
: parsed.to?.value
.map((addr) => addr.address)
.join(', ') ?? '';