Safer JavaScript with the Maybe Type
JavaScript’s dynamic typing can lead to runtime errors and bugs. With the Maybe type, we can encapsulate type checking and guard against missing values, making our code safer and easier to read. Learn how to create and unwrap Maybes, safely access object properties, and compose functions for reusability in this course.
JavaScript’s dynamic typing makes it incredibly flexible. That flexibility can lead to trouble though. When values have the potential to change types or to end up as
or
, that can lead to runtime errors in our code or bizarre bugs that take forever to track down because of type coercion. To battle this, we end up with code that is littered will conditionals for
or
values and type checks, making the core logic harder to read and refactor later.
The
Maybe
encapsulates the type checking and guards against missing values for us. With
Maybe
in our toolbelt, we can keep our functions free of all the guardrails, outsource that work to the
Maybe
and keep our business logic free of all the clutter.
Course Content
Course Introduction: Safer JavaScript with the Maybe type
Understand the Maybe Data Type
Create a Maybe with a `safe` Utility Function
Unwrap Values from a Maybe
Safely Access Object Properties with `prop`
Safely Access Nested Object Properties with `propPath`
Flatten Nested Maybes with `chain`
Recover from a Nothing with the `alt` method
Recover from a Nothing with the `coalesce` Method
Compose Functions for Reusability with the Maybe Type
Apply a function in a Maybe context to Maybe inputs
Make your own functions safer by lifting them into a Maybe context