jackbearheart / email-addresses

An RFC 5322 email address parser
MIT License
249 stars 36 forks source link

bug fix: added domain as part of the ParsedGroup return type #51

Open aymather opened 4 years ago

aymather commented 4 years ago

I'd like to suggest adding the domain to the ParsedGroup return type because this basic example isn't working as expected.

import { parseOneAddress } from 'email-addresses'

let email: string = 'aymather@gmail.com'
const parsed = parseOneAddress(email)
console.log(parsed.domain)

It is telling me:

Property 'domain' does not exist on type 'ParsedMailbox | ParsedGroup'.
  Property 'domain' does not exist on type 'ParsedGroup'.ts(2339)
jackbearheart commented 4 years ago

It's true that a "group" does not have a "domain" associated with it. I don't think adding a property that doesn't exist will be good for TS users who want accurate types. However, I see that this is inconvenient because the return types of all the functions include ParsedMailbox | ParsedGroup and then you have to handle the case of there being a group. I discussed this a bit here: https://github.com/jackbearheart/email-addresses/issues/41#issuecomment-437234965

I think the real solution is creating a parseOneMailbox function with the return type ParsedMailbox | null, because this is a very common use case.