Combine types not inferred properly for homogenous ResultAsync list
See original GitHub issueI 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:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This Comment Is Kind Of Irrelevant. Safe To Skip To Next Comment.
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.Here is another example:
Info on “const assertions”:
https://mariusschulz.com/blog/const-assertions-in-literal-expressions-in-typescript
Closing because of lack of response from @rhlsthrm, feel free to reopen though!