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.

Unbounded lists of results without a type annotation don't have enough type information

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
incetarikcommented, Sep 28, 2022

Bounty increased to $300 USD.

@supermacro Hello, could you please review the PR here?

0reactions
supermacrocommented, Oct 24, 2022

Update

This issue is resolved within v5.1.0

Read more comments on GitHub >

github_iconTop 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 >

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