by vikingcaffiene · 6y ago · 0 comments
https://en.m.wikipedia.org/wiki/Option_type
If you are using TypeScript (TFA mentions it), fp-ts provides a good functional programming layer. Although if you aren't already familiar with functional programming it's going to be hard as the documentation is rather lacking.
I would go even further that even if the semantics are a little clunky (wrap/unwrap) any programming language can implement Option.
As an aside, TypeScript is very unsafe by default, and still relatively unsafe with strict mode enabled. Stringent fp-ts usage bypasses these issues.
Is it? I have been using Typescript more and more these last few years, and lately haven't really experienced any issues where it's type system failed me.
To clarify, I'm not trying to argue against your point, genuinely curious to learn if fp-ts can really improve my code quality in production.
Only in Javascript though. In Typescript this would be clear from the fact that the type has the property.
Of course Typescript is not Javascript, but nobody should use plain JS anymore.
When I'm using hooks in React this is very common.
You must give an initial value, and at some point in the future you will update it.
By setting the value as null initially I can easily check whether the variable has been filled or not. This is most important for initially rendering the UI.
It would be better to just error out and not allow the uninitialized usage at all, but unfortunately that's not possible in Javascript.
For instance, if I'm fetching some data I usually want to initialize the data variable as null for a couple of reasons.
1) null is not truthy, empty arrays and objects are
2) It shows that the variable is supposed to be filled, unlike undefined