The Data module simplifies creating and handling data structures in TypeScript. It provides tools for defining data types, ensuring equality between objects, and hashing data for efficient comparisons.
Value Equality
The Data module provides constructors for creating data types with built-in support for equality and hashing, eliminating the need for custom implementations.
This means that two values created using these constructors are considered equal if they have the same structure and values.
struct
In plain JavaScript, objects are considered equal only if they refer to the exact same instance.
Example (Comparing Two Objects in Plain JavaScript)
However, the Data.struct constructor allows you to compare values based on their structure and content.
Example (Creating and Checking Equality of Structs)
The comparison performed by Equal.equals is shallow, meaning nested objects are not compared recursively unless they are also created using Data.struct.
Example (Shallow Comparison with Nested Objects)
To ensure nested objects are compared by structure, use Data.struct for them as well.
Example (Correctly Comparing Nested Objects)
tuple
To represent your data using tuples, you can use the Data.tuple constructor. This ensures that your tuples can be compared structurally.
Example (Creating and Checking Equality of Tuples)
array
You can use Data.array to create an array-like data structure that supports structural equality.
Example (Creating and Checking Equality of Arrays)
Constructors
The module introduces a concept known as “Case classes”, which automate various essential operations when defining data types.
These operations include generating constructors, handling equality checks, and managing hashing.
Case classes can be defined in two primary ways:
as plain objects using case or tagged
as TypeScript classes using Class or TaggedClass
case
The Data.case helper generates constructors and built-in support for equality checks and hashing for your data type.
Example (Defining a Case Class and Checking Equality)
In this example, Data.case is used to create a constructor for Person. The resulting instances have built-in support for equality checks, allowing you to compare them directly using Equal.equals.
Example (Defining and Comparing Nested Case Classes)
This example demonstrates using Data.case to create nested data structures, such as a Person type containing an Address. Both Person and Address constructors support equality checks.
Alternatively, you can use Data.struct to create nested data structures without defining a separate Address constructor.
Example (Using Data.struct for Nested Objects)
tagged
When you’re working with a data type that includes a tag field, like in disjoint union types, defining the tag manually for each instance can get repetitive. Using the case approach requires you to specify the tag field every time, which can be cumbersome.
Example (Defining a Tagged Case Class Manually)
Here, we create a Person type with a _tag field using Data.case. Notice that the _tag needs to be specified for every new instance.
To streamline this process, the Data.tagged helper automatically adds the tag. It follows the convention in the Effect ecosystem of naming the tag field as "_tag".
Example (Using Data.tagged to Simplify Tagging)
The Data.tagged helper allows you to define the tag just once, making instance creation simpler.
Class
If you prefer working with classes instead of plain objects, you can use Data.Class as an alternative to Data.case. This approach may feel more natural in scenarios where you want a class-oriented structure, complete with methods and custom logic.
Example (Using Data.Class for a Class-Oriented Structure)
Here’s how to define a Person class using Data.Class:
One of the benefits of using classes is that you can easily add custom methods and getters. This allows you to extend the functionality of your data types.
Example (Adding Custom Getters to a Class)
In this example, we add a upperName getter to the Person class to return the name in uppercase:
TaggedClass
If you prefer a class-based approach but also want the benefits of tagging for disjoint unions, Data.TaggedClass can be a helpful option. It works similarly to tagged but is tailored for class definitions.
Example (Defining a Tagged Class with Built-In Tagging)
Here’s how to define a Person class using Data.TaggedClass. Notice that the tag "Person" is automatically added:
One benefit of using tagged classes is the ability to easily add custom methods and getters, extending the class’s functionality as needed.
Example (Adding Custom Getters to a Tagged Class)
In this example, we add a upperName getter to the Person class, which returns the name in uppercase:
Union of Tagged Structs
To create a disjoint union of tagged structs, you can use Data.TaggedEnum and Data.taggedEnum. These utilities make it straightforward to define and work with unions of plain objects.
Definition
The type passed to Data.TaggedEnum must be an object where the keys represent the tags,
and the values define the structure of the corresponding data types.
Example (Defining a Tagged Union and Checking Equality)
$is and $match
The Data.taggedEnum provides $is and $match functions for convenient type guarding and pattern matching.
Example (Using Type Guards and Pattern Matching)
Adding Generics
You can create more flexible and reusable tagged unions by using TaggedEnum.WithGenerics. This approach allows you to define tagged unions that can handle different types dynamically.
Example (Using Generics with TaggedEnum)
Errors
In Effect, handling errors is simplified using specialized constructors:
Error
TaggedError
These constructors make defining custom error types straightforward, while also providing useful integrations like equality checks and structured error handling.
Error
Data.Error lets you create an Error type with extra fields beyond the typical message property.
Example (Creating a Custom Error with Additional Fields)
You can yield an instance of NotFound directly in an Effect.gen, without needing to use Effect.fail.
Example (Yielding a Custom Error in Effect.gen)
TaggedError
Effect provides a TaggedError API to add a _tag field automatically to your custom errors. This simplifies error handling with APIs like Effect.catchTag or Effect.catchTags.
Native Cause Support
Errors created using Data.Error or Data.TaggedError can include a cause property, integrating with the native cause feature of JavaScript’s Error for more detailed error tracing.