Return union type without type annotating `match`
See original GitHub issueThe 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:
- Created 3 years ago
- Comments:13 (10 by maintainers)
Top 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 >
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 Free
Top 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
v3.1.1 has been released! I’m closing this issue
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.