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()

// 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()

// 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()

// 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()

// 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()

// 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()

// 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")

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