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

Why an Option type instead of type | undefined? #87

Open
Visual-Dawg opened this issue Sep 20, 2023 · 4 comments
Open

Why an Option type instead of type | undefined? #87

Visual-Dawg opened this issue Sep 20, 2023 · 4 comments

Comments

@Visual-Dawg
Copy link

Hey, this is a question instead of a real issue, but the discussion tab isnt present.

So I am wondering why you use an Option-Type instead of simply using a union type of <provided type> | undefined?
Because with the specific type you always need to wrap the value first instead of just using the functions.

I am not saying that one is better than the other, but simply curious :D

@Visual-Dawg
Copy link
Author

I looked more into it, as undefined can be a pain so often and found this article.

For v4, would it make sense to convert all Option functions to take value | undefined?
This would make it so much easier to work with it

@redbar0n
Copy link

probably because Option handles null as well?

@Visual-Dawg
Copy link
Author

Then you can just expand the union to x | undefined | null.
I asked this question, as the option type has no syntax support.

@JUSTIVE
Copy link

JUSTIVE commented Mar 4, 2024

It abstracts the absence of a given type T. You could think of it as just an alias for type representation in raw typescript: T|null|undefined, but there's little more about it.
An option type(and the functions in the option module) does is lift functions that work with a given type. If there's a function that takes A and then returns B(A->B), O.map will lift this function O<A> -> O<B>. Those nulls and undefineds beneath the option type are just implementations of the idea of absence.
Of course, just judging by its implementation, option<T> seems too naive, and meaningless, but the same goes for other types, like result. After all, all those types will be represented in js/ts, a structural type system. They exists because not they encapsulate complex things, it represents mathematic concepts.

BTW, I understand there are some issues with functions that take options as arguments are easy to cause an error, since the type signature says it takes option type(which should be T| null | undefined), but only works properly when it takes undefined(internal representation of true None).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants