Skip to content

Option

The Option data type represents optional values. An Option<A> can either be Some<A>, containing a value of type A, or None, representing the absence of a value.

You can use Option in scenarios like:

  • Using it for initial values
  • Returning values from functions that are not defined for all possible inputs (referred to as “partial functions”)
  • Managing optional fields in data structures
  • Handling optional function arguments

Use the Option.some constructor to create an Option that holds a value of type A.

Example (Creating an Option with a Value)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// An Option holding the number 1
const
const value: Option.Option<number>
value
=
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1)
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 value: Option.Option<number>
value
)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }

Use the Option.none constructor to create an Option representing the absence of a value.

Example (Creating an Option with No Value)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// An Option holding no value
const
const noValue: Option.Option<never>
noValue
=
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <never>() => Option.Option<never>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
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 noValue: Option.Option<never>
noValue
)
// Output: { _id: 'Option', _tag: 'None' }

You can create an Option based on a predicate, for example, to check if a value is positive.

Example (Using Explicit Option Creation)

Here’s how you can achieve this using Option.none and Option.some:

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const isPositive: (n: number) => boolean
isPositive
= (
n: number
n
: number) =>
n: number
n
> 0
const
const parsePositive: (n: number) => Option.Option<number>
parsePositive
= (
n: number
n
: number):
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =>
const isPositive: (n: number) => boolean
isPositive
(
n: number
n
) ?
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(
n: number
n
) :
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <number>() => Option.Option<number>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()

Example (Using Option.liftPredicate for Conciseness)

Alternatively, you can simplify the above logic with Option.liftPredicate:

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const isPositive: (n: number) => boolean
isPositive
= (
n: number
n
: number) =>
n: number
n
> 0
// ┌─── (b: number) => Option<number>
// ▼
const
const parsePositive: (b: number) => Option.Option<number>
parsePositive
=
import Option

@since2.0.0

@since2.0.0

Option
.
const liftPredicate: <number, number>(predicate: Predicate<number>) => (b: number) => Option.Option<number> (+3 overloads)

Transforms a Predicate function into a Some of the input value if the predicate returns true or None if the predicate returns false.

@parampredicate - A Predicate function that takes in a value of type A and returns a boolean.

@example

import { Option } from "effect"
const getOption = Option.liftPredicate((n: number) => n >= 0)
assert.deepStrictEqual(getOption(-1), Option.none())
assert.deepStrictEqual(getOption(1), Option.some(1))

@since2.0.0

liftPredicate
(
const isPositive: (n: number) => boolean
isPositive
)

Consider a User model where the "email" property is optional and can hold a string value. We use the Option<string> type to represent this optional property:

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
interface
interface User
User
{
readonly
User.id: number
id
: number
readonly
User.username: string
username
: string
readonly
User.email: Option.Option<string>
email
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>
}

Here are examples of how to create User instances with and without an email:

Example (Creating Users with and without Email)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
interface
interface User
User
{
readonly
User.id: number
id
: number
readonly
User.username: string
username
: string
readonly
User.email: Option.Option<string>
email
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>
}
const
const withEmail: User
withEmail
:
interface User
User
= {
User.id: number
id
: 1,
User.username: string
username
: "john_doe",
User.email: Option.Option<string>
email
:
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("john.doe@example.com")
}
const
const withoutEmail: User
withoutEmail
:
interface User
User
= {
User.id: number
id
: 2,
User.username: string
username
: "jane_doe",
User.email: Option.Option<string>
email
:
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <string>() => Option.Option<string>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
}

You can check whether an Option is a Some or a None using the Option.isSome and Option.isNone guards.

Example (Using Guards to Check Option Values)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const foo: Option.Option<number>
foo
=
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1)
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 Option

@since2.0.0

@since2.0.0

Option
.
const isSome: <number>(self: Option.Option<number>) => self is Option.Some<number>

Determine if a Option is a Some.

@paramself - The Option to check.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.isSome(Option.some(1)), true)
assert.deepStrictEqual(Option.isSome(Option.none()), false)

@since2.0.0

isSome
(
const foo: Option.Option<number>
foo
))
// Output: true
if (
import Option

@since2.0.0

@since2.0.0

Option
.
const isNone: <number>(self: Option.Option<number>) => self is Option.None<number>

Determine if a Option is a None.

@paramself - The Option to check.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.isNone(Option.some(1)), false)
assert.deepStrictEqual(Option.isNone(Option.none()), true)

@since2.0.0

isNone
(
const foo: Option.Option<number>
foo
)) {
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
("Option is empty")
} else {
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
(`Option has a value: ${
const foo: Option.Some<number>
foo
.
Some<number>.value: number
value
}`)
}
// Output: "Option has a value: 1"

Use Option.match to handle both cases of an Option by specifying separate callbacks for None and Some.

Example (Pattern Matching with Option)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const foo: Option.Option<number>
foo
=
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1)
const
const message: string
message
=
import Option

@since2.0.0

@since2.0.0

Option
.
const match: <number, string, string>(self: Option.Option<number>, options: {
readonly onNone: LazyArg<string>;
readonly onSome: (a: number) => string;
}) => string (+1 overload)

Matches the given Option and returns either the provided onNone value or the result of the provided onSome function when passed the Option's value.

@paramself - The Option to match

@paramonNone - The value to be returned if the Option is None

@paramonSome - The function to be called if the Option is Some, it will be passed the Option's value and its result will be returned

@example

import { pipe, Option } from "effect"
assert.deepStrictEqual(
pipe(Option.some(1), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
'a some containing 1'
)
assert.deepStrictEqual(
pipe(Option.none(), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
'a none'
)

@since2.0.0

match
(
const foo: Option.Option<number>
foo
, {
onNone: LazyArg<string>
onNone
: () => "Option is empty",
onSome: (a: number) => string
onSome
: (
value: number
value
) => `Option has a value: ${
value: number
value
}`
})
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 message: string
message
)
// Output: "Option has a value: 1"

The Option.map function lets you transform the value inside an Option without manually unwrapping and re-wrapping it. If the Option holds a value (Some), the transformation function is applied. If the Option is None, the function is ignored, and the Option remains unchanged.

Example (Mapping a Value in Some)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// Transform the value inside Some
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 Option

@since2.0.0

@since2.0.0

Option
.
const map: <number, number>(self: Option.Option<number>, f: (a: number) => number) => Option.Option<number> (+1 overload)

Maps the Some side of an Option value to a new Option value.

@paramself - An Option to map

@paramf - The function to map over the value of the Option

@since2.0.0

map
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1), (
n: number
n
) =>
n: number
n
+ 1))
// Output: { _id: 'Option', _tag: 'Some', value: 2 }

When dealing with None, the mapping function is not executed, and the Option remains None:

Example (Mapping over None)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// Mapping over None results in None
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 Option

@since2.0.0

@since2.0.0

Option
.
const map: <never, number>(self: Option.Option<never>, f: (a: never) => number) => Option.Option<number> (+1 overload)

Maps the Some side of an Option value to a new Option value.

@paramself - An Option to map

@paramf - The function to map over the value of the Option

@since2.0.0

map
(
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <never>() => Option.Option<never>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
(), (
n: never
n
) =>
n: never
n
+ 1))
// Output: { _id: 'Option', _tag: 'None' }

The Option.flatMap function is similar to Option.map, but it is designed to handle cases where the transformation might return another Option. This allows us to chain computations that depend on whether or not a value is present in an Option.

Consider a User model that includes a nested optional Address, which itself contains an optional street property:

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
interface
interface User
User
{
readonly
User.id: number
id
: number
readonly
User.username: string
username
: string
readonly
User.email: Option.Option<string>
email
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>
readonly
User.address: Option.Option<Address>
address
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<
interface Address
Address
>
}
interface
interface Address
Address
{
readonly
Address.city: string
city
: string
readonly
Address.street: Option.Option<string>
street
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>
}

In this model, the address field is an Option<Address>, and the street field within Address is an Option<string>.

We can use Option.flatMap to extract the street property from address:

Example (Extracting a Nested Optional Property)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
interface
interface Address
Address
{
readonly
Address.city: string
city
: string
readonly
Address.street: Option.Option<string>
street
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>
}
interface
interface User
User
{
readonly
User.id: number
id
: number
readonly
User.username: string
username
: string
readonly
User.email: Option.Option<string>
email
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>
readonly
User.address: Option.Option<Address>
address
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<
interface Address
Address
>
}
const
const user: User
user
:
interface User
User
= {
User.id: number
id
: 1,
User.username: string
username
: "john_doe",
User.email: Option.Option<string>
email
:
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("john.doe@example.com"),
User.address: Option.Option<Address>
address
:
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <{
city: string;
street: Option.Option<string>;
}>(value: {
city: string;
street: Option.Option<string>;
}) => Option.Option<{
city: string;
street: Option.Option<string>;
}>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
({
city: string
city
: "New York",
street: Option.Option<string>
street
:
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("123 Main St")
})
}
// Use flatMap to extract the street value
const
const street: Option.Option<string>
street
=
const user: User
user
.
User.address: Option.Option<Address>
address
.
Pipeable.pipe<Option.Option<Address>, Option.Option<string>>(this: Option.Option<Address>, ab: (_: Option.Option<Address>) => Option.Option<string>): Option.Option<...> (+21 overloads)
pipe
(
import Option

@since2.0.0

@since2.0.0

Option
.
const flatMap: <Address, string>(f: (a: Address) => Option.Option<string>) => (self: Option.Option<Address>) => Option.Option<string> (+1 overload)

Applies a function to the value of an Option and flattens the result, if the input is Some.

@since2.0.0

flatMap
((
address: Address
address
) =>
address: Address
address
.
Address.street: Option.Option<string>
street
)
)
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 street: Option.Option<string>
street
)
// Output: { _id: 'Option', _tag: 'Some', value: '123 Main St' }

If user.address is Some, Option.flatMap applies the function (address) => address.street to retrieve the street value.

If user.address is None, the function is not executed, and street remains None.

This approach lets us handle nested optional values concisely, avoiding manual checks and making the code cleaner and easier to read.

The Option.filter function allows you to filter an Option based on a given predicate. If the predicate is not met or if the Option is None, the result will be None.

Example (Filtering an Option Value)

Here’s how you can simplify some code using Option.filter for a more idiomatic approach:

Original Code

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// Function to remove empty strings from an Option
const
const removeEmptyString: (input: Option.Option<string>) => Option.None<string> | Option.Some<string>
removeEmptyString
= (
input: Option.Option<string>
input
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>) => {
if (
import Option

@since2.0.0

@since2.0.0

Option
.
const isSome: <string>(self: Option.Option<string>) => self is Option.Some<string>

Determine if a Option is a Some.

@paramself - The Option to check.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.isSome(Option.some(1)), true)
assert.deepStrictEqual(Option.isSome(Option.none()), false)

@since2.0.0

isSome
(
input: Option.Option<string>
input
) &&
input: Option.Some<string>
input
.
Some<string>.value: string
value
=== "") {
return
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <never>() => Option.Option<never>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
() // Return None if the value is an empty string
}
return
input: Option.Option<string>
input
// Otherwise, return the original Option
}
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 removeEmptyString: (input: Option.Option<string>) => Option.None<string> | Option.Some<string>
removeEmptyString
(
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <string>() => Option.Option<string>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()))
// Output: { _id: 'Option', _tag: 'None' }
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 removeEmptyString: (input: Option.Option<string>) => Option.None<string> | Option.Some<string>
removeEmptyString
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("")))
// Output: { _id: 'Option', _tag: 'None' }
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 removeEmptyString: (input: Option.Option<string>) => Option.None<string> | Option.Some<string>
removeEmptyString
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("a")))
// Output: { _id: 'Option', _tag: 'Some', value: 'a' }

Refactored Idiomatic Code

Using Option.filter, we can write the same logic more concisely:

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const removeEmptyString: (input: Option.Option<string>) => Option.Option<string>
removeEmptyString
= (
input: Option.Option<string>
input
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string>) =>
import Option

@since2.0.0

@since2.0.0

Option
.
const filter: <string>(self: Option.Option<string>, predicate: Predicate<string>) => Option.Option<string> (+3 overloads)

Filters an Option using a predicate. If the predicate is not satisfied or the Option is None returns None.

If you need to change the type of the Option in addition to filtering, see filterMap.

@parampredicate - A predicate function to apply to the Option value.

@paramfb - The Option to filter.

@example

import { Option } from "effect"
// predicate
const isEven = (n: number) => n % 2 === 0
assert.deepStrictEqual(Option.filter(Option.none(), isEven), Option.none())
assert.deepStrictEqual(Option.filter(Option.some(3), isEven), Option.none())
assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
// refinement
const isNumber = (v: unknown): v is number => typeof v === "number"
assert.deepStrictEqual(Option.filter(Option.none(), isNumber), Option.none())
assert.deepStrictEqual(Option.filter(Option.some('hello'), isNumber), Option.none())
assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))

@since2.0.0

filter
(
input: Option.Option<string>
input
, (
value: string
value
) =>
value: string
value
!== "")
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 removeEmptyString: (input: Option.Option<string>) => Option.Option<string>
removeEmptyString
(
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <string>() => Option.Option<string>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()))
// Output: { _id: 'Option', _tag: 'None' }
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 removeEmptyString: (input: Option.Option<string>) => Option.Option<string>
removeEmptyString
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("")))
// Output: { _id: 'Option', _tag: 'None' }
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 removeEmptyString: (input: Option.Option<string>) => Option.Option<string>
removeEmptyString
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("a")))
// Output: { _id: 'Option', _tag: 'Some', value: 'a' }

To retrieve the value stored inside an Option, you can use several helper functions provided by the Option module. Here’s an overview of the available methods:

This function extracts the value from a Some. If the Option is None, it throws an error.

Example (Retrieving Value or Throwing an Error)

import {
import Option

@since2.0.0

@since2.0.0

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

@since2.0.0

@since2.0.0

Option
.
const getOrThrow: <number>(self: Option.Option<number>) => number

Extracts the value of an Option or throws if the Option is None.

The thrown error is a default error. To configure the error thrown, see

getOrThrowWith

.

@paramself - The Option to extract the value from.

@throwsError("getOrThrow called on a None")

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.getOrThrow(Option.some(1)), 1)
assert.throws(() => Option.getOrThrow(Option.none()))

@since2.0.0

getOrThrow
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(10)))
// Output: 10
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 Option

@since2.0.0

@since2.0.0

Option
.
const getOrThrow: <any>(self: Option.Option<any>) => any

Extracts the value of an Option or throws if the Option is None.

The thrown error is a default error. To configure the error thrown, see

getOrThrowWith

.

@paramself - The Option to extract the value from.

@throwsError("getOrThrow called on a None")

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.getOrThrow(Option.some(1)), 1)
assert.throws(() => Option.getOrThrow(Option.none()))

@since2.0.0

getOrThrow
(
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <any>() => Option.Option<any>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()))
// throws: Error: getOrThrow called on a None

These functions convert a None to either null or undefined, which is useful when working with non-Option-based code.

Example (Converting None to null or undefined)

import {
import Option

@since2.0.0

@since2.0.0

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

@since2.0.0

@since2.0.0

Option
.
const getOrNull: <number>(self: Option.Option<number>) => number | null

Returns the value of the Option if it is a Some, otherwise returns null.

@paramself - The Option to extract the value from.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.getOrNull(Option.some(1)), 1)
assert.deepStrictEqual(Option.getOrNull(Option.none()), null)

@since2.0.0

getOrNull
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(5)))
// Output: 5
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 Option

@since2.0.0

@since2.0.0

Option
.
const getOrNull: <any>(self: Option.Option<any>) => any

Returns the value of the Option if it is a Some, otherwise returns null.

@paramself - The Option to extract the value from.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.getOrNull(Option.some(1)), 1)
assert.deepStrictEqual(Option.getOrNull(Option.none()), null)

@since2.0.0

getOrNull
(
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <any>() => Option.Option<any>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()))
// Output: 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 Option

@since2.0.0

@since2.0.0

Option
.
const getOrUndefined: <number>(self: Option.Option<number>) => number | undefined

Returns the value of the Option if it is a Some, otherwise returns undefined.

@paramself - The Option to extract the value from.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.getOrUndefined(Option.some(1)), 1)
assert.deepStrictEqual(Option.getOrUndefined(Option.none()), undefined)

@since2.0.0

getOrUndefined
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(5)))
// Output: 5
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 Option

@since2.0.0

@since2.0.0

Option
.
const getOrUndefined: <any>(self: Option.Option<any>) => any

Returns the value of the Option if it is a Some, otherwise returns undefined.

@paramself - The Option to extract the value from.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.getOrUndefined(Option.some(1)), 1)
assert.deepStrictEqual(Option.getOrUndefined(Option.none()), undefined)

@since2.0.0

getOrUndefined
(
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <any>() => Option.Option<any>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()))
// Output: undefined

This function allows you to specify a default value to return when the Option is None.

Example (Providing a Default Value When None)

import {
import Option

@since2.0.0

@since2.0.0

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

@since2.0.0

@since2.0.0

Option
.
const getOrElse: <number, number>(self: Option.Option<number>, onNone: LazyArg<number>) => number (+1 overload)

Returns the value of the Option if it is Some, otherwise returns onNone

@paramself - The Option to get the value of.

@paramonNone - Function that returns the default value to return if the Option is None.

@example

import { pipe, Option } from "effect"
assert.deepStrictEqual(pipe(Option.some(1), Option.getOrElse(() => 0)), 1)
assert.deepStrictEqual(pipe(Option.none(), Option.getOrElse(() => 0)), 0)

@since2.0.0

getOrElse
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(5), () => 0))
// Output: 5
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 Option

@since2.0.0

@since2.0.0

Option
.
const getOrElse: <any, number>(self: Option.Option<any>, onNone: LazyArg<number>) => any (+1 overload)

Returns the value of the Option if it is Some, otherwise returns onNone

@paramself - The Option to get the value of.

@paramonNone - Function that returns the default value to return if the Option is None.

@example

import { pipe, Option } from "effect"
assert.deepStrictEqual(pipe(Option.some(1), Option.getOrElse(() => 0)), 1)
assert.deepStrictEqual(pipe(Option.none(), Option.getOrElse(() => 0)), 0)

@since2.0.0

getOrElse
(
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <any>() => Option.Option<any>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
(), () => 0))
// Output: 0

When a computation returns None, you might want to try an alternative computation that yields an Option. The Option.orElse function is helpful in such cases. It lets you chain multiple computations, moving on to the next if the current one results in None. This approach is often used in retry logic, attempting computations until one succeeds or all possibilities are exhausted.

Example (Attempting Alternative Computations)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// Simulating a computation that may or may not produce a result
const
const computation: () => Option.Option<number>
computation
= ():
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =>
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
() < 0.5 ?
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(10) :
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <number>() => Option.Option<number>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
// Simulates an alternative computation
const
const alternativeComputation: () => Option.Option<number>
alternativeComputation
= ():
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =>
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
() < 0.5 ?
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(20) :
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <number>() => Option.Option<number>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
// Attempt the first computation, then try an alternative if needed
const
const program: Option.Option<number>
program
=
const computation: () => Option.Option<number>
computation
().
Pipeable.pipe<Option.Option<number>, Option.Option<number>>(this: Option.Option<number>, ab: (_: Option.Option<number>) => Option.Option<number>): Option.Option<...> (+21 overloads)
pipe
(
import Option

@since2.0.0

@since2.0.0

Option
.
const orElse: <number>(that: LazyArg<Option.Option<number>>) => <A>(self: Option.Option<A>) => Option.Option<number | A> (+1 overload)

Returns the provided Option that if self is None, otherwise returns self.

@paramself - The first Option to be checked.

@paramthat - The Option to return if self is None.

@example

import { pipe, Option } from "effect"
assert.deepStrictEqual(
pipe(
Option.none(),
Option.orElse(() => Option.none())
),
Option.none()
)
assert.deepStrictEqual(
pipe(
Option.some('a'),
Option.orElse(() => Option.none())
),
Option.some('a')
)
assert.deepStrictEqual(
pipe(
Option.none(),
Option.orElse(() => Option.some('b'))
),
Option.some('b')
)
assert.deepStrictEqual(
pipe(
Option.some('a'),
Option.orElse(() => Option.some('b'))
),
Option.some('a')
)

@since2.0.0

orElse
(() =>
const alternativeComputation: () => Option.Option<number>
alternativeComputation
())
)
const
const result: string
result
=
import Option

@since2.0.0

@since2.0.0

Option
.
const match: <number, string, string>(self: Option.Option<number>, options: {
readonly onNone: LazyArg<string>;
readonly onSome: (a: number) => string;
}) => string (+1 overload)

Matches the given Option and returns either the provided onNone value or the result of the provided onSome function when passed the Option's value.

@paramself - The Option to match

@paramonNone - The value to be returned if the Option is None

@paramonSome - The function to be called if the Option is Some, it will be passed the Option's value and its result will be returned

@example

import { pipe, Option } from "effect"
assert.deepStrictEqual(
pipe(Option.some(1), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
'a some containing 1'
)
assert.deepStrictEqual(
pipe(Option.none(), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
'a none'
)

@since2.0.0

match
(
const program: Option.Option<number>
program
, {
onNone: LazyArg<string>
onNone
: () => "Both computations resulted in None",
// At least one computation succeeded
onSome: (a: number) => string
onSome
: (
value: number
value
) => `Computed value: ${
value: number
value
}`
})
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 result: string
result
)
// Output: Computed value: 10

You can also use Option.firstSomeOf to get the first Some value from an iterable of Option values:

Example (Retrieving the First Some Value)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const first: Option.Option<number>
first
=
import Option

@since2.0.0

@since2.0.0

Option
.
const firstSomeOf: <unknown, (Option.None<number> | Option.Some<number>)[]>(collection: (Option.None<number> | Option.Some<number>)[]) => Option.Option<number>

Given an Iterable collection of Options, returns the first Some found in the collection.

@paramcollection - An iterable collection of Option to be searched.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.firstSomeOf([Option.none(), Option.some(1), Option.some(2)]), Option.some(1))

@since2.0.0

firstSomeOf
([
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <never>() => Option.Option<never>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
(),
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(2),
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <never>() => Option.Option<never>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
(),
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(3)
])
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 first: Option.Option<number>
first
)
// Output: { _id: 'Option', _tag: 'Some', value: 2 }

When dealing with the Option data type, you may encounter code that uses undefined or null to represent optional values. The Option module provides several APIs to make interaction with these nullable types straightforward.

Option.fromNullable converts a nullable value (null or undefined) into an Option. If the value is null or undefined, it returns Option.none(). Otherwise, it wraps the value in Option.some().

Example (Creating Option from Nullable Values)

import {
import Option

@since2.0.0

@since2.0.0

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

@since2.0.0

@since2.0.0

Option
.
const fromNullable: <null>(nullableValue: null) => Option.Option<never>

Constructs a new Option from a nullable type. If the value is null or undefined, returns None, otherwise returns the value wrapped in a Some.

@paramnullableValue - The nullable value to be converted to an Option.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.fromNullable(undefined), Option.none())
assert.deepStrictEqual(Option.fromNullable(null), Option.none())
assert.deepStrictEqual(Option.fromNullable(1), Option.some(1))

@since2.0.0

fromNullable
(null))
// Output: { _id: 'Option', _tag: 'None' }
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 Option

@since2.0.0

@since2.0.0

Option
.
const fromNullable: <undefined>(nullableValue: undefined) => Option.Option<never>

Constructs a new Option from a nullable type. If the value is null or undefined, returns None, otherwise returns the value wrapped in a Some.

@paramnullableValue - The nullable value to be converted to an Option.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.fromNullable(undefined), Option.none())
assert.deepStrictEqual(Option.fromNullable(null), Option.none())
assert.deepStrictEqual(Option.fromNullable(1), Option.some(1))

@since2.0.0

fromNullable
(
var undefined
undefined
))
// Output: { _id: 'Option', _tag: 'None' }
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 Option

@since2.0.0

@since2.0.0

Option
.
const fromNullable: <number>(nullableValue: number) => Option.Option<number>

Constructs a new Option from a nullable type. If the value is null or undefined, returns None, otherwise returns the value wrapped in a Some.

@paramnullableValue - The nullable value to be converted to an Option.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.fromNullable(undefined), Option.none())
assert.deepStrictEqual(Option.fromNullable(null), Option.none())
assert.deepStrictEqual(Option.fromNullable(1), Option.some(1))

@since2.0.0

fromNullable
(1))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }

If you need to convert an Option back to a nullable value, there are two helper methods:

  • Option.getOrNull: Converts None to null.
  • Option.getOrUndefined: Converts None to undefined.

The Option type works as a subtype of the Effect type, allowing you to use it with functions from the Effect module. While these functions are built to handle Effect values, they can also manage Option values correctly.

Option VariantMapped to EffectDescription
NoneEffect<never, NoSuchElementException>Represents the absence of a value
Some<A>Effect<A>Represents the presence of a value

Example (Combining Option with Effect)

import {
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
,
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
// Function to get the head of an array, returning Option
const
const head: <A>(array: ReadonlyArray<A>) => Option.Option<A>
head
= <
function (type parameter) A in <A>(array: ReadonlyArray<A>): Option.Option<A>
A
>(
array: readonly A[]
array
:
interface ReadonlyArray<T>
ReadonlyArray
<
function (type parameter) A in <A>(array: ReadonlyArray<A>): Option.Option<A>
A
>):
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<
function (type parameter) A in <A>(array: ReadonlyArray<A>): Option.Option<A>
A
> =>
array: readonly A[]
array
.
ReadonlyArray<A>.length: number

Gets the length of the array. This is a number one higher than the highest element defined in an array.

length
> 0 ?
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <A>(value: A) => Option.Option<A>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(
array: readonly A[]
array
[0]) :
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <A>() => Option.Option<A>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
// Simulated fetch function that returns Effect
const
const fetchData: () => Effect.Effect<string, string>
fetchData
= ():
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
interface Effect<out A, out E = never, out R = never>

The Effect interface defines a value that lazily describes a workflow or job. The workflow requires some context R, and may fail with an error of type E, or succeed with a value of type A.

Effect values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability.

To run an Effect value, you need a Runtime, which is a type that is capable of executing Effect values.

@since2.0.0

@since2.0.0

Effect
<string, string> => {
const
const success: boolean
success
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
() > 0.5
return
const success: boolean
success
?
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>

Creates an Effect that always succeeds with a given value.

When to Use

Use this function when you need an effect that completes successfully with a specific value without any errors or external dependencies.

@seefail to create an effect that represents a failure.

@example

// Title: Creating a Successful Effect
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)

@since2.0.0

succeed
("some data")
:
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>

Creates an Effect that represents a recoverable error.

When to Use

Use this function to explicitly signal an error in an Effect. The error will keep propagating unless it is handled. You can handle the error with functions like

catchAll

or

catchTag

.

@seesucceed to create an effect that represents a successful value.

@example

// Title: Creating a Failed Effect
import { Effect } from "effect"
// ┌─── Effect<never, Error, never>
// ▼
const failure = Effect.fail(
new Error("Operation failed due to network error")
)

@since2.0.0

fail
("Failed to fetch data")
}
// Mixing Either and Effect
const
const program: Effect.Effect<[number, string], string | NoSuchElementException, never>
program
=
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const all: <readonly [Option.Option<number>, Effect.Effect<string, string, never>], {
readonly concurrency?: Concurrency | undefined;
readonly batching?: boolean | "inherit" | undefined;
readonly discard?: boolean | undefined;
readonly mode?: "default" | "validate" | "either" | undefined;
readonly concurrentFinalizers?: boolean | undefined;
}>(arg: readonly [...], options?: {
readonly concurrency?: Concurrency | undefined;
readonly batching?: boolean | "inherit" | undefined;
readonly discard?: boolean | undefined;
readonly mode?: "default" | "validate" | "either" | undefined;
readonly concurrentFinalizers?: boolean | undefined;
} | undefined) => Effect.Effect<...>

Combines multiple effects into one, returning results based on the input structure.

When to Use

Use Effect.all when you need to run multiple effects and combine their results into a single output. It supports tuples, iterables, structs, and records, making it flexible for different input types.

For instance, if the input is a tuple:

// ┌─── a tuple of effects
// ▼
Effect.all([effect1, effect2, ...])

the effects are executed sequentially, and the result is a new effect containing the results as a tuple. The results in the tuple match the order of the effects passed to Effect.all.

Concurrency

You can control the execution order (e.g., sequential vs. concurrent) using the concurrency option.

Short-Circuiting Behavior

The Effect.all function stops execution on the first error it encounters, this is called "short-circuiting". If any effect in the collection fails, the remaining effects will not run, and the error will be propagated. To change this behavior, you can use the mode option, which allows all effects to run and collect results as Either or Option.

The mode option

The { mode: "either" } option changes the behavior of Effect.all to ensure all effects run, even if some fail. Instead of stopping on the first failure, this mode collects both successes and failures, returning an array of Either instances where each result is either a Right (success) or a Left (failure).

Similarly, the { mode: "validate" } option uses Option to indicate success or failure. Each effect returns None for success and Some with the error for failure.

@seeforEach for iterating over elements and applying an effect.

@example

// Title: Combining Effects in Tuples
import { Effect, Console } from "effect"
const tupleOfEffects = [
Effect.succeed(42).pipe(Effect.tap(Console.log)),
Effect.succeed("Hello").pipe(Effect.tap(Console.log))
] as const
// ┌─── Effect<[number, string], never, never>
// ▼
const resultsAsTuple = Effect.all(tupleOfEffects)
Effect.runPromise(resultsAsTuple).then(console.log)
// Output:
// 42
// Hello
// [ 42, 'Hello' ]

@example

// Title: Combining Effects in Iterables import { Effect, Console } from "effect"

const iterableOfEffects: Iterable<Effect.Effect> = [1, 2, 3].map( (n) => Effect.succeed(n).pipe(Effect.tap(Console.log)) )

// ┌─── Effect<number[], never, never> // ▼ const resultsAsArray = Effect.all(iterableOfEffects)

Effect.runPromise(resultsAsArray).then(console.log) // Output: // 1 // 2 // 3 // [ 1, 2, 3 ]

@example

// Title: Combining Effects in Structs import { Effect, Console } from "effect"

const structOfEffects = { a: Effect.succeed(42).pipe(Effect.tap(Console.log)), b: Effect.succeed("Hello").pipe(Effect.tap(Console.log)) }

// ┌─── Effect<{ a: number; b: string; }, never, never> // ▼ const resultsAsStruct = Effect.all(structOfEffects)

Effect.runPromise(resultsAsStruct).then(console.log) // Output: // 42 // Hello // { a: 42, b: 'Hello' }

@example

// Title: Combining Effects in Records import { Effect, Console } from "effect"

const recordOfEffects: Record<string, Effect.Effect> = { key1: Effect.succeed(1).pipe(Effect.tap(Console.log)), key2: Effect.succeed(2).pipe(Effect.tap(Console.log)) }

// ┌─── Effect<{ [x: string]: number; }, never, never> // ▼ const resultsAsRecord = Effect.all(recordOfEffects)

Effect.runPromise(resultsAsRecord).then(console.log) // Output: // 1 // 2 // { key1: 1, key2: 2 }

@example

// Title: Short-Circuiting Behavior import { Effect, Console } from "effect"

const program = Effect.all([ Effect.succeed("Task1").pipe(Effect.tap(Console.log)), Effect.fail("Task2: Oh no!").pipe(Effect.tap(Console.log)), // Won't execute due to earlier failure Effect.succeed("Task3").pipe(Effect.tap(Console.log)) ])

Effect.runPromiseExit(program).then(console.log) // Output: // Task1 // { // _id: 'Exit', // _tag: 'Failure', // cause: { _id: 'Cause', _tag: 'Fail', failure: 'Task2: Oh no!' } // }

@example

// Title: Collecting Results with mode: "either" import { Effect, Console } from "effect"

const effects = [ Effect.succeed("Task1").pipe(Effect.tap(Console.log)), Effect.fail("Task2: Oh no!").pipe(Effect.tap(Console.log)), Effect.succeed("Task3").pipe(Effect.tap(Console.log)) ]

const program = Effect.all(effects, { mode: "either" })

Effect.runPromiseExit(program).then(console.log) // Output: // Task1 // Task3 // { // _id: 'Exit', // _tag: 'Success', // value: [ // { _id: 'Either', _tag: 'Right', right: 'Task1' }, // { _id: 'Either', _tag: 'Left', left: 'Task2: Oh no!' }, // { _id: 'Either', _tag: 'Right', right: 'Task3' } // ] // }

@example

//Example: Collecting Results with mode: "validate" import { Effect, Console } from "effect"

const effects = [ Effect.succeed("Task1").pipe(Effect.tap(Console.log)), Effect.fail("Task2: Oh no!").pipe(Effect.tap(Console.log)), Effect.succeed("Task3").pipe(Effect.tap(Console.log)) ]

const program = Effect.all(effects, { mode: "validate" })

Effect.runPromiseExit(program).then((result) => console.log("%o", result)) // Output: // Task1 // Task3 // { // _id: 'Exit', // _tag: 'Failure', // cause: { // _id: 'Cause', // _tag: 'Fail', // failure: [ // { _id: 'Option', _tag: 'None' }, // { _id: 'Option', _tag: 'Some', value: 'Task2: Oh no!' }, // { _id: 'Option', _tag: 'None' } // ] // } // }

@since2.0.0

all
([
const head: <number>(array: readonly number[]) => Option.Option<number>
head
([1, 2, 3]),
const fetchData: () => Effect.Effect<string, string>
fetchData
()])
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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

Executes an effect and returns the result as a Promise.

When to Use

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

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

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

@example

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

@example

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

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

@since2.0.0

runPromise
(
const program: Effect.Effect<[number, string], string | NoSuchElementException, never>
program
).
Promise<[number, string]>.then<void, never>(onfulfilled?: ((value: [number, string]) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

@paramonfulfilled The callback to execute when the Promise is resolved.

@paramonrejected The callback to execute when the Promise is rejected.

@returnsA Promise for the completion of which ever callback is executed.

then
(
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
)
/*
Example Output:
[ 1, 'some data' ]
*/

The Option.zipWith function lets you combine two Option values using a provided function. It creates a new Option that holds the combined value of both original Option values.

Example (Combining Two Options into an Object)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const maybeName: Option.Option<string>
maybeName
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("John")
const
const maybeAge: Option.Option<number>
maybeAge
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(25)
// Combine the name and age into a person object
const
const person: Option.Option<{
name: string;
age: number;
}>
person
=
import Option

@since2.0.0

@since2.0.0

Option
.
const zipWith: <string, number, {
name: string;
age: number;
}>(self: Option.Option<string>, that: Option.Option<number>, f: (a: string, b: number) => {
name: string;
age: number;
}) => Option.Option<{
name: string;
age: number;
}> (+1 overload)

Zips two Option values together using a provided function, returning a new Option of the result.

@paramself - The left-hand side of the zip operation

@paramthat - The right-hand side of the zip operation

@paramf - The function used to combine the values of the two Options

@example

import { Option } from "effect"
type Complex = [real: number, imaginary: number]
const complex = (real: number, imaginary: number): Complex => [real, imaginary]
assert.deepStrictEqual(Option.zipWith(Option.none(), Option.none(), complex), Option.none())
assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.none(), complex), Option.none())
assert.deepStrictEqual(Option.zipWith(Option.none(), Option.some(1), complex), Option.none())
assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.some(2), complex), Option.some([1, 2]))
assert.deepStrictEqual(Option.zipWith(Option.some(1), complex)(Option.some(2)), Option.some([2, 1]))

@since2.0.0

zipWith
(
const maybeName: Option.Option<string>
maybeName
,
const maybeAge: Option.Option<number>
maybeAge
, (
name: string
name
,
age: number
age
) => ({
name: string
name
:
name: string
name
.
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
(),
age: number
age
}))
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 person: Option.Option<{
name: string;
age: number;
}>
person
)
/*
Output:
{ _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
*/

If either of the Option values is None, the result will be None:

Example (Handling None Values)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const maybeName: Option.Option<string>
maybeName
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("John")
const
const maybeAge: Option.Option<number>
maybeAge
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <number>() => Option.Option<number>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
// Since maybeAge is a None, the result will also be None
const
const person: Option.Option<{
name: string;
age: number;
}>
person
=
import Option

@since2.0.0

@since2.0.0

Option
.
const zipWith: <string, number, {
name: string;
age: number;
}>(self: Option.Option<string>, that: Option.Option<number>, f: (a: string, b: number) => {
name: string;
age: number;
}) => Option.Option<{
name: string;
age: number;
}> (+1 overload)

Zips two Option values together using a provided function, returning a new Option of the result.

@paramself - The left-hand side of the zip operation

@paramthat - The right-hand side of the zip operation

@paramf - The function used to combine the values of the two Options

@example

import { Option } from "effect"
type Complex = [real: number, imaginary: number]
const complex = (real: number, imaginary: number): Complex => [real, imaginary]
assert.deepStrictEqual(Option.zipWith(Option.none(), Option.none(), complex), Option.none())
assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.none(), complex), Option.none())
assert.deepStrictEqual(Option.zipWith(Option.none(), Option.some(1), complex), Option.none())
assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.some(2), complex), Option.some([1, 2]))
assert.deepStrictEqual(Option.zipWith(Option.some(1), complex)(Option.some(2)), Option.some([2, 1]))

@since2.0.0

zipWith
(
const maybeName: Option.Option<string>
maybeName
,
const maybeAge: Option.Option<number>
maybeAge
, (
name: string
name
,
age: number
age
) => ({
name: string
name
:
name: string
name
.
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
(),
age: number
age
}))
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 person: Option.Option<{
name: string;
age: number;
}>
person
)
// Output: { _id: 'Option', _tag: 'None' }

To combine multiple Option values without transforming their contents, you can use Option.all. This function returns an Option with a structure matching the input:

  • If you pass a tuple, the result will be a tuple of the same length.
  • If you pass a struct, the result will be a struct with the same keys.
  • If you pass an Iterable, the result will be an array.

Example (Combining Multiple Options into a Tuple and Struct)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const maybeName: Option.Option<string>
maybeName
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("John")
const
const maybeAge: Option.Option<number>
maybeAge
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(25)
// ┌─── Option<[string, number]>
// ▼
const
const tuple: Option.Option<[string, number]>
tuple
=
import Option

@since2.0.0

@since2.0.0

Option
.
const all: <readonly [Option.Option<string>, Option.Option<number>]>(input: readonly [Option.Option<string>, Option.Option<number>]) => Option.Option<[string, number]>

Takes a structure of Options and returns an Option of values with the same structure.

  • If a tuple is supplied, then the returned Option will contain a tuple with the same length.
  • If a struct is supplied, then the returned Option will contain a struct with the same keys.
  • If an iterable is supplied, then the returned Option will contain an array.

@paramfields - the struct of Options to be sequenced.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.all([Option.some(1), Option.some(2)]), Option.some([1, 2]))
assert.deepStrictEqual(Option.all({ a: Option.some(1), b: Option.some("hello") }), Option.some({ a: 1, b: "hello" }))
assert.deepStrictEqual(Option.all({ a: Option.some(1), b: Option.none() }), Option.none())

@since2.0.0

all
([
const maybeName: Option.Option<string>
maybeName
,
const maybeAge: Option.Option<number>
maybeAge
])
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 tuple: Option.Option<[string, number]>
tuple
)
/*
Output:
{ _id: 'Option', _tag: 'Some', value: [ 'John', 25 ] }
*/
// ┌─── Option<{ name: string; age: number; }>
// ▼
const
const struct: Option.Option<{
name: string;
age: number;
}>
struct
=
import Option

@since2.0.0

@since2.0.0

Option
.
const all: <{
readonly name: Option.Option<string>;
readonly age: Option.Option<number>;
}>(input: {
readonly name: Option.Option<string>;
readonly age: Option.Option<number>;
}) => Option.Option<...>

Takes a structure of Options and returns an Option of values with the same structure.

  • If a tuple is supplied, then the returned Option will contain a tuple with the same length.
  • If a struct is supplied, then the returned Option will contain a struct with the same keys.
  • If an iterable is supplied, then the returned Option will contain an array.

@paramfields - the struct of Options to be sequenced.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.all([Option.some(1), Option.some(2)]), Option.some([1, 2]))
assert.deepStrictEqual(Option.all({ a: Option.some(1), b: Option.some("hello") }), Option.some({ a: 1, b: "hello" }))
assert.deepStrictEqual(Option.all({ a: Option.some(1), b: Option.none() }), Option.none())

@since2.0.0

all
({
name: Option.Option<string>
name
:
const maybeName: Option.Option<string>
maybeName
,
age: Option.Option<number>
age
:
const maybeAge: Option.Option<number>
maybeAge
})
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 struct: Option.Option<{
name: string;
age: number;
}>
struct
)
/*
Output:
{ _id: 'Option', _tag: 'Some', value: { name: 'John', age: 25 } }
*/

If any of the Option values are None, the result will be None:

Example

import { Option } from "effect"
const maybeName: Option.Option<string> = Option.some("John")
const maybeAge: Option.Option<number> = Option.none()
console.log(Option.all([maybeName, maybeAge]))
// Output: { _id: 'Option', _tag: 'None' }

Similar to Effect.gen, Option.gen provides a more readable, generator-based syntax for working with Option values, making code that involves Option easier to write and understand. This approach is similar to using async/await but tailored for Option.

Example (Using Option.gen to Create a Combined Value)

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const maybeName: Option.Option<string>
maybeName
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
("John")
const
const maybeAge: Option.Option<number>
maybeAge
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(25)
const
const person: Option.Option<{
name: string;
age: number;
}>
person
=
import Option

@since2.0.0

@since2.0.0

Option
.
const gen: Gen
<unknown, YieldWrap<Option.None<string>> | YieldWrap<Option.Some<string>> | YieldWrap<Option.None<number>> | YieldWrap<Option.Some<number>>, {
...;
}>(...args: [self: ...] | [body: ...]) => Option.Option<...>

@since2.0.0

gen
(function* () {
const
const name: string
name
= (yield*
const maybeName: Option.Option<string>
maybeName
).
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
()
const
const age: number
age
= yield*
const maybeAge: Option.Option<number>
maybeAge
return {
name: string
name
,
age: number
age
}
})
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 person: Option.Option<{
name: string;
age: number;
}>
person
)
/*
Output:
{ _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
*/

When any of the Option values in the sequence is None, the generator immediately returns the None value, skipping further operations:

Example (Handling a None Value with Option.gen)

In this example, Option.gen halts execution as soon as it encounters the None value, effectively propagating the missing value without performing further operations.

import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const maybeName: Option.Option<string>
maybeName
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<string> =
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <string>() => Option.Option<string>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
const
const maybeAge: Option.Option<number>
maybeAge
:
import Option

@since2.0.0

@since2.0.0

Option
.
type Option<A> = Option.None<A> | Option.Some<A>

@since2.0.0

@since2.0.0

Option
<number> =
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(25)
const
const program: Option.Option<{
name: string;
age: number;
}>
program
=
import Option

@since2.0.0

@since2.0.0

Option
.
const gen: Gen
<unknown, YieldWrap<Option.None<string>> | YieldWrap<Option.Some<string>> | YieldWrap<Option.None<number>> | YieldWrap<Option.Some<number>>, {
...;
}>(...args: [self: ...] | [body: ...]) => Option.Option<...>

@since2.0.0

gen
(function* () {
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
("Retrieving name...")
const
const name: string
name
= (yield*
const maybeName: Option.Option<string>
maybeName
).
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
()
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
("Retrieving age...")
const
const age: number
age
= yield*
const maybeAge: Option.Option<number>
maybeAge
return {
name: string
name
,
age: number
age
}
})
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 program: Option.Option<{
name: string;
age: number;
}>
program
)
/*
Output:
Retrieving name...
{ _id: 'Option', _tag: 'None' }
*/

The use of console.log in these example is for demonstration purposes only. When using Option.gen, avoid including side effects in your generator functions, as Option should remain a pure data structure.

You can compare Option values using the Option.getEquivalence function. This function allows you to specify how to compare the contents of Option types by providing an Equivalence for the type of value they may contain.

Example (Comparing Optional Numbers for Equivalence)

Suppose you have optional numbers and want to check if they are equivalent. Here’s how you can use Option.getEquivalence:

import {
import Option

@since2.0.0

@since2.0.0

Option
,
import Equivalence
Equivalence
} from "effect"
const
const myEquivalence: Equivalence.Equivalence<Option.Option<number>>
myEquivalence
=
import Option

@since2.0.0

@since2.0.0

Option
.
const getEquivalence: <number>(isEquivalent: Equivalence.Equivalence<number>) => Equivalence.Equivalence<Option.Option<number>>

@example

import { Option, Number } from "effect"
const isEquivalent = Option.getEquivalence(Number.Equivalence)
assert.deepStrictEqual(isEquivalent(Option.none(), Option.none()), true)
assert.deepStrictEqual(isEquivalent(Option.none(), Option.some(1)), false)
assert.deepStrictEqual(isEquivalent(Option.some(1), Option.none()), false)
assert.deepStrictEqual(isEquivalent(Option.some(1), Option.some(2)), false)
assert.deepStrictEqual(isEquivalent(Option.some(1), Option.some(1)), true)

@since2.0.0

getEquivalence
(
import Equivalence
Equivalence
.
const number: Equivalence.Equivalence<number>

@since2.0.0

number
)
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 myEquivalence: Equivalence.Equivalence
(self: Option.Option<number>, that: Option.Option<number>) => boolean
myEquivalence
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1),
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1)))
// Output: true, both options contain the number 1
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 myEquivalence: Equivalence.Equivalence
(self: Option.Option<number>, that: Option.Option<number>) => boolean
myEquivalence
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1),
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(2)))
// Output: false, the numbers are different
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 myEquivalence: Equivalence.Equivalence
(self: Option.Option<number>, that: Option.Option<number>) => boolean
myEquivalence
(
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1),
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <number>() => Option.Option<number>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()))
// Output: false, one is a number and the other is empty

You can sort a collection of Option values using the Option.getOrder function. This function helps specify a custom sorting rule for the type of value contained within the Option.

Example (Sorting Optional Numbers)

Suppose you have a list of optional numbers and want to sort them in ascending order, with empty values (Option.none()) treated as the lowest:

import {
import Option

@since2.0.0

@since2.0.0

Option
,
import Array
Array
,
import Order
Order
} from "effect"
const
const items: (Option.None<number> | Option.Some<number>)[]
items
= [
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(1),
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <never>() => Option.Option<never>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
(),
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <number>(value: number) => Option.Option<number>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(2)]
// Create an order for sorting Option values containing numbers
const
const myOrder: Order.Order<Option.Option<number>>
myOrder
=
import Option

@since2.0.0

@since2.0.0

Option
.
const getOrder: <number>(O: Order.Order<number>) => Order.Order<Option.Option<number>>

The Order instance allows Option values to be compared with compare, whenever there is an Order instance for the type the Option contains.

None is considered to be less than any Some value.

@example

import { pipe, Option, Number } from "effect"
const O = Option.getOrder(Number.Order)
assert.deepStrictEqual(O(Option.none(), Option.none()), 0)
assert.deepStrictEqual(O(Option.none(), Option.some(1)), -1)
assert.deepStrictEqual(O(Option.some(1), Option.none()), 1)
assert.deepStrictEqual(O(Option.some(1), Option.some(2)), -1)
assert.deepStrictEqual(O(Option.some(1), Option.some(1)), 0)

@since2.0.0

getOrder
(
import Order
Order
.
const number: Order.Order<number>

@since2.0.0

number
)
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 Array
Array
.
const sort: <Option.Option<number>>(O: Order.Order<Option.Option<number>>) => <A, S>(self: S) => Array.ReadonlyArray.With<S, Array.ReadonlyArray.Infer<S>> (+2 overloads)

Create a new array with elements sorted in increasing order based on the specified comparator. If the input is a NonEmptyReadonlyArray, the output will also be a NonEmptyReadonlyArray.

@since2.0.0

sort
(
const myOrder: Order.Order<Option.Option<number>>
myOrder
)(
const items: (Option.None<number> | Option.Some<number>)[]
items
))
/*
Output:
[
{ _id: 'Option', _tag: 'None' }, // None appears first because it's considered the lowest
{ _id: 'Option', _tag: 'Some', value: 1 }, // Sorted in ascending order
{ _id: 'Option', _tag: 'Some', value: 2 }
]
*/

Example (Sorting Optional Dates in Reverse Order)

Consider a more complex case where you have a list of objects containing optional dates, and you want to sort them in descending order, with Option.none() values at the end:

import { Option, Array, Order } from "effect"
const items = [
{ data: Option.some(new Date(10)) },
{ data: Option.some(new Date(20)) },
{ data: Option.none() }
]
// Define the order to sort dates within Option values in reverse
const sorted = Array.sortWith(
items,
(item) => item.data,
Order.reverse(Option.getOrder(Order.Date))
)
console.log(sorted)
/*
Output:
[
{ data: { _id: 'Option', _tag: 'Some', value: '1970-01-01T00:00:00.020Z' } },
{ data: { _id: 'Option', _tag: 'Some', value: '1970-01-01T00:00:00.010Z' } },
{ data: { _id: 'Option', _tag: 'None' } } // None placed last
]
*/