Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access email.to.text - Property 'text' does not exist on type 'AddressObject | AddressObject[]' #320

Open
ufo512 opened this issue Oct 18, 2022 · 7 comments

Comments

@ufo512
Copy link

ufo512 commented Oct 18, 2022

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> &lt;<a href="mailto:test@example.com" class="mp_address_email">test@example.com</a>&gt;</span>',
    text: 'Test <test@example.com>'
  },
  html: false
. . .

What might be the problem here? Am I using wrong keywords?

@vAugagneur
Copy link

same probleme

@liesislukas
Copy link

image

image

@jekozyra
Copy link

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.

@DreAmigo
Copy link

Did we get any resolution on this?

@hlamber
Copy link

hlamber commented Mar 29, 2024

I search solution too

@dev-ABsid
Copy link

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(', ') ?? '';

@zixiang2018
Copy link

Hi, are there any other resolution to this?

I see that a PR was merged long ago which added AddressObject[] to cc, to and bcc due to a possible input like from this example.

However, I am not sure if this is actually a valid input where there are 3 "To"s, as per that example. My input has always given me a single AddressObject.

May I check if @andris9 has any inputs on this?

Reference: DefinitelyTyped/DefinitelyTyped#50744 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants