Question about Option type guards
See original GitHub issueHi 👋
A belt port in TS is something I’ve already dreamed of, so thank you!
I tried adding it to our codebase, but think it might have an issue with the Option
type guards (isSome
/ isNone
)
On usage with ts-pattern
(for pattern maching):
import { A, O } from "@mobily/ts-belt";
import { match, when } from "ts-pattern";
const array = A.make(Math.floor(Math.random() * 10), "foo");
match(A.get(array, 5))
.with(when(O.isSome), value => { /* value is O.Option<string>, it should be string */ })
.with(when(O.isNone), value => { /* value is O.Option<never>, it should be never */ })
.exhaustive();
I checked the types of the given function and it confirms the issue:
/** Returns `true` if the provided `option` is `Some(value)`, otherwise, returns `false`. */
export declare function isSome<T>(option: Option<T>): option is Option<T>;
/** Returns `true` if the provided `option` is `None`, otherwise, returns `false`. */
export declare function isNone<T>(option: Option<T>): option is Option<never>;
Shouldn’t it returns option is T
and option is never
instead?
Same example with Rescript (v
is int
, not option(int)
):
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
A few questions about type guards : r/typescript - Reddit
2. Type guards have to be callable at runtime as well. And at run time, you have no type information at all. So...
Read more >How to use type guards in TypeScript - LogRocket Blog
A type guard is a TypeScript technique used to get information about the type of a variable, usually within a conditional block.
Read more >type guards for optional parameters - Stack Overflow
type guards for optional parameters · 2 ? · 1. Does this approach meet your needs? · 1. that code btw allows name:...
Read more >Type guards - Learn fp-ts - GitBook
Type guards. fp-ts has a neat way to deal with type guards, using Option#getRefinements. The problem stem from typescript type safety.
Read more >How to get the types you want with TypeScript type guards
Has TypeScript ever prevented you from accessing a property you KNOW exists on a variable? Read on and hear all about type guards....
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
@zoontek the
Option
type is nowtype Option<T> = T | undefined | null
, this change has been published inv3.8.0
🚀 thanks for all your help and valuable suggestions, I really appreciate that! ❤️I will check a bit longer to see if I can create
ts-pattern
guard, I don’t want to lose pattern matching (I think you understand that 😄) I close this issue, will publish a comment if I find something.