DEV Community

Discussion on: Maybe just Nullable?

Collapse
 
lucasbernalte profile image
Lucas Bernalte

This is a great article, thanks for sharing your thoughts. When switching to TypeScript this is one of the first things I thought: should I create a Maybe implementation right at the very beginning of the transition? I think I will stick with Nullable as this is already represented in TS, but I am still curious of how things can escalate badly when implementing the Maybe in a TS project.

I also like the idea of a chainNullable in order to compose. The "if not null" check still bothers me a little :)

Collapse
 
macsikora profile image
Pragmatic Maciej

Hey Lucas, tnx for comment. So you can implement Maybe, use it, nothing bad will happen outside of the fact that null will not disappear, and you need to watch out your back, because in any third party lib, the null is still some kind of standard, not surprisingly though. What does it mean is that you will need to protect your project from using any null things, including newest staff like nullish coalescing operator and optional chaining, also you will need to replace standard JS API like Array into some wrapper, as standard Array uses null | undefined. Another subject is communication with server, JSON will send you null fields, those need to be parsed and changed to Maybe, allowing any null to go through your code means that finally you have Nothing | null | undefined as extra-nullish :D.

So to sum it up, you can, but it demands a lot of project rules, and avoiding part of the language syntax and part of the language build interfaces. There is no really going back from that I would say.