Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarification on how D.get works when key exists but value is undefined #75

Open
eLysgaard opened this issue Mar 3, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@eLysgaard
Copy link

eLysgaard commented Mar 3, 2023

I have some trouble using D.get and I'm unsure if this is a bug or if I'm misunderstanding the documentation.

Here's a minimal example to show the issue I'm having

const foo: {bar: string | undefined} = {bar: undefined};
pipe(
  foo,
  D.get('bar'),
  O.map(value => value.length) // The type of "value" is string, but its actual value is undefined so it throws
)

I can see the documentation says

Returns Some(value) if the given key exists, otherwise returns None.

In the above example the key does exist, and based on the return type of D.get I guess it becomes ExtractValue<string | undefined> which would end up as Exclude<string | undefined, null | undefined | void> resulting in string, which does not correspond to the actual value being passed to O.map

To resolve this I've gone ahead with the following solution.

const foo: {bar: string | undefined} = {bar: undefined};
pipe(
  foo,
  D.get('bar'),
  O.fromNullable, // This seems to properly wrap the undefined value from `foo.bar` in Option
  O.map(value => value.length) // The type of "value" is string, but its actual value is undefined so it throws
)
@mobily mobily added the bug Something isn't working label Mar 3, 2023
@mobily mobily self-assigned this Mar 3, 2023
@JUSTIVE
Copy link

JUSTIVE commented Mar 11, 2024

seems that it's option's problem, related to #97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants