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.

Question about Option type guards

See original GitHub issue

Hi 👋

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)):

https://rescript-lang.org/try?code=DYUwLgBAtgngcgV2MAhgI1ANRcBIIC8EAygPZQgAUArAJQBQ9AzgO4CWYAxgBbTxKoMIbLnwBvegB8S5KgDdahAHwQAUkwB0wUgHMATJQBEcnHghsmEAA4AnEExAA7MIYA0EBVIhxSj-ARV1LV0jE1FzS3QHZ0MGAF96IA

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mobilycommented, Jan 2, 2022

@zoontek the Option type is now type Option<T> = T | undefined | null, this change has been published in v3.8.0 🚀 thanks for all your help and valuable suggestions, I really appreciate that! ❤️

1reaction
zoontekcommented, Dec 13, 2021

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.

Read more comments on GitHub >

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

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