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.

Return union type without type annotating `match`

See original GitHub issue

The following compiles:

import { match } from "ts-pattern";

type T = string | number;

const x = match<boolean, T>(true)
  .with(true, () => "")
  .with(false, () => 0)
  .run();

Would it be possible to construct a union type along the way in the builder? I mean, so that the above code would compile without the type annotations?

I’m not sure if that’s wanted in every scenario, so maybe it would need a second version of match for it. In any way, if possible, I’d say this would be a valuable thing to have.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
gvergnaudcommented, Mar 25, 2021

v3.1.1 has been released! I’m closing this issue

1reaction
m-ruttercommented, Mar 24, 2021

Seems to work great! I can still restrict the return type by explicitly setting the generic params, but will otherwise correctly infer the union type.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typing — Support for type hints — Python 3.11.1 documentation
The most fundamental support consists of the types Any, Union, Callable, ... The Python runtime does not enforce function and variable type annotations....
Read more >
Handbook - Unions and Intersection Types - TypeScript
A union type describes a value that can be one of several types. We use the vertical bar ( | ) to separate...
Read more >
Check a variable against Union type at runtime in Python 3.6
I tried to implement the check by myself but while bark.__annotations__['descr'] is shown as typing.Union[int, str] in the REPL I can't access ...
Read more >
Kinds of types - mypy 0.991 documentation
Union types ​​ For example, if an argument has type Union[int, str] , both integers and strings are valid argument values. Operations are...
Read more >
Union Types - Scala 3 - EPFL
The more precise union type is also inferred if UserName and Password are declared without an explicit parent, since in that case their...
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