Unbounded lists of results without a type annotation don't have enough type information
See original GitHub issueThis is next proposition to solve problem with #224.
I feel I learned a lot about TypeScript in last week 😉 Today I tried again to provide proper types for Heterogeneous combine
function.
First, I would like to provide my new proposition, implementation of combine
types, based on our findings in #224 and my experimetns.
import { Result, Ok, Err } from 'neverthrow';
declare function combine<T extends readonly Result<unknown, unknown>[]>(
results: [...T]
): Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number]>;
// Given a list of Results, this extracts all the different `T` types from that list
type ExtractOkTypes<T extends readonly Result<unknown, unknown>[]> = {
[Key in keyof T]: T[Key] extends Result<unknown, unknown>
? ExtractOkFromUnion<T[Key]>
: never;
};
// Given a list of Results, this extracts all the different `E` types from that list
type ExtractErrTypes<T extends readonly Result<unknown, unknown>[]> = {
[Key in keyof T]: T[Key] extends Result<unknown, unknown>
? ExtractErrFromUnion<T[Key]>
: never;
};
// need to be separated generic type to run it for every element of union T separately
type ExtractOkFromUnion<T extends Result<unknown, unknown>> = T extends Ok<
infer V,
unknown
> // filter out "unknown" values
? V extends {}
? V
: never
: never;
// need to be separated generic type to run it for every element of union T separately
type ExtractErrFromUnion<T extends Result<unknown, unknown>> = T extends Err<
unknown,
infer E
> // filter out "unknown" values
? E extends {}
? E
: never
: never;
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (9 by maintainers)
Top Results From Across the Web
Nullness: Annotating wildcards and their bounds · Issue #31 - GitHub
It seems that we might need to allow annotating an unbounded wildcard Foo<@NotNull ... One may want to say something like "write-only list...
Read more >How to annotate function that takes a tuple of variable length ...
If we assume that List[str] is okay for variable length lists, then why Tuple[str] is not okay for variable length tuple? And (type(("a",...
Read more >1. Typechecking - Hack and HHVM [Book] - O'Reilly
It allows you to pass around class names that aren't statically known, but in such a way that the typechecker has enough information...
Read more >PEP 484 – Type Hints - Python Enhancement Proposals
Any function without annotations should be treated as having the most general type possible, or ignored, by any type checker. Functions with the...
Read more >Type Errors - Pyre
4: Missing Attribute Annotation. In strict mode, Pyre will error when an attribute does not have an annotation. class A ...
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
@supermacro Hello, could you please review the PR here?
Update
This issue is resolved within v5.1.0