Skip to content

Data

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.

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.

In plain JavaScript, objects are considered equal only if they refer to the exact same instance.

Example (Comparing Two Objects in Plain JavaScript)

1
const
const alice: { name: string; age: number; }
alice
= {
(property) name: string
name
: "Alice",
(property) age: number
age
: 30 }
2
3
// This comparison is false because they are different instances
4
// @ts-expect-error
5
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const alice: { name: string; age: number; }
alice
=== {
(property) name: string
name
: "Alice",
(property) age: number
age
: 30 }) // Output: false

However, the Data.struct constructor allows you to compare values based on their structure and content.

Example (Creating and Checking Equality of Structs)

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
// ┌─── { readonly name: string; readonly age: number; }
4
// ▼
5
const
const alice: { readonly name: string; readonly age: number; }
alice
=
import Data
Data
.
const struct: <{ name: string; age: number; }>(a: { name: string; age: number; }) => { readonly name: string; readonly age: number; }
struct
({
(property) name: string
name
: "Alice",
(property) age: number
age
: 30 })
6
7
// Check if Alice is equal to a new object
8
// with the same structure and values
9
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<{ readonly name: string; readonly age: number; }, { readonly name: string; readonly age: number; }>(self: { readonly name: string; readonly age: number; }, that: { readonly name: string; readonly age: number; }): boolean (+1 overload)
equals
(
const alice: { readonly name: string; readonly age: number; }
alice
,
import Data
Data
.
const struct: <{ name: string; age: number; }>(a: { name: string; age: number; }) => { readonly name: string; readonly age: number; }
struct
({
(property) name: string
name
: "Alice",
(property) age: number
age
: 30 })))
10
// Output: true
11
12
// Check if Alice is equal to a plain JavaScript object
13
// with the same content
14
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<{ readonly name: string; readonly age: number; }, { name: string; age: number; }>(self: { readonly name: string; readonly age: number; }, that: { name: string; age: number; }): boolean (+1 overload)
equals
(
const alice: { readonly name: string; readonly age: number; }
alice
, {
(property) name: string
name
: "Alice",
(property) age: number
age
: 30 }))
15
// Output: false

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)

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
const
const nested: { readonly name: string; readonly nested_field: { value: number; }; }
nested
=
import Data
Data
.
const struct: <{ name: string; nested_field: { value: number; }; }>(a: { name: string; nested_field: { value: number; }; }) => { readonly name: string; readonly nested_field: { value: number; }; }
struct
({
(property) name: string
name
: "Alice",
(property) nested_field: { value: number; }
nested_field
: {
(property) value: number
value
: 42 } })
4
5
// This will be false because the nested objects are compared by reference
6
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
7
import Equal
Equal
.
function equals<{ readonly name: string; readonly nested_field: { value: number; }; }, { readonly name: string; readonly nested_field: { value: number; }; }>(self: { readonly name: string; readonly nested_field: { value: number; }; }, that: { ...; }): boolean (+1 overload)
equals
(
8
const nested: { readonly name: string; readonly nested_field: { value: number; }; }
nested
,
9
import Data
Data
.
const struct: <{ name: string; nested_field: { value: number; }; }>(a: { name: string; nested_field: { value: number; }; }) => { readonly name: string; readonly nested_field: { value: number; }; }
struct
({
(property) name: string
name
: "Alice",
(property) nested_field: { value: number; }
nested_field
: {
(property) value: number
value
: 42 } })
10
)
11
)
12
// Output: false

To ensure nested objects are compared by structure, use Data.struct for them as well.

Example (Correctly Comparing Nested Objects)

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
const
const nested: { readonly name: string; readonly nested_field: { readonly value: number; }; }
nested
=
import Data
Data
.
const struct: <{ name: string; nested_field: { readonly value: number; }; }>(a: { name: string; nested_field: { readonly value: number; }; }) => { readonly name: string; readonly nested_field: { readonly value: number; }; }
struct
({
4
(property) name: string
name
: "Alice",
5
(property) nested_field: { readonly value: number; }
nested_field
:
import Data
Data
.
const struct: <{ value: number; }>(a: { value: number; }) => { readonly value: number; }
struct
({
(property) value: number
value
: 42 })
6
})
7
8
// Now, the comparison returns true
9
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
10
import Equal
Equal
.
function equals<{ readonly name: string; readonly nested_field: { readonly value: number; }; }, { readonly name: string; readonly nested_field: { readonly value: number; }; }>(self: { readonly name: string; readonly nested_field: { readonly value: number; }; }, that: { ...; }): boolean (+1 overload)
equals
(
11
const nested: { readonly name: string; readonly nested_field: { readonly value: number; }; }
nested
,
12
import Data
Data
.
const struct: <{ name: string; nested_field: { readonly value: number; }; }>(a: { name: string; nested_field: { readonly value: number; }; }) => { readonly name: string; readonly nested_field: { readonly value: number; }; }
struct
({
13
(property) name: string
name
: "Alice",
14
(property) nested_field: { readonly value: number; }
nested_field
:
import Data
Data
.
const struct: <{ value: number; }>(a: { value: number; }) => { readonly value: number; }
struct
({
(property) value: number
value
: 42 })
15
})
16
)
17
)
18
// Output: true

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)

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
// ┌─── readonly [string, number]
4
// ▼
5
const
const alice: readonly [string, number]
alice
=
import Data
Data
.
const tuple: <[string, number]>(as_0: string, as_1: number) => readonly [string, number]
tuple
("Alice", 30)
6
7
// Check if Alice is equal to a new tuple
8
// with the same structure and values
9
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<readonly [string, number], readonly [string, number]>(self: readonly [string, number], that: readonly [string, number]): boolean (+1 overload)
equals
(
const alice: readonly [string, number]
alice
,
import Data
Data
.
const tuple: <[string, number]>(as_0: string, as_1: number) => readonly [string, number]
tuple
("Alice", 30)))
10
// Output: true
11
12
// Check if Alice is equal to a plain JavaScript tuple
13
// with the same content
14
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<readonly [string, number], (string | number)[]>(self: readonly [string, number], that: (string | number)[]): boolean (+1 overload)
equals
(
const alice: readonly [string, number]
alice
, ["Alice", 30]))
15
// Output: false

You can use Data.array to create an array-like data structure that supports structural equality.

Example (Creating and Checking Equality of Arrays)

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
// ┌─── readonly number[]
4
// ▼
5
const
const numbers: readonly number[]
numbers
=
import Data
Data
.
const array: <number[]>(as: number[]) => readonly number[]
array
([1, 2, 3, 4, 5])
6
7
// Check if the array is equal to a new array
8
// with the same values
9
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<readonly number[], readonly number[]>(self: readonly number[], that: readonly number[]): boolean (+1 overload)
equals
(
const numbers: readonly number[]
numbers
,
import Data
Data
.
const array: <number[]>(as: number[]) => readonly number[]
array
([1, 2, 3, 4, 5])))
10
// Output: true
11
12
// Check if the array is equal to a plain JavaScript array
13
// with the same content
14
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<readonly number[], number[]>(self: readonly number[], that: number[]): boolean (+1 overload)
equals
(
const numbers: readonly number[]
numbers
, [1, 2, 3, 4, 5]))
15
// Output: false

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

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.

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
interface
interface Person
Person
{
4
readonly
(property) Person.name: string
name
: string
5
}
6
7
// Create a constructor for `Person`
8
//
9
// ┌─── (args: { readonly name: string; }) => Person
10
// ▼
11
const
const make: Data.Case.Constructor<Person, never>
make
=
import Data
Data
.
(alias) case<Person>(): Data.Case.Constructor<Person, never> export case

Provides a constructor for the specified `Case`.

case
<
interface Person
Person
>()
12
13
const
const alice: Person
alice
=
const make: Data.Case.Constructor (args: { readonly name: string; }) => Person
make
({
(property) name: string
name
: "Alice" })
14
15
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals
(
const alice: Person
alice
,
const make: Data.Case.Constructor (args: { readonly name: string; }) => Person
make
({
(property) name: string
name
: "Alice" })))
16
// Output: true
17
18
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals
(
const alice: Person
alice
,
const make: Data.Case.Constructor (args: { readonly name: string; }) => Person
make
({
(property) name: string
name
: "John" })))
19
// Output: false

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.

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
interface
interface Address
Address
{
4
readonly
(property) Address.street: string
street
: string
5
readonly
(property) Address.city: string
city
: string
6
}
7
8
// Create a constructor for `Address`
9
const
const Address: Data.Case.Constructor<Address, never>
Address
=
import Data
Data
.
(alias) case<Address>(): Data.Case.Constructor<Address, never> export case

Provides a constructor for the specified `Case`.

case
<
interface Address
Address
>()
10
11
interface
interface Person
Person
{
12
readonly
(property) Person.name: string
name
: string
13
readonly
(property) Person.address: Address
address
:
interface Address
Address
14
}
15
16
// Create a constructor for `Person`
17
const
const Person: Data.Case.Constructor<Person, never>
Person
=
import Data
Data
.
(alias) case<Person>(): Data.Case.Constructor<Person, never> export case

Provides a constructor for the specified `Case`.

case
<
interface Person
Person
>()
18
19
const
const alice: Person
alice
=
const Person: Data.Case.Constructor (args: { readonly name: string; readonly address: Address; }) => Person
Person
({
20
(property) name: string
name
: "Alice",
21
(property) address: Address
address
:
const Address: Data.Case.Constructor (args: { readonly street: string; readonly city: string; }) => Address
Address
({
(property) street: string
street
: "123 Main St",
(property) city: string
city
: "Wonderland" })
22
})
23
24
const
const anotherAlice: Person
anotherAlice
=
const Person: Data.Case.Constructor (args: { readonly name: string; readonly address: Address; }) => Person
Person
({
25
(property) name: string
name
: "Alice",
26
(property) address: Address
address
:
const Address: Data.Case.Constructor (args: { readonly street: string; readonly city: string; }) => Address
Address
({
(property) street: string
street
: "123 Main St",
(property) city: string
city
: "Wonderland" })
27
})
28
29
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals
(
const alice: Person
alice
,
const anotherAlice: Person
anotherAlice
))
30
// Output: true

Alternatively, you can use Data.struct to create nested data structures without defining a separate Address constructor.

Example (Using Data.struct for Nested Objects)

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
interface
interface Person
Person
{
4
readonly
(property) Person.name: string
name
: string
5
readonly
(property) Person.address: { readonly street: string; readonly city: string; }
address
: {
6
readonly
(property) street: string
street
: string
7
readonly
(property) city: string
city
: string
8
}
9
}
10
11
const
const Person: Data.Case.Constructor<Person, never>
Person
=
import Data
Data
.
(alias) case<Person>(): Data.Case.Constructor<Person, never> export case

Provides a constructor for the specified `Case`.

case
<
interface Person
Person
>()
12
13
const
const alice: Person
alice
=
const Person: Data.Case.Constructor (args: { readonly name: string; readonly address: { readonly street: string; readonly city: string; }; }) => Person
Person
({
14
(property) name: string
name
: "Alice",
15
(property) address: { readonly street: string; readonly city: string; }
address
:
import Data
Data
.
const struct: <{ street: string; city: string; }>(a: { street: string; city: string; }) => { readonly street: string; readonly city: string; }
struct
({
(property) street: string
street
: "123 Main St",
(property) city: string
city
: "Wonderland" })
16
})
17
18
const
const anotherAlice: Person
anotherAlice
=
const Person: Data.Case.Constructor (args: { readonly name: string; readonly address: { readonly street: string; readonly city: string; }; }) => Person
Person
({
19
(property) name: string
name
: "Alice",
20
(property) address: { readonly street: string; readonly city: string; }
address
:
import Data
Data
.
const struct: <{ street: string; city: string; }>(a: { street: string; city: string; }) => { readonly street: string; readonly city: string; }
struct
({
(property) street: string
street
: "123 Main St",
(property) city: string
city
: "Wonderland" })
21
})
22
23
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals
(
const alice: Person
alice
,
const anotherAlice: Person
anotherAlice
))
24
// Output: true

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.

1
import {
import Data
Data
} from "effect"
2
3
interface
interface Person
Person
{
4
readonly
(property) Person._tag: "Person"
_tag
: "Person" // the tag
5
readonly
(property) Person.name: string
name
: string
6
}
7
8
const
const Person: Data.Case.Constructor<Person, never>
Person
=
import Data
Data
.
(alias) case<Person>(): Data.Case.Constructor<Person, never> export case

Provides a constructor for the specified `Case`.

case
<
interface Person
Person
>()
9
10
// Repeating `_tag: 'Person'` for each instance
11
const
const alice: Person
alice
=
const Person: Data.Case.Constructor (args: { readonly _tag: "Person"; readonly name: string; }) => Person
Person
({
(property) _tag: "Person"
_tag
: "Person",
(property) name: string
name
: "Alice" })
12
const
const bob: Person
bob
=
const Person: Data.Case.Constructor (args: { readonly _tag: "Person"; readonly name: string; }) => Person
Person
({
(property) _tag: "Person"
_tag
: "Person",
(property) name: string
name
: "Bob" })

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.

1
import {
import Data
Data
} from "effect"
2
3
interface
interface Person
Person
{
4
readonly
(property) Person._tag: "Person"
_tag
: "Person" // the tag
5
readonly
(property) Person.name: string
name
: string
6
}
7
8
const
const Person: Data.Case.Constructor<Person, "_tag">
Person
=
import Data
Data
.
const tagged: <Person>(tag: "Person") => Data.Case.Constructor<Person, "_tag">

Provides a tagged constructor for the specified `Case`.

tagged
<
interface Person
Person
>("Person")
9
10
// The `_tag` field is automatically added
11
const
const alice: Person
alice
=
const Person: Data.Case.Constructor (args: { readonly name: string; }) => Person
Person
({
(property) name: string
name
: "Alice" })
12
const
const bob: Person
bob
=
const Person: Data.Case.Constructor (args: { readonly name: string; }) => Person
Person
({
(property) name: string
name
: "Bob" })
13
14
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const alice: Person
alice
)
15
// Output: { name: 'Alice', _tag: 'Person' }

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:

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
// Define a Person class extending Data.Class
4
class
class Person
Person
extends
import Data
Data
.
const Class: new <A extends Record<string, any> = {}>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A]: A[P]; }) => Readonly<A>

Provides a constructor for a Case Class.

Class
<{
(property) name: string
name
: string }> {}
5
6
// Create an instance of Person
7
const
const alice: Person
alice
= new
constructor Person<{ name: string; }>(args: { readonly name: string; }): Person

Provides a constructor for a Case Class.

Person
({
(property) name: string
name
: "Alice" })
8
9
// Check for equality between two instances
10
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals
(
const alice: Person
alice
, new
constructor Person<{ name: string; }>(args: { readonly name: string; }): Person

Provides a constructor for a Case Class.

Person
({
(property) name: string
name
: "Alice" })))
11
// Output: true

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:

1
import {
import Data
Data
} from "effect"
2
3
// Extend Person class with a custom getter
4
class
class Person
Person
extends
import Data
Data
.
const Class: new <A extends Record<string, any> = {}>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A]: A[P]; }) => Readonly<A>

Provides a constructor for a Case Class.

Class
<{
(property) name: string
name
: string }> {
5
get
(getter) Person.upperName: string
upperName
() {
6
return this.
(property) name: string
name
.
(method) String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
()
7
}
8
}
9
10
// Create an instance and use the custom getter
11
const
const alice: Person
alice
= new
constructor Person<{ name: string; }>(args: { readonly name: string; }): Person

Provides a constructor for a Case Class.

Person
({
(property) name: string
name
: "Alice" })
12
13
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const alice: Person
alice
.
(property) Person.upperName: string
upperName
)
14
// Output: ALICE

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:

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
// Define a tagged class Person with the _tag "Person"
4
class
class Person
Person
extends
import Data
Data
.
const TaggedClass: <"Person">(tag: "Person") => new <A>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => Readonly<A> & { ...; }

Provides a Tagged constructor for a Case Class.

TaggedClass
("Person")<{
(property) name: string
name
: string }> {}
5
6
// Create an instance of Person
7
const
const alice: Person
alice
= new
constructor Person<{ name: string; }>(args: { readonly name: string; }): Person
Person
({
(property) name: string
name
: "Alice" })
8
9
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const alice: Person
alice
)
10
// Output: Person { name: 'Alice', _tag: 'Person' }
11
12
// Check equality between two instances
13
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)
equals
(
const alice: Person
alice
, new
constructor Person<{ name: string; }>(args: { readonly name: string; }): Person
Person
({
(property) name: string
name
: "Alice" })))
14
// Output: true

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:

1
import {
import Data
Data
} from "effect"
2
3
// Extend the Person class with a custom getter
4
class
class Person
Person
extends
import Data
Data
.
const TaggedClass: <"Person">(tag: "Person") => new <A>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => Readonly<A> & { ...; }

Provides a Tagged constructor for a Case Class.

TaggedClass
("Person")<{
(property) name: string
name
: string }> {
5
get
(getter) Person.upperName: string
upperName
() {
6
return this.
(property) name: string
name
.
(method) String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
()
7
}
8
}
9
10
// Create an instance and use the custom getter
11
const
const alice: Person
alice
= new
constructor Person<{ name: string; }>(args: { readonly name: string; }): Person
Person
({
(property) name: string
name
: "Alice" })
12
13
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const alice: Person
alice
.
(property) Person.upperName: string
upperName
)
14
// Output: ALICE

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.

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)

1
import {
import Data
Data
,
import Equal
Equal
} from "effect"
2
3
// Define a union type using TaggedEnum
4
type
type RemoteData = { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }
RemoteData
=
import Data
Data
.
type TaggedEnum<A extends Record<string, Record<string, any>> & UntaggedChildren<A>> = keyof A extends infer Tag ? Tag extends keyof A ? Simplify<{ readonly _tag: Tag; } & { readonly [K in keyof A[Tag]]: A[Tag][K]; }> : never : never namespace TaggedEnum

Create a tagged enum data type, which is a union of `Data` structs. ```ts import { Data } from "effect" type HttpError = Data.TaggedEnum<{ BadRequest: { readonly status: 400, readonly message: string } NotFound: { readonly status: 404, readonly message: string } }> // Equivalent to: type HttpErrorPlain = | { readonly _tag: "BadRequest" readonly status: 400 readonly message: string } | { readonly _tag: "NotFound" readonly status: 404 readonly message: string } ```

TaggedEnum
<{
5
(property) Loading: {}
Loading
: {}
6
(property) Success: { readonly data: string; }
Success
: { readonly
(property) data: string
data
: string }
7
(property) Failure: { readonly reason: string; }
Failure
: { readonly
(property) reason: string
reason
: string }
8
}>
9
10
// Create constructors for each case in the union
11
const {
const Loading: Data.Case.Constructor<{ readonly _tag: "Loading"; }, "_tag">
Loading
,
const Success: Data.Case.Constructor<{ readonly _tag: "Success"; readonly data: string; }, "_tag">
Success
,
const Failure: Data.Case.Constructor<{ readonly _tag: "Failure"; readonly reason: string; }, "_tag">
Failure
} =
import Data
Data
.
const taggedEnum: <{ readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }>() => { readonly Loading: Data.Case.Constructor<{ readonly _tag: "Loading"; }, "_tag">; readonly Success: Data.Case.Constructor<...>; readonly Failure: Data.Case.Constructor<...>; readonly $is: <Tag>(tag: Tag) => (u: unknown) => u is Extract<...> | ... 1 more ... | Extract<...>; readonly $match: { ...; }; } (+4 overloads)

Create a constructor for a tagged union of `Data` structs. You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to the constructor.

taggedEnum
<
type RemoteData = { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }
RemoteData
>()
12
13
// Instantiate different states
14
const
const state1: { readonly _tag: "Loading"; }
state1
=
const Loading: Data.Case.Constructor (args: void) => { readonly _tag: "Loading"; }
Loading
()
15
const
const state2: { readonly _tag: "Success"; readonly data: string; }
state2
=
const Success: Data.Case.Constructor (args: { readonly data: string; }) => { readonly _tag: "Success"; readonly data: string; }
Success
({
(property) data: string
data
: "test" })
16
const
const state3: { readonly _tag: "Success"; readonly data: string; }
state3
=
const Success: Data.Case.Constructor (args: { readonly data: string; }) => { readonly _tag: "Success"; readonly data: string; }
Success
({
(property) data: string
data
: "test" })
17
const
const state4: { readonly _tag: "Failure"; readonly reason: string; }
state4
=
const Failure: Data.Case.Constructor (args: { readonly reason: string; }) => { readonly _tag: "Failure"; readonly reason: string; }
Failure
({
(property) reason: string
reason
: "not found" })
18
19
// Check equality between states
20
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<{ readonly _tag: "Success"; readonly data: string; }, { readonly _tag: "Success"; readonly data: string; }>(self: { readonly _tag: "Success"; readonly data: string; }, that: { readonly _tag: "Success"; readonly data: string; }): boolean (+1 overload)
equals
(
const state2: { readonly _tag: "Success"; readonly data: string; }
state2
,
const state3: { readonly _tag: "Success"; readonly data: string; }
state3
)) // Output: true
21
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<{ readonly _tag: "Success"; readonly data: string; }, { readonly _tag: "Failure"; readonly reason: string; }>(self: { readonly _tag: "Success"; readonly data: string; }, that: { readonly _tag: "Failure"; readonly reason: string; }): boolean (+1 overload)
equals
(
const state2: { readonly _tag: "Success"; readonly data: string; }
state2
,
const state4: { readonly _tag: "Failure"; readonly reason: string; }
state4
)) // Output: false
22
23
// Display the states
24
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const state1: { readonly _tag: "Loading"; }
state1
) // Output: { _tag: 'Loading' }
25
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const state2: { readonly _tag: "Success"; readonly data: string; }
state2
) // Output: { data: 'test', _tag: 'Success' }
26
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const state4: { readonly _tag: "Failure"; readonly reason: string; }
state4
) // Output: { reason: 'not found', _tag: 'Failure' }

The Data.taggedEnum provides $is and $match functions for convenient type guarding and pattern matching.

Example (Using Type Guards and Pattern Matching)

1
import {
import Data
Data
} from "effect"
2
3
type
type RemoteData = { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }
RemoteData
=
import Data
Data
.
type TaggedEnum<A extends Record<string, Record<string, any>> & UntaggedChildren<A>> = keyof A extends infer Tag ? Tag extends keyof A ? Simplify<{ readonly _tag: Tag; } & { readonly [K in keyof A[Tag]]: A[Tag][K]; }> : never : never namespace TaggedEnum

Create a tagged enum data type, which is a union of `Data` structs. ```ts import { Data } from "effect" type HttpError = Data.TaggedEnum<{ BadRequest: { readonly status: 400, readonly message: string } NotFound: { readonly status: 404, readonly message: string } }> // Equivalent to: type HttpErrorPlain = | { readonly _tag: "BadRequest" readonly status: 400 readonly message: string } | { readonly _tag: "NotFound" readonly status: 404 readonly message: string } ```

TaggedEnum
<{
4
(property) Loading: {}
Loading
: {}
5
(property) Success: { readonly data: string; }
Success
: { readonly
(property) data: string
data
: string }
6
(property) Failure: { readonly reason: string; }
Failure
: { readonly
(property) reason: string
reason
: string }
7
}>
8
9
const {
const $is: <Tag extends "Loading" | "Success" | "Failure">(tag: Tag) => (u: unknown) => u is Extract<{ readonly _tag: "Loading"; }, { readonly _tag: Tag; }> | Extract<{ readonly _tag: "Success"; readonly data: string; }, { readonly _tag: Tag; }> | Extract<...>
$is
,
const $match: { <Cases extends { readonly Loading: (args: { readonly _tag: "Loading"; }) => any; readonly Success: (args: { readonly _tag: "Success"; readonly data: string; }) => any; readonly Failure: (args: { readonly _tag: "Failure"; readonly reason: string; }) => any; }>(cases: Cases): (value: { readonly _tag: "Loading"; } | ... 1 more ... | { readonly _tag: "Failure"; readonly reason: string; }) => Unify<...>; <Cases extends { ...; }>(value: { ...; } | ... 1 more ... | { ...; }, cases: Cases): Unify<...>; }
$match
,
const Loading: Data.Case.Constructor<{ readonly _tag: "Loading"; }, "_tag">
Loading
,
const Success: Data.Case.Constructor<{ readonly _tag: "Success"; readonly data: string; }, "_tag">
Success
} =
import Data
Data
.
const taggedEnum: <{ readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }>() => { readonly Loading: Data.Case.Constructor<{ readonly _tag: "Loading"; }, "_tag">; readonly Success: Data.Case.Constructor<...>; readonly Failure: Data.Case.Constructor<...>; readonly $is: <Tag>(tag: Tag) => (u: unknown) => u is Extract<...> | ... 1 more ... | Extract<...>; readonly $match: { ...; }; } (+4 overloads)

Create a constructor for a tagged union of `Data` structs. You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to the constructor.

taggedEnum
<
type RemoteData = { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }
RemoteData
>()
10
11
// Use `$is` to create a type guard for "Loading"
12
const
const isLoading: (u: unknown) => u is Extract<{ readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }, { readonly _tag: "Loading"; }>
isLoading
=
const $is: <"Loading">(tag: "Loading") => (u: unknown) => u is Extract<{ readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }, { readonly _tag: "Loading"; }>
$is
("Loading")
13
14
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const isLoading: (u: unknown) => u is Extract<{ readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }, { readonly _tag: "Loading"; }>
isLoading
(
const Loading: Data.Case.Constructor (args: void) => { readonly _tag: "Loading"; }
Loading
()))
15
// Output: true
16
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const isLoading: (u: unknown) => u is Extract<{ readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }, { readonly _tag: "Loading"; }>
isLoading
(
const Success: Data.Case.Constructor (args: { readonly data: string; }) => { readonly _tag: "Success"; readonly data: string; }
Success
({
(property) data: string
data
: "test" })))
17
// Output: false
18
19
// Use `$match` for pattern matching
20
const
const matcher: (value: { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }) => string
matcher
=
const $match: <{ Loading: () => string; Success: ({ data }: { readonly _tag: "Success"; readonly data: string; }) => string; Failure: ({ reason }: { readonly _tag: "Failure"; readonly reason: string; }) => string; }>(cases: { ...; }) => (value: { ...; } | ... 1 more ... | { ...; }) => string (+1 overload)
$match
({
21
(property) Loading: () => string
Loading
: () => "this is a Loading",
22
(property) Success: ({ data }: { readonly _tag: "Success"; readonly data: string; }) => string
Success
: ({
(parameter) data: string
data
}) => `this is a Success: ${
(parameter) data: string
data
}`,
23
(property) Failure: ({ reason }: { readonly _tag: "Failure"; readonly reason: string; }) => string
Failure
: ({
(parameter) reason: string
reason
}) => `this is a Failre: ${
(parameter) reason: string
reason
}`
24
})
25
26
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const matcher: (value: { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: string; } | { readonly _tag: "Failure"; readonly reason: string; }) => string
matcher
(
const Success: Data.Case.Constructor (args: { readonly data: string; }) => { readonly _tag: "Success"; readonly data: string; }
Success
({
(property) data: string
data
: "test" })))
27
// Output: "this is a Success: test"

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)

1
import {
import Data
Data
} from "effect"
2
3
// Define a generic TaggedEnum for RemoteData
4
type
type RemoteData<Success, Failure> = { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: Success; } | { readonly _tag: "Failure"; readonly reason: Failure; }
RemoteData
<
(type parameter) Success in type RemoteData<Success, Failure>
Success
,
(type parameter) Failure in type RemoteData<Success, Failure>
Failure
> =
import Data
Data
.
type TaggedEnum<A extends Record<string, Record<string, any>> & UntaggedChildren<A>> = keyof A extends infer Tag ? Tag extends keyof A ? Simplify<{ readonly _tag: Tag; } & { readonly [K in keyof A[Tag]]: A[Tag][K]; }> : never : never namespace TaggedEnum

Create a tagged enum data type, which is a union of `Data` structs. ```ts import { Data } from "effect" type HttpError = Data.TaggedEnum<{ BadRequest: { readonly status: 400, readonly message: string } NotFound: { readonly status: 404, readonly message: string } }> // Equivalent to: type HttpErrorPlain = | { readonly _tag: "BadRequest" readonly status: 400 readonly message: string } | { readonly _tag: "NotFound" readonly status: 404 readonly message: string } ```

TaggedEnum
<{
5
(property) Loading: {}
Loading
: {}
6
(property) Success: { data: Success; }
Success
: {
(property) data: Success
data
:
(type parameter) Success in type RemoteData<Success, Failure>
Success
}
7
(property) Failure: { reason: Failure; }
Failure
: {
(property) reason: Failure
reason
:
(type parameter) Failure in type RemoteData<Success, Failure>
Failure
}
8
}>
9
10
// Extend TaggedEnum.WithGenerics to add generics
11
interface
interface RemoteDataDefinition
RemoteDataDefinition
extends
import Data
Data
.
namespace TaggedEnum

Create a tagged enum data type, which is a union of `Data` structs. ```ts import { Data } from "effect" type HttpError = Data.TaggedEnum<{ BadRequest: { readonly status: 400, readonly message: string } NotFound: { readonly status: 404, readonly message: string } }> // Equivalent to: type HttpErrorPlain = | { readonly _tag: "BadRequest" readonly status: 400 readonly message: string } | { readonly _tag: "NotFound" readonly status: 404 readonly message: string } ```

TaggedEnum
.
interface TaggedEnum<A extends Record<string, Record<string, any>> & UntaggedChildren<A>>.WithGenerics<Count extends number>
WithGenerics
<2> {
12
readonly
(property) RemoteDataDefinition.taggedEnum: { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: this["A"]; } | { readonly _tag: "Failure"; readonly reason: this["B"]; }
taggedEnum
:
type RemoteData<Success, Failure> = { readonly _tag: "Loading"; } | { readonly _tag: "Success"; readonly data: Success; } | { readonly _tag: "Failure"; readonly reason: Failure; }
RemoteData
<this["A"], this["B"]>
13
}
14
15
// Create constructors for the generic RemoteData
16
const {
const Loading: <A, B>(args: void) => { readonly _tag: "Loading"; }
Loading
,
const Failure: <A, B>(args: { readonly reason: B; }) => { readonly _tag: "Failure"; readonly reason: B; }
Failure
,
const Success: <A, B>(args: { readonly data: A; }) => { readonly _tag: "Success"; readonly data: A; }
Success
} =
17
import Data
Data
.
const taggedEnum: <RemoteDataDefinition>() => { readonly Loading: <A, B>(args: void) => { readonly _tag: "Loading"; }; readonly Success: <A, B>(args: { readonly data: A; }) => { readonly _tag: "Success"; readonly data: A; }; readonly Failure: <A, B>(args: { ...; }) => { ...; }; readonly $is: <Tag>(tag: Tag) => { ...; }; readonly $match: { ...; }; } (+4 overloads)

Create a constructor for a tagged union of `Data` structs. You can also pass a `TaggedEnum.WithGenerics` if you want to add generics to the constructor.

taggedEnum
<
interface RemoteDataDefinition
RemoteDataDefinition
>()
18
19
// Instantiate each case with specific types
20
const
const loading: { readonly _tag: "Loading"; }
loading
=
const Loading: <unknown, unknown>(args: void) => { readonly _tag: "Loading"; }
Loading
()
21
const
const failure: { readonly _tag: "Failure"; readonly reason: string; }
failure
=
const Failure: <unknown, string>(args: { readonly reason: string; }) => { readonly _tag: "Failure"; readonly reason: string; }
Failure
({
(property) reason: string
reason
: "not found" })
22
const
const success: { readonly _tag: "Success"; readonly data: number; }
success
=
const Success: <number, unknown>(args: { readonly data: number; }) => { readonly _tag: "Success"; readonly data: number; }
Success
({
(property) data: number
data
: 1 })

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.

Data.Error lets you create an Error type with extra fields beyond the typical message property.

Example (Creating a Custom Error with Additional Fields)

1
import {
import Data
Data
} from "effect"
2
3
// Define a custom error with additional fields
4
class
class NotFound
NotFound
extends
import Data
Data
.
const Error: new <A extends Record<string, any> = {}>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A]: A[P]; }) => YieldableError & Readonly<A>

Provides a constructor for a Case Class.

Error
<{
(property) message: string
message
: string;
(property) file: string
file
: string }> {}
5
6
// Create an instance of the custom error
7
const
const err: NotFound
err
= new
constructor NotFound<{ message: string; file: string; }>(args: { readonly message: string; readonly file: string; }): NotFound

Provides a constructor for a Case Class.

NotFound
({
8
(property) message: string
message
: "Cannot find this file",
9
(property) file: string
file
: "foo.txt"
10
})
11
12
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const err: NotFound
err
instanceof
var Error: ErrorConstructor
Error
)
13
// Output: true
14
15
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const err: NotFound
err
.
(property) file: string
file
)
16
// Output: foo.txt
17
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const err: NotFound
err
)
18
/*
19
Output:
20
NotFound [Error]: Cannot find this file
21
file: 'foo.txt'
22
... stack trace ...
23
*/

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)

1
import {
import Data
Data
,
import Effect
Effect
} from "effect"
2
3
class
class NotFound
NotFound
extends
import Data
Data
.
const Error: new <A extends Record<string, any> = {}>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A]: A[P]; }) => YieldableError & Readonly<A>

Provides a constructor for a Case Class.

Error
<{
(property) message: string
message
: string;
(property) file: string
file
: string }> {}
4
5
const
const program: Effect.Effect<void, NotFound, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<never, NotFound, never>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<never, NotFound, never>>, void, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
6
yield* new
constructor NotFound<{ message: string; file: string; }>(args: { readonly message: string; readonly file: string; }): NotFound

Provides a constructor for a Case Class.

NotFound
({
7
(property) message: string
message
: "Cannot find this file",
8
(property) file: string
file
: "foo.txt"
9
})
10
})
11
12
import Effect
Effect
.
const runPromise: <void, NotFound>(effect: Effect.Effect<void, NotFound, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
const program: Effect.Effect<void, NotFound, never>
program
)
13
/*
14
throws:
15
Error: Cannot find this file
16
at ... {
17
name: '(FiberFailure) Error',
18
[Symbol(effect/Runtime/FiberFailure/Cause)]: {
19
_tag: 'Fail',
20
error: NotFound [Error]: Cannot find this file
21
at ...stack trace...
22
file: 'foo.txt'
23
}
24
}
25
}
26
*/

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.

1
import {
import Data
Data
,
import Effect
Effect
,
import Console
Console
} from "effect"
2
3
// Define a custom tagged error
4
class
class NotFound
NotFound
extends
import Data
Data
.
const TaggedError: <"NotFound">(tag: "NotFound") => new <A>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => YieldableError & { ...; } & Readonly<...>
TaggedError
("NotFound")<{
5
(property) message: string
message
: string
6
(property) file: string
file
: string
7
}> {}
8
9
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<never, NotFound, never>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<never, NotFound, never>>, void, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
10
yield* new
constructor NotFound<{ message: string; file: string; }>(args: { readonly message: string; readonly file: string; }): NotFound
NotFound
({
11
(property) message: string
message
: "Cannot find this file",
12
(property) file: string
file
: "foo.txt"
13
})
14
}).
(method) Pipeable.pipe<Effect.Effect<void, NotFound, never>, Effect.Effect<void, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, NotFound, never>) => Effect.Effect<void, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
15
// Catch and handle the tagged error
16
import Effect
Effect
.
const catchTag: <"NotFound", NotFound, void, never, never>(k: "NotFound", f: (e: NotFound) => Effect.Effect<void, never, never>) => <A, R>(self: Effect.Effect<A, NotFound, R>) => Effect.Effect<...> (+1 overload)

Recovers from the specified tagged error.

catchTag
("NotFound", (
(parameter) err: NotFound
err
) =>
17
import Console
Console
.
const error: (...args: ReadonlyArray<any>) => Effect.Effect<void>
error
(`${
(parameter) err: NotFound
err
.
(property) message: string
message
} (${
(parameter) err: NotFound
err
.
(property) file: string
file
})`)
18
)
19
)
20
21
import Effect
Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
const program: Effect.Effect<void, never, never>
program
)
22
// Output: Cannot find this file (foo.txt)

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.

Example (Using the cause Property)

1
import {
import Data
Data
,
import Effect
Effect
} from "effect"
2
3
// Define an error with a cause property
4
class
class MyError
MyError
extends
import Data
Data
.
const Error: new <A extends Record<string, any> = {}>(args: Equals<A, {}> extends true ? void : { readonly [P in keyof A]: A[P]; }) => YieldableError & Readonly<A>

Provides a constructor for a Case Class.

Error
<{
(property) cause: Error
cause
:
interface Error
Error
}> {}
5
6
const
const program: Effect.Effect<void, MyError, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<never, MyError, never>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<never, MyError, never>>, void, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
7
yield* new
constructor MyError<{ cause: Error; }>(args: { readonly cause: Error; }): MyError

Provides a constructor for a Case Class.

MyError
({
8
(property) cause: Error
cause
: new
var Error: ErrorConstructor new (message?: string) => Error
Error
("Something went wrong")
9
})
10
})
11
12
import Effect
Effect
.
const runPromise: <void, MyError>(effect: Effect.Effect<void, MyError, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
const program: Effect.Effect<void, MyError, never>
program
)
13
/*
14
throws:
15
Error: An error has occurred
16
at ... {
17
name: '(FiberFailure) Error',
18
[Symbol(effect/Runtime/FiberFailure/Cause)]: {
19
_tag: 'Fail',
20
error: MyError
21
at ...
22
[cause]: Error: Something went wrong
23
at ...
24
*/