question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using multiple extra accessors causes type errors arising from union types

See original GitHub issue

Describe the bug 🐛

In TypeScript, if I attempt to use two extra accessors with different return types, the types for the computed values seem to be a union over the return types of all of the extra accessors, rather than just the return type of the accessor in use.

(Related: those types also include undefined, even in cases where the value is required, which seems wrong — but that’s a separate issue I guess.)

To Reproduce 📝

Here we use the asEmail example given in README.md, and we add another extra accessor called asZero which always returns zero. So the type for asEmail is string, and the type for asZero is number:

process.env.ADMIN = 'admin@example.com'
 
const asEmail: ExtensionFn<string> = (value) => {
  const split = String(value).split('@')
  if (split.length !== 2) {
    throw new Error('must contain exactly one "@"')
  }
  return value
}

const asZero: ExtensionFn<number> = (value) => 0;

const env = from(process.env, {asEmail, asZero})

Then the type required for a value using either of these extra accessors is number | string | undefined, so I have to write the following:

const validEmail: number | string | undefined = env.get('ADMIN').asEmail()
const zero: number | string | undefined = env.get('ADMIN').asZero()

Expected behaviour 🤷‍♂️🤷

I would expect this to work / not fail type checking:

const validEmail: string | undefined = env.get('ADMIN').asEmail()
const zero: number | undefined = env.get('ADMIN').asZero()

(In fact, as mentioned above, I’d love to not have to include undefined here; AFAICS asEmail always either returns a string or raises an error, so why not…

const validEmail: string = env.get('ADMIN').asEmail()
const zero: number = env.get('ADMIN').asZero()

… but again, that’s secondary to the issue I really want to raise, which is the union thing.)

Environment (please complete the following information) 💻:

  • OS: macOS Mojave 10.14.6
  • Runtime: Chrome 79.0.3945.130
  • Version 6.0.4

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
evanshortisscommented, Apr 14, 2020

@gimbo I’ve fixed the undefined now also, thank you!

1reaction
gimbocommented, Apr 14, 2020

@evanshortiss Hi Evan - didn’t get chance to look at this over the weekend but I’ve just just checked and it does indeed seem to be fixed for my use case. The undefineds are still there but the number/string clash is gone. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Unions and Intersection Types
This method requires you to define an extra function, but it's much more obvious when you forget it because the error message includes...
Read more >
Typescript: Interfaces in Union Types Cause Errors with ...
i am struggling with similar issues. I am looking at declaring interfaces that have fields that are a union of a primitive type...
Read more >
Union type of multiple interfaces must treat properties that ...
Union type of multiple interfaces must treat properties that exists only in some interfaces as optional ( | undefined ), not errors #50495....
Read more >
TypeScript errors and how to fix them
error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead. Broken Code ❌. 1...
Read more >
XSL Transformations (XSLT) Version 3.0
Please use multiple Bugzilla entries (or, if necessary, multiple email ... an argument whose declared type is a union type with member types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found