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)

const
const alice: {
name: string;
age: number;
}
alice
= {
name: string
name
: "Alice",
age: number
age
: 30 }
// This comparison is false because they are different instances
// @ts-expect-error
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const alice: {
name: string;
age: number;
}
alice
=== {
name: string
name
: "Alice",
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)

import {
import Data
Data
,
import Equal
Equal
} from "effect"
// ┌─── { readonly name: string; readonly age: number; }
// ▼
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;
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
name: string
name
: "Alice",
age: number
age
: 30 })
// Check if Alice is equal to a new object
// with the same structure and values
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

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)

@since2.0.0

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;
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
name: string
name
: "Alice",
age: number
age
: 30 })))
// Output: true
// Check if Alice is equal to a plain JavaScript object
// with the same content
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

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)

@since2.0.0

equals
(
const alice: {
readonly name: string;
readonly age: number;
}
alice
, {
name: string
name
: "Alice",
age: number
age
: 30 }))
// 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)

import {
import Data
Data
,
import Equal
Equal
} from "effect"
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;
};
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
name: string
name
: "Alice",
nested_field: {
value: number;
}
nested_field
: {
value: number
value
: 42 } })
// This will be false because the nested objects are compared by reference
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
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: {
readonly name: string;
readonly nested_field: {
value: number;
};
}): boolean (+1 overload)

@since2.0.0

equals
(
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;
};
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
name: string
name
: "Alice",
nested_field: {
value: number;
}
nested_field
: {
value: number
value
: 42 } })
)
)
// Output: false

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

Example (Correctly Comparing Nested Objects)

import {
import Data
Data
,
import Equal
Equal
} from "effect"
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;
};
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
name: string
name
: "Alice",
nested_field: {
readonly value: number;
}
nested_field
:
import Data
Data
.
const struct: <{
value: number;
}>(a: {
value: number;
}) => {
readonly value: number;
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
value: number
value
: 42 })
})
// Now, the comparison returns true
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
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: {
readonly name: string;
readonly nested_field: {
readonly value: number;
};
}): boolean (+1 overload)

@since2.0.0

equals
(
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;
};
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
name: string
name
: "Alice",
nested_field: {
readonly value: number;
}
nested_field
:
import Data
Data
.
const struct: <{
value: number;
}>(a: {
value: number;
}) => {
readonly value: number;
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
value: number
value
: 42 })
})
)
)
// 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)

import {
import Data
Data
,
import Equal
Equal
} from "effect"
// ┌─── readonly [string, number]
// ▼
const
const alice: readonly [string, number]
alice
=
import Data
Data
.
const tuple: <[string, number]>(as_0: string, as_1: number) => readonly [string, number]

@example

import { Data, Equal } from "effect"
const alice = Data.tuple("Alice", 30)
const bob = Data.tuple("Bob", 40)
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.tuple("Alice", 30)), true)
assert.deepStrictEqual(Equal.equals(alice, ["Alice", 30]), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

tuple
("Alice", 30)
// Check if Alice is equal to a new tuple
// with the same structure and values
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<readonly [string, number], readonly [string, number]>(self: readonly [string, number], that: readonly [string, number]): boolean (+1 overload)

@since2.0.0

equals
(
const alice: readonly [string, number]
alice
,
import Data
Data
.
const tuple: <[string, number]>(as_0: string, as_1: number) => readonly [string, number]

@example

import { Data, Equal } from "effect"
const alice = Data.tuple("Alice", 30)
const bob = Data.tuple("Bob", 40)
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.tuple("Alice", 30)), true)
assert.deepStrictEqual(Equal.equals(alice, ["Alice", 30]), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

tuple
("Alice", 30)))
// Output: true
// Check if Alice is equal to a plain JavaScript tuple
// with the same content
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<readonly [string, number], (string | number)[]>(self: readonly [string, number], that: (string | number)[]): boolean (+1 overload)

@since2.0.0

equals
(
const alice: readonly [string, number]
alice
, ["Alice", 30]))
// 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)

import {
import Data
Data
,
import Equal
Equal
} from "effect"
// ┌─── readonly number[]
// ▼
const
const numbers: readonly number[]
numbers
=
import Data
Data
.
const array: <number[]>(as: number[]) => readonly number[]

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
const persons = Data.array([alice, bob])
assert.deepStrictEqual(
Equal.equals(
persons,
Data.array([
Data.struct({ name: "Alice", age: 30 }),
Data.struct({ name: "Bob", age: 40 })
])
),
true
)

@since2.0.0

array
([1, 2, 3, 4, 5])
// Check if the array is equal to a new array
// with the same values
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<readonly number[], readonly number[]>(self: readonly number[], that: readonly number[]): boolean (+1 overload)

@since2.0.0

equals
(
const numbers: readonly number[]
numbers
,
import Data
Data
.
const array: <number[]>(as: number[]) => readonly number[]

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
const persons = Data.array([alice, bob])
assert.deepStrictEqual(
Equal.equals(
persons,
Data.array([
Data.struct({ name: "Alice", age: 30 }),
Data.struct({ name: "Bob", age: 40 })
])
),
true
)

@since2.0.0

array
([1, 2, 3, 4, 5])))
// Output: true
// Check if the array is equal to a plain JavaScript array
// with the same content
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<readonly number[], number[]>(self: readonly number[], that: number[]): boolean (+1 overload)

@since2.0.0

equals
(
const numbers: readonly number[]
numbers
, [1, 2, 3, 4, 5]))
// 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.

import {
import Data
Data
,
import Equal
Equal
} from "effect"
interface
interface Person
Person
{
readonly
Person.name: string
name
: string
}
// Create a constructor for `Person`
//
// ┌─── (args: { readonly name: string; }) => Person
// ▼
const
const make: Data.Case.Constructor<Person, never>
make
=
import Data
Data
.
case<Person>(): Data.Case.Constructor<Person, never>
export case

Provides a constructor for the specified Case.

@example

import { Data, Equal } from "effect"
interface Person {
readonly name: string
}
// Creating a constructor for the specified Case
const Person = Data.case<Person>()
// Creating instances of Person
const mike1 = Person({ name: "Mike" })
const mike2 = Person({ name: "Mike" })
const john = Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

case
<
interface Person
Person
>()
const
const alice: Person
alice
=
const make: Data.Case.Constructor
(args: {
readonly name: string;
}) => Person
make
({
name: string
name
: "Alice" })
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)

@since2.0.0

equals
(
const alice: Person
alice
,
const make: Data.Case.Constructor
(args: {
readonly name: string;
}) => Person
make
({
name: string
name
: "Alice" })))
// Output: true
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)

@since2.0.0

equals
(
const alice: Person
alice
,
const make: Data.Case.Constructor
(args: {
readonly name: string;
}) => Person
make
({
name: string
name
: "John" })))
// 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.

import {
import Data
Data
,
import Equal
Equal
} from "effect"
interface
interface Address
Address
{
readonly
Address.street: string
street
: string
readonly
Address.city: string
city
: string
}
// Create a constructor for `Address`
const
const Address: Data.Case.Constructor<Address, never>
Address
=
import Data
Data
.
case<Address>(): Data.Case.Constructor<Address, never>
export case

Provides a constructor for the specified Case.

@example

import { Data, Equal } from "effect"
interface Person {
readonly name: string
}
// Creating a constructor for the specified Case
const Person = Data.case<Person>()
// Creating instances of Person
const mike1 = Person({ name: "Mike" })
const mike2 = Person({ name: "Mike" })
const john = Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

case
<
interface Address
Address
>()
interface
interface Person
Person
{
readonly
Person.name: string
name
: string
readonly
Person.address: Address
address
:
interface Address
Address
}
// Create a constructor for `Person`
const
const Person: Data.Case.Constructor<Person, never>
Person
=
import Data
Data
.
case<Person>(): Data.Case.Constructor<Person, never>
export case

Provides a constructor for the specified Case.

@example

import { Data, Equal } from "effect"
interface Person {
readonly name: string
}
// Creating a constructor for the specified Case
const Person = Data.case<Person>()
// Creating instances of Person
const mike1 = Person({ name: "Mike" })
const mike2 = Person({ name: "Mike" })
const john = Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

case
<
interface Person
Person
>()
const
const alice: Person
alice
=
const Person: Data.Case.Constructor
(args: {
readonly name: string;
readonly address: Address;
}) => Person
Person
({
name: string
name
: "Alice",
address: Address
address
:
const Address: Data.Case.Constructor
(args: {
readonly street: string;
readonly city: string;
}) => Address
Address
({
street: string
street
: "123 Main St",
city: string
city
: "Wonderland" })
})
const
const anotherAlice: Person
anotherAlice
=
const Person: Data.Case.Constructor
(args: {
readonly name: string;
readonly address: Address;
}) => Person
Person
({
name: string
name
: "Alice",
address: Address
address
:
const Address: Data.Case.Constructor
(args: {
readonly street: string;
readonly city: string;
}) => Address
Address
({
street: string
street
: "123 Main St",
city: string
city
: "Wonderland" })
})
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)

@since2.0.0

equals
(
const alice: Person
alice
,
const anotherAlice: Person
anotherAlice
))
// 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)

import {
import Data
Data
,
import Equal
Equal
} from "effect"
interface
interface Person
Person
{
readonly
Person.name: string
name
: string
readonly
Person.address: {
readonly street: string;
readonly city: string;
}
address
: {
readonly
street: string
street
: string
readonly
city: string
city
: string
}
}
// Create a constructor for `Person`
const
const Person: Data.Case.Constructor<Person, never>
Person
=
import Data
Data
.
case<Person>(): Data.Case.Constructor<Person, never>
export case

Provides a constructor for the specified Case.

@example

import { Data, Equal } from "effect"
interface Person {
readonly name: string
}
// Creating a constructor for the specified Case
const Person = Data.case<Person>()
// Creating instances of Person
const mike1 = Person({ name: "Mike" })
const mike2 = Person({ name: "Mike" })
const john = Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

case
<
interface Person
Person
>()
const
const alice: Person
alice
=
const Person: Data.Case.Constructor
(args: {
readonly name: string;
readonly address: {
readonly street: string;
readonly city: string;
};
}) => Person
Person
({
name: string
name
: "Alice",
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;
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
street: string
street
: "123 Main St",
city: string
city
: "Wonderland" })
})
const
const anotherAlice: Person
anotherAlice
=
const Person: Data.Case.Constructor
(args: {
readonly name: string;
readonly address: {
readonly street: string;
readonly city: string;
};
}) => Person
Person
({
name: string
name
: "Alice",
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;
}

@example

import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
street: string
street
: "123 Main St",
city: string
city
: "Wonderland" })
})
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)

@since2.0.0

equals
(
const alice: Person
alice
,
const anotherAlice: Person
anotherAlice
))
// Output: true

Example (Defining and Comparing Recursive Case Classes)

This example demonstrates a recursive structure using Data.case to define a binary tree where each node can contain other nodes.

import {
import Data
Data
,
import Equal
Equal
} from "effect"
interface
interface BinaryTree<T>
BinaryTree
<
function (type parameter) T in BinaryTree<T>
T
> {
readonly
BinaryTree<T>.value: T
value
:
function (type parameter) T in BinaryTree<T>
T
readonly
BinaryTree<T>.left: BinaryTree<T> | null
left
:
interface BinaryTree<T>
BinaryTree
<
function (type parameter) T in BinaryTree<T>
T
> | null
readonly
BinaryTree<T>.right: BinaryTree<T> | null
right
:
interface BinaryTree<T>
BinaryTree
<
function (type parameter) T in BinaryTree<T>
T
> | null
}
// Create a constructor for `BinaryTree`
const
const BinaryTree: Data.Case.Constructor<BinaryTree<number>, never>
BinaryTree
=
import Data
Data
.
case<BinaryTree<number>>(): Data.Case.Constructor<BinaryTree<number>, never>
export case

Provides a constructor for the specified Case.

@example

import { Data, Equal } from "effect"
interface Person {
readonly name: string
}
// Creating a constructor for the specified Case
const Person = Data.case<Person>()
// Creating instances of Person
const mike1 = Person({ name: "Mike" })
const mike2 = Person({ name: "Mike" })
const john = Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

case
<
interface BinaryTree<T>
BinaryTree
<number>>()
const
const tree1: BinaryTree<number>
tree1
=
const BinaryTree: Data.Case.Constructor
(args: {
readonly value: number;
readonly left: BinaryTree<number> | null;
readonly right: BinaryTree<number> | null;
}) => BinaryTree<number>
BinaryTree
({
value: number
value
: 0,
left: BinaryTree<number> | null
left
:
const BinaryTree: Data.Case.Constructor
(args: {
readonly value: number;
readonly left: BinaryTree<number> | null;
readonly right: BinaryTree<number> | null;
}) => BinaryTree<number>
BinaryTree
({
value: number
value
: 1,
left: BinaryTree<number> | null
left
: null,
right: BinaryTree<number> | null
right
: null }),
right: BinaryTree<number> | null
right
: null
})
const
const tree2: BinaryTree<number>
tree2
=
const BinaryTree: Data.Case.Constructor
(args: {
readonly value: number;
readonly left: BinaryTree<number> | null;
readonly right: BinaryTree<number> | null;
}) => BinaryTree<number>
BinaryTree
({
value: number
value
: 0,
left: BinaryTree<number> | null
left
:
const BinaryTree: Data.Case.Constructor
(args: {
readonly value: number;
readonly left: BinaryTree<number> | null;
readonly right: BinaryTree<number> | null;
}) => BinaryTree<number>
BinaryTree
({
value: number
value
: 1,
left: BinaryTree<number> | null
left
: null,
right: BinaryTree<number> | null
right
: null }),
right: BinaryTree<number> | null
right
: null
})
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<BinaryTree<number>, BinaryTree<number>>(self: BinaryTree<number>, that: BinaryTree<number>): boolean (+1 overload)

@since2.0.0

equals
(
const tree1: BinaryTree<number>
tree1
,
const tree2: BinaryTree<number>
tree2
))
// 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.

import {
import Data
Data
} from "effect"
interface
interface Person
Person
{
readonly
Person._tag: "Person"
_tag
: "Person" // the tag
readonly
Person.name: string
name
: string
}
const
const Person: Data.Case.Constructor<Person, never>
Person
=
import Data
Data
.
case<Person>(): Data.Case.Constructor<Person, never>
export case

Provides a constructor for the specified Case.

@example

import { Data, Equal } from "effect"
interface Person {
readonly name: string
}
// Creating a constructor for the specified Case
const Person = Data.case<Person>()
// Creating instances of Person
const mike1 = Person({ name: "Mike" })
const mike2 = Person({ name: "Mike" })
const john = Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

case
<
interface Person
Person
>()
// Repeating `_tag: 'Person'` for each instance
const
const alice: Person
alice
=
const Person: Data.Case.Constructor
(args: {
readonly _tag: "Person";
readonly name: string;
}) => Person
Person
({
_tag: "Person"
_tag
: "Person",
name: string
name
: "Alice" })
const
const bob: Person
bob
=
const Person: Data.Case.Constructor
(args: {
readonly _tag: "Person";
readonly name: string;
}) => Person
Person
({
_tag: "Person"
_tag
: "Person",
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.

import {
import Data
Data
} from "effect"
interface
interface Person
Person
{
readonly
Person._tag: "Person"
_tag
: "Person" // the tag
readonly
Person.name: string
name
: string
}
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.

@example

import { Data } from "effect"
interface Person {
readonly _tag: "Person" // the tag
readonly name: string
}
const Person = Data.tagged<Person>("Person")
const mike = Person({ name: "Mike" })
assert.deepEqual(mike, { _tag: "Person", name: "Mike" })

@since2.0.0

tagged
<
interface Person
Person
>("Person")
// The `_tag` field is automatically added
const
const alice: Person
alice
=
const Person: Data.Case.Constructor
(args: {
readonly name: string;
}) => Person
Person
({
name: string
name
: "Alice" })
const
const bob: Person
bob
=
const Person: Data.Case.Constructor
(args: {
readonly name: string;
}) => Person
Person
({
name: string
name
: "Bob" })
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const alice: Person
alice
)
// 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:

import {
import Data
Data
,
import Equal
Equal
} from "effect"
// Define a Person class extending Data.Class
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.

@example

import { Data, Equal } from "effect"
class Person extends Data.Class<{ readonly name: string }> {}
// Creating instances of Person
const mike1 = new Person({ name: "Mike" })
const mike2 = new Person({ name: "Mike" })
const john = new Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

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

Provides a constructor for a Case Class.

@example

import { Data, Equal } from "effect"
class Person extends Data.Class<{ readonly name: string }> {}
// Creating instances of Person
const mike1 = new Person({ name: "Mike" })
const mike2 = new Person({ name: "Mike" })
const john = new Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

Person
({
name: string
name
: "Alice" })
// Check for equality between two instances
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)

@since2.0.0

equals
(
const alice: Person
alice
, new
constructor Person<{
name: string;
}>(args: {
readonly name: string;
}): Person

Provides a constructor for a Case Class.

@example

import { Data, Equal } from "effect"
class Person extends Data.Class<{ readonly name: string }> {}
// Creating instances of Person
const mike1 = new Person({ name: "Mike" })
const mike2 = new Person({ name: "Mike" })
const john = new Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

Person
({
name: string
name
: "Alice" })))
// 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:

import {
import Data
Data
} from "effect"
// Extend Person class with a custom getter
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.

@example

import { Data, Equal } from "effect"
class Person extends Data.Class<{ readonly name: string }> {}
// Creating instances of Person
const mike1 = new Person({ name: "Mike" })
const mike2 = new Person({ name: "Mike" })
const john = new Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

Class
<{
name: string
name
: string }> {
get
Person.upperName: string
upperName
() {
return this.
name: string
name
.
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

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

Provides a constructor for a Case Class.

@example

import { Data, Equal } from "effect"
class Person extends Data.Class<{ readonly name: string }> {}
// Creating instances of Person
const mike1 = new Person({ name: "Mike" })
const mike2 = new Person({ name: "Mike" })
const john = new Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)

@since2.0.0

Person
({
name: string
name
: "Alice" })
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const alice: Person
alice
.
Person.upperName: string
upperName
)
// 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:

import {
import Data
Data
,
import Equal
Equal
} from "effect"
// Define a tagged class Person with the _tag "Person"
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.

@example

import { Data, Equal } from "effect"
class Person extends Data.TaggedClass("Person")<{ readonly name: string }> {}
// Creating instances of Person
const mike1 = new Person({ name: "Mike" })
const mike2 = new Person({ name: "Mike" })
const john = new Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)
assert.deepStrictEqual(mike1._tag, "Person")

@since2.0.0

TaggedClass
("Person")<{
name: string
name
: string }> {}
// Create an instance of Person
const
const alice: Person
alice
= new
constructor Person<{
name: string;
}>(args: {
readonly name: string;
}): Person
Person
({
name: string
name
: "Alice" })
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const alice: Person
alice
)
// Output: Person { name: 'Alice', _tag: 'Person' }
// Check equality between two instances
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<Person, Person>(self: Person, that: Person): boolean (+1 overload)

@since2.0.0

equals
(
const alice: Person
alice
, new
constructor Person<{
name: string;
}>(args: {
readonly name: string;
}): Person
Person
({
name: string
name
: "Alice" })))
// 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:

import {
import Data
Data
} from "effect"
// Extend the Person class with a custom getter
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.

@example

import { Data, Equal } from "effect"
class Person extends Data.TaggedClass("Person")<{ readonly name: string }> {}
// Creating instances of Person
const mike1 = new Person({ name: "Mike" })
const mike2 = new Person({ name: "Mike" })
const john = new Person({ name: "John" })
// Checking equality
assert.deepStrictEqual(Equal.equals(mike1, mike2), true)
assert.deepStrictEqual(Equal.equals(mike1, john), false)
assert.deepStrictEqual(mike1._tag, "Person")

@since2.0.0

TaggedClass
("Person")<{
name: string
name
: string }> {
get
Person.upperName: string
upperName
() {
return this.
name: string
name
.
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
()
}
}
// Create an instance and use the custom getter
const
const alice: Person
alice
= new
constructor Person<{
name: string;
}>(args: {
readonly name: string;
}): Person
Person
({
name: string
name
: "Alice" })
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const alice: Person
alice
.
Person.upperName: string
upperName
)
// 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)

import {
import Data
Data
,
import Equal
Equal
} from "effect"
// Define a union type using TaggedEnum
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

Create a tagged enum data type, which is a union of Data structs.

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
}

@since2.0.0

@since2.0.0

TaggedEnum
<{
type Loading: {}
Loading
: {}
type Success: {
readonly data: string;
}
Success
: { readonly
data: string
data
: string }
type Failure: {
readonly reason: string;
}
Failure
: { readonly
reason: string
reason
: string }
}>
// Create constructors for each case in the union
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.

@example

import { Data } from "effect"
const { BadRequest, NotFound } = Data.taggedEnum<
| { readonly _tag: "BadRequest"; readonly status: 400; readonly message: string }
| { readonly _tag: "NotFound"; readonly status: 404; readonly message: string }
>()
const notFound = NotFound({ status: 404, message: "Not Found" })

@example

import { Data } from "effect"

type MyResult<E, A> = Data.TaggedEnum<{ Failure: { readonly error: E } Success: { readonly value: A } }> interface MyResultDefinition extends Data.TaggedEnum.WithGenerics<2> { readonly taggedEnum: MyResult<this["A"], this["B"]> } const { Failure, Success } = Data.taggedEnum()

const success = Success({ value: 1 })

@since2.0.0

taggedEnum
<
type RemoteData = {
readonly _tag: "Loading";
} | {
readonly _tag: "Success";
readonly data: string;
} | {
readonly _tag: "Failure";
readonly reason: string;
}
RemoteData
>()
// Instantiate different states
const
const state1: {
readonly _tag: "Loading";
}
state1
=
const Loading: Data.Case.Constructor
(args: void) => {
readonly _tag: "Loading";
}
Loading
()
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
({
data: string
data
: "test" })
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
({
data: string
data
: "test" })
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
({
reason: string
reason
: "not found" })
// Check equality between states
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

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)

@since2.0.0

equals
(
const state2: {
readonly _tag: "Success";
readonly data: string;
}
state2
,
const state3: {
readonly _tag: "Success";
readonly data: string;
}
state3
)) // Output: true
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

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)

@since2.0.0

equals
(
const state2: {
readonly _tag: "Success";
readonly data: string;
}
state2
,
const state4: {
readonly _tag: "Failure";
readonly reason: string;
}
state4
)) // Output: false
// Display the states
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const state1: {
readonly _tag: "Loading";
}
state1
) // Output: { _tag: 'Loading' }
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const state2: {
readonly _tag: "Success";
readonly data: string;
}
state2
) // Output: { data: 'test', _tag: 'Success' }
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

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)

import {
import Data
Data
} from "effect"
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

Create a tagged enum data type, which is a union of Data structs.

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
}

@since2.0.0

@since2.0.0

TaggedEnum
<{
type Loading: {}
Loading
: {}
type Success: {
readonly data: string;
}
Success
: { readonly
data: string
data
: string }
type Failure: {
readonly reason: string;
}
Failure
: { readonly
reason: string
reason
: string }
}>
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.

@example

import { Data } from "effect"
const { BadRequest, NotFound } = Data.taggedEnum<
| { readonly _tag: "BadRequest"; readonly status: 400; readonly message: string }
| { readonly _tag: "NotFound"; readonly status: 404; readonly message: string }
>()
const notFound = NotFound({ status: 404, message: "Not Found" })

@example

import { Data } from "effect"

type MyResult<E, A> = Data.TaggedEnum<{ Failure: { readonly error: E } Success: { readonly value: A } }> interface MyResultDefinition extends Data.TaggedEnum.WithGenerics<2> { readonly taggedEnum: MyResult<this["A"], this["B"]> } const { Failure, Success } = Data.taggedEnum()

const success = Success({ value: 1 })

@since2.0.0

taggedEnum
<
type RemoteData = {
readonly _tag: "Loading";
} | {
readonly _tag: "Success";
readonly data: string;
} | {
readonly _tag: "Failure";
readonly reason: string;
}
RemoteData
>()
// Use `$is` to create a type guard for "Loading"
const
const isLoading: (u: unknown) => u is {
readonly _tag: "Loading";
}
isLoading
=
const $is: <"Loading">(tag: "Loading") => (u: unknown) => u is {
readonly _tag: "Loading";
}
$is
("Loading")
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const isLoading: (u: unknown) => u is {
readonly _tag: "Loading";
}
isLoading
(
const Loading: Data.Case.Constructor
(args: void) => {
readonly _tag: "Loading";
}
Loading
()))
// Output: true
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const isLoading: (u: unknown) => u is {
readonly _tag: "Loading";
}
isLoading
(
const Success: Data.Case.Constructor
(args: {
readonly data: string;
}) => {
readonly _tag: "Success";
readonly data: string;
}
Success
({
data: string
data
: "test" })))
// Output: false
// Use `$match` for pattern matching
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: {
Loading: () => string;
Success: ({ data }: {
readonly _tag: "Success";
readonly data: string;
}) => string;
Failure: ({ reason }: {
readonly _tag: "Failure";
readonly reason: string;
}) => string;
}) => (value: {
...;
} | ... 1 more ... | {
...;
}) => string (+1 overload)
$match
({
type Loading: () => string
Loading
: () => "this is a Loading",
type Success: ({ data }: {
readonly _tag: "Success";
readonly data: string;
}) => string
Success
: ({
data: string
data
}) => `this is a Success: ${
data: string
data
}`,
type Failure: ({ reason }: {
readonly _tag: "Failure";
readonly reason: string;
}) => string
Failure
: ({
reason: string
reason
}) => `this is a Failre: ${
reason: string
reason
}`
})
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

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
({
data: string
data
: "test" })))
// 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)

import {
import Data
Data
} from "effect"
// Define a generic TaggedEnum for RemoteData
type
type RemoteData<Success, Failure> = {
readonly _tag: "Loading";
} | {
readonly _tag: "Success";
readonly data: Success;
} | {
readonly _tag: "Failure";
readonly reason: Failure;
}
RemoteData
<
function (type parameter) Success in type RemoteData<Success, Failure>
Success
,
function (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

Create a tagged enum data type, which is a union of Data structs.

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
}

@since2.0.0

@since2.0.0

TaggedEnum
<{
type Loading: {}
Loading
: {}
type Success: {
data: Success;
}
Success
: {
data: Success
data
:
function (type parameter) Success in type RemoteData<Success, Failure>
Success
}
type Failure: {
reason: Failure;
}
Failure
: {
reason: Failure
reason
:
function (type parameter) Failure in type RemoteData<Success, Failure>
Failure
}
}>
// Extend TaggedEnum.WithGenerics to add generics
interface
interface RemoteDataDefinition
RemoteDataDefinition
extends
import Data
Data
.
namespace TaggedEnum

Create a tagged enum data type, which is a union of Data structs.

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
}

@since2.0.0

@since2.0.0

TaggedEnum
.
interface TaggedEnum<A extends Record<string, Record<string, any>> & UntaggedChildren<A>>.WithGenerics<Count extends number>

@since2.0.0

WithGenerics
<2> {
readonly
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"]>
}
// Create constructors for the generic RemoteData
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
} =
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.

@example

import { Data } from "effect"
const { BadRequest, NotFound } = Data.taggedEnum<
| { readonly _tag: "BadRequest"; readonly status: 400; readonly message: string }
| { readonly _tag: "NotFound"; readonly status: 404; readonly message: string }
>()
const notFound = NotFound({ status: 404, message: "Not Found" })

@example

import { Data } from "effect"

type MyResult<E, A> = Data.TaggedEnum<{ Failure: { readonly error: E } Success: { readonly value: A } }> interface MyResultDefinition extends Data.TaggedEnum.WithGenerics<2> { readonly taggedEnum: MyResult<this["A"], this["B"]> } const { Failure, Success } = Data.taggedEnum()

const success = Success({ value: 1 })

@since2.0.0

taggedEnum
<
interface RemoteDataDefinition
RemoteDataDefinition
>()
// Instantiate each case with specific types
const
const loading: {
readonly _tag: "Loading";
}
loading
=
const Loading: <unknown, unknown>(args: void) => {
readonly _tag: "Loading";
}
Loading
()
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
({
reason: string
reason
: "not found" })
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
({
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)

import {
import Data
Data
} from "effect"
// Define a custom error with additional fields
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.

@since2.0.0

Error
<{
message: string
message
: string;
file: string
file
: string }> {}
// Create an instance of the custom error
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.

@since2.0.0

NotFound
({
message: string
message
: "Cannot find this file",
file: string
file
: "foo.txt"
})
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const err: NotFound
err
instanceof
var Error: ErrorConstructor
Error
)
// Output: true
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const err: NotFound
err
.
file: string
file
)
// Output: foo.txt
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 and process.stderr. 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 for more information.

Example using the global console:

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:

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

@seesource

console
.
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) (the arguments are all passed to util.format()).

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() for more information.

@sincev0.1.100

log
(
const err: NotFound
err
)
/*
Output:
NotFound [Error]: Cannot find this file
file: 'foo.txt'
... stack trace ...
*/

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)

import {
import Data
Data
,
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
} from "effect"
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.

@since2.0.0

Error
<{
message: string
message
: string;
file: string
file
: string }> {}
const
const program: Effect.Effect<void, NotFound, never>
program
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

Effect.gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

@example

import { Effect } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, Error> =>
discountRate === 0
? Effect.fail(new Error("Discount rate cannot be zero"))
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function* () {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

gen
(function* () {
yield* new
constructor NotFound<{
message: string;
file: string;
}>(args: {
readonly message: string;
readonly file: string;
}): NotFound

Provides a constructor for a Case Class.

@since2.0.0

NotFound
({
message: string
message
: "Cannot find this file",
file: string
file
: "foo.txt"
})
})
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const runPromise: <void, NotFound>(effect: Effect.Effect<void, NotFound, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<void>

Executes an effect and returns the result as a Promise.

When to Use

Use runPromise when you need to execute an effect and work with the result using Promise syntax, typically for compatibility with other promise-based code.

If the effect succeeds, the promise will resolve with the result. If the effect fails, the promise will reject with an error.

@seerunPromiseExit for a version that returns an Exit type instead of rejecting.

@example

// Title: Running a Successful Effect as a Promise
import { Effect } from "effect"
Effect.runPromise(Effect.succeed(1)).then(console.log)
// Output: 1

@example

//Example: Handling a Failing Effect as a Rejected Promise import { Effect } from "effect"

Effect.runPromise(Effect.fail("my error")).catch(console.error) // Output: // (FiberFailure) Error: my error

@since2.0.0

runPromise
(
const program: Effect.Effect<void, NotFound, never>
program
)
/*
throws:
Error: Cannot find this file
at ... {
name: '(FiberFailure) Error',
[Symbol(effect/Runtime/FiberFailure/Cause)]: {
_tag: 'Fail',
error: NotFound [Error]: Cannot find this file
at ...stack trace...
file: 'foo.txt'
}
}
}
*/

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.

import {
import Data
Data
,
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
,
import Console
Console
} from "effect"
// Define a custom tagged error
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<...>

@since2.0.0

TaggedError
("NotFound")<{
message: string
message
: string
file: string
file
: string
}> {}
const
const program: Effect.Effect<void, never, never>
program
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

Effect.gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

@example

import { Effect } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, Error> =>
discountRate === 0
? Effect.fail(new Error("Discount rate cannot be zero"))
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function* () {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

gen
(function* () {
yield* new
constructor NotFound<{
message: string;
file: string;
}>(args: {
readonly message: string;
readonly file: string;
}): NotFound
NotFound
({
message: string
message
: "Cannot find this file",
file: string
file
: "foo.txt"
})
}).
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
(
// Catch and handle the tagged error
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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)

Catches and handles specific errors by their _tag field, which is used as a discriminator.

When to Use

catchTag is useful when your errors are tagged with a readonly _tag field that identifies the error type. You can use this function to handle specific error types by matching the _tag value. This allows for precise error handling, ensuring that only specific errors are caught and handled.

The error type must have a readonly _tag field to use catchTag. This field is used to identify and match errors.

@seecatchTags for a version that allows you to handle multiple error types at once.

@example

// Title: Handling Errors by Tag
import { Effect, Random } from "effect"
class HttpError {
readonly _tag = "HttpError"
}
class ValidationError {
readonly _tag = "ValidationError"
}
// ┌─── Effect<string, HttpError | ValidationError, never>
// ▼
const program = Effect.gen(function* () {
const n1 = yield* Random.next
const n2 = yield* Random.next
if (n1 < 0.5) {
yield* Effect.fail(new HttpError())
}
if (n2 < 0.5) {
yield* Effect.fail(new ValidationError())
}
return "some result"
})
// ┌─── Effect<string, ValidationError, never>
// ▼
const recovered = program.pipe(
// Only handle HttpError errors
Effect.catchTag("HttpError", (_HttpError) =>
Effect.succeed("Recovering from HttpError")
)
)

@since2.0.0

catchTag
("NotFound", (
err: NotFound
err
) =>
import Console
Console
.
const error: (...args: ReadonlyArray<any>) => Effect.Effect<void>

@since2.0.0

error
(`${
err: NotFound
err
.
message: string
message
} (${
err: NotFound
err
.
file: string
file
})`)
)
)
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<void>

Executes an effect and returns the result as a Promise.

When to Use

Use runPromise when you need to execute an effect and work with the result using Promise syntax, typically for compatibility with other promise-based code.

If the effect succeeds, the promise will resolve with the result. If the effect fails, the promise will reject with an error.

@seerunPromiseExit for a version that returns an Exit type instead of rejecting.

@example

// Title: Running a Successful Effect as a Promise
import { Effect } from "effect"
Effect.runPromise(Effect.succeed(1)).then(console.log)
// Output: 1

@example

//Example: Handling a Failing Effect as a Rejected Promise import { Effect } from "effect"

Effect.runPromise(Effect.fail("my error")).catch(console.error) // Output: // (FiberFailure) Error: my error

@since2.0.0

runPromise
(
const program: Effect.Effect<void, never, never>
program
)
// 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)

import {
import Data
Data
,
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
} from "effect"
// Define an error with a cause property
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.

@since2.0.0

Error
<{
cause: Error
cause
:
interface Error
Error
}> {}
const
const program: Effect.Effect<void, MyError, never>
program
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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)

Provides a way to write effectful code using generator functions, simplifying control flow and error handling.

When to Use

Effect.gen allows you to write code that looks and behaves like synchronous code, but it can handle asynchronous tasks, errors, and complex control flow (like loops and conditions). It helps make asynchronous code more readable and easier to manage.

The generator functions work similarly to async/await but with more explicit control over the execution of effects. You can yield* values from effects and return the final result at the end.

@example

import { Effect } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, Error> =>
discountRate === 0
? Effect.fail(new Error("Discount rate cannot be zero"))
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function* () {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})

@since2.0.0

gen
(function* () {
yield* new
constructor MyError<{
cause: Error;
}>(args: {
readonly cause: Error;
}): MyError

Provides a constructor for a Case Class.

@since2.0.0

MyError
({
cause: Error
cause
: new
var Error: ErrorConstructor
new (message?: string) => Error
Error
("Something went wrong")
})
})
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const runPromise: <void, MyError>(effect: Effect.Effect<void, MyError, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<void>

Executes an effect and returns the result as a Promise.

When to Use

Use runPromise when you need to execute an effect and work with the result using Promise syntax, typically for compatibility with other promise-based code.

If the effect succeeds, the promise will resolve with the result. If the effect fails, the promise will reject with an error.

@seerunPromiseExit for a version that returns an Exit type instead of rejecting.

@example

// Title: Running a Successful Effect as a Promise
import { Effect } from "effect"
Effect.runPromise(Effect.succeed(1)).then(console.log)
// Output: 1

@example

//Example: Handling a Failing Effect as a Rejected Promise import { Effect } from "effect"

Effect.runPromise(Effect.fail("my error")).catch(console.error) // Output: // (FiberFailure) Error: my error

@since2.0.0

runPromise
(
const program: Effect.Effect<void, MyError, never>
program
)
/*
throws:
Error: An error has occurred
at ... {
name: '(FiberFailure) Error',
[Symbol(effect/Runtime/FiberFailure/Cause)]: {
_tag: 'Fail',
error: MyError
at ...
[cause]: Error: Something went wrong
at ...
*/