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.

Combine types not inferred properly for homogenous ResultAsync list

See original GitHub issue

I have code similar to the following:

result.andThen(value => {
  // ...
  return combine([
    ResultAsync.fromPromise(
      getBalance(senderAddress), // returns Promise<BigNumber>
      (err) => new Error(...),
    ),
    ResultAsync.fromPromise(
      getBalance(receiverAddress), // returns Promise<BigNumber>
      (err) => new Error(...),
    ),
  ]);
}).andThen(([senderBalance, receiverBalance]) => {
  // senderBalance and receiverBalance should both be typed as BigNumber
});

When I try this code, I get type errors:

Argument of type '([senderBalance, receiverBalance]: [any, any]) => ResultAsync<unknown, HandlerError> | ResultAsync<undefined, unknown>' is not assignable to parameter of type '(t: unknown) => Result<unknown, unknown> | ResultAsync<unknown, unknown>'.
  Types of parameters '__0' and 't' are incompatible.
    Type 'unknown' is not assignable to type '[any, any]'.ts(2345)

Am I doing something wrong? If I change the last andThen to the following it works:

.andThen((balances) => {
  const [senderBalance, receiverBalance] = balances as BigNumber[];
  // ...
});

I don’t believe I should have to cast it since the combine should give me the proper result with an array of values.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
supermacrocommented, Jul 22, 2021

This Comment Is Kind Of Irrelevant. Safe To Skip To Next Comment.

The following comment is regarding heterogeneous lists and I later realized that we were talking about homogeneous lists. Keeping this here for context anyways.


This is a known issue which I have not had the time to address … #226 provides some (somewhat outdated) context on the problem. This comment summarizes the discussion.

I am open to addressing the issue (given I have time, or someone else is able to address this sooner than me) under a MAJOR change for the package.


Anyways, the current way that I work around this “limitation” is to declare lists as const so as to turn them into tuples.

  return combine([
    ResultAsync.fromPromise(
      getBalance(senderAddress), // returns Promise<BigNumber>
      (err) => new Error(...),
    ),
    ResultAsync.fromPromise(
      getBalance(receiverAddress), // returns Promise<BigNumber>
      (err) => new Error(...),
    ),
  ] as const);

Here is another example:

  return combine([
    functionThatReturnsOneResultAsync(arg1),
    functionThatReturnsDifferentResultAsync(arg2),
  ] as const)

Info on “const assertions”:

https://mariusschulz.com/blog/const-assertions-in-literal-expressions-in-typescript

0reactions
supermacrocommented, Dec 18, 2021

Closing because of lack of response from @rhlsthrm, feel free to reopen though!

Read more comments on GitHub >

github_iconTop Results From Across the Web

supermacro/neverthrow: Type-Safe Errors for JS & TypeScript
combine works on both heterogeneous and homogeneous lists. This means that you can have lists that contain different kinds of ResultAsync s and...
Read more >
typescript - Combine type guard signatures - Stack Overflow
I've used the UnionToIntersection construct shown here but cannot correctly type the And function, here's my attempt and the error I'm getting:
Read more >
Use Intersection Types to Combine Types in TypeScript
TypeScript allows us to not only create individual types, but combine them to create more powerful use cases and completeness.
Read more >
Handbook - Unions and Intersection Types - TypeScript
An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has...
Read more >
Advanced Types - TypeScript - JavaScript that scales.
You can combine singleton types, union types, type guards, and type aliases to build an advanced pattern called discriminated unions, also known as...
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