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
Creating Options
some
Use the Option.some constructor to create an Option that holds a value of type A.
Example (Creating an Option with a Value)
none
Use the Option.none constructor to create an Option representing the absence of a value.
Example (Creating an Option with No Value)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
// An Option holding no value
4
const
constnoValue:Option.Option<never>
noValue=
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
constnone: <never>() =>Option.Option<never>
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()
5
6
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
When to Use
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”)
Lifts a Predicate or Refinement into the Option context, returning a
Some of the input value if the predicate is satisfied, or None otherwise.
Details
This function transforms a Predicate (or a more specific Refinement) into
a function that produces an Option. If the predicate evaluates to true,
the input value is wrapped in a Some. If the predicate evaluates to
false, the result is None.
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:
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
interface
interfaceUser
User {
4
readonly
User.id: number
id:number
5
readonly
User.username: string
username:string
6
readonly
User.email: Option.Option<string>
email:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<string>
7
}
Here are examples of how to create User instances with and without an email:
Example (Creating Users with and without Email)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
interface
interfaceUser
User {
4
readonly
User.id: number
id:number
5
readonly
User.username: string
username:string
6
readonly
User.email: Option.Option<string>
email:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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”)
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Checks whether an Option represents the absence of a value (None).
@see ― isSome for the opposite check.
@example
import { Option } from"effect"
console.log(Option.isNone(Option.some(1)))
// Output: false
console.log(Option.isNone(Option.none()))
// Output: true
@since ― 2.0.0
isNone(
constfoo:Option.Option<number>
foo)) {
9
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Performs pattern matching on an Option to handle both Some and None
cases.
Details
This function allows you to match against an Option and handle both
scenarios: when the Option is None (i.e., contains no value), and when
the Option is Some (i.e., contains a value). It executes one of the
provided functions based on the case:
If the Option is None, the onNone function is executed and its result
is returned.
If the Option is Some, the onSome function is executed with the
contained value, and its result is returned.
This function provides a concise and functional way to handle optional values
without resorting to if or manual checks, making your code more declarative
and readable.
@example
// Title: Pattern Matching with Option
import { Option } from"effect"
constfoo= Option.some(1)
constmessage= Option.match(foo, {
onNone: () =>"Option is empty",
onSome: (value) =>`Option has a value: ${value}`
})
console.log(message)
// Output: "Option has a value: 1"
@since ― 2.0.0
match(
constfoo:Option.Option<number>
foo, {
6
onNone: LazyArg<string>
onNone: () =>"Option is empty",
7
onSome: (a:number) => string
onSome: (
value: number
value) =>`Option has a value: ${
value: number
value}`
8
})
9
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
// Transform the value inside Some
4
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Transforms the value inside a Some to a new value using the provided
function, while leaving None unchanged.
Details
This function applies a mapping function f to the value inside an Option
if it is a Some. If the Option is None, it remains unchanged. The
result is a new Option with the transformed value (if it was a Some) or
still None.
This utility is particularly useful for chaining transformations in a
functional way without needing to manually handle None cases.
When dealing with None, the mapping function is not executed, and the Option remains None:
Example (Mapping over None)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
// Mapping over None results in None
4
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Transforms the value inside a Some to a new value using the provided
function, while leaving None unchanged.
Details
This function applies a mapping function f to the value inside an Option
if it is a Some. If the Option is None, it remains unchanged. The
result is a new Option with the transformed value (if it was a Some) or
still None.
This utility is particularly useful for chaining transformations in a
functional way without needing to manually handle None cases.
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none(), (
n: never
n) =>
n: never
n+1))
5
// Output: { _id: 'Option', _tag: 'None' }
flatMap
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:
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
interface
interfaceUser
User {
4
readonly
User.id: number
id:number
5
readonly
User.username: string
username:string
6
readonly
User.email: Option.Option<string>
email:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<string>
7
readonly
User.address: Option.Option<Address>
address:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<
interfaceAddress
Address>
8
}
9
10
interface
interfaceAddress
Address {
11
readonly
Address.city: string
city:string
12
readonly
Address.street: Option.Option<string>
street:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<string>
13
}
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
interface
interfaceAddress
Address {
4
readonly
Address.city: string
city:string
5
readonly
Address.street: Option.Option<string>
street:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<string>
6
}
7
8
interface
interfaceUser
User {
9
readonly
User.id: number
id:number
10
readonly
User.username: string
username:string
11
readonly
User.email: Option.Option<string>
email:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<string>
12
readonly
User.address: Option.Option<Address>
address:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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”)
Applies a function to the value of a Some and flattens the resulting
Option. If the input is None, it remains None.
Details
This function allows you to chain computations that return Option values.
If the input Option is Some, the provided function f is applied to the
contained value, and the resulting Option is returned. If the input is
None, the function is not applied, and the result remains None.
This utility is particularly useful for sequencing operations that may fail
or produce optional results, enabling clean and concise workflows for
handling such cases.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
filter
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
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
// Function to remove empty strings from an 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.
When to Use
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”)
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none() // Return None if the value is an empty string
7
}
8
return
input: Option.Option<string>
input// Otherwise, return the original Option
9
}
10
11
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()))
12
// Output: { _id: 'Option', _tag: 'None' }
13
14
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
When to Use
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”)
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()))
7
// Output: { _id: 'Option', _tag: 'None' }
8
9
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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:
getOrThrow
This function extracts the value from a Some. If the Option is None, it throws an error.
Example (Retrieving Value or Throwing an Error)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Extracts the value of an Option or throws a default error if the Option
is None.
Details
This function extracts the value from an Option if it is Some. If the
Option is None, it throws a default error. It is useful for fail-fast
scenarios where the absence of a value is treated as an exceptional case and
a default error is sufficient.
@see ― getOrThrowWith for a version that allows you to provide a custom error.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Extracts the value of an Option or throws a default error if the Option
is None.
Details
This function extracts the value from an Option if it is Some. If the
Option is None, it throws a default error. It is useful for fail-fast
scenarios where the absence of a value is treated as an exceptional case and
a default error is sufficient.
@see ― getOrThrowWith for a version that allows you to provide a custom error.
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()))
7
// throws: Error: getOrThrow called on a None
getOrNull / getOrUndefined
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Returns the value contained in the Option if it is Some; otherwise,
returns null.
Details
This function provides a way to extract the value of an Option while
falling back to null if the Option is None.
It is particularly useful in scenarios where null is an acceptable
placeholder for the absence of a value, such as when interacting with APIs or
systems that use null as a default for missing values.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Returns the value contained in the Option if it is Some; otherwise,
returns null.
Details
This function provides a way to extract the value of an Option while
falling back to null if the Option is None.
It is particularly useful in scenarios where null is an acceptable
placeholder for the absence of a value, such as when interacting with APIs or
systems that use null as a default for missing values.
@example
import { Option } from"effect"
console.log(Option.getOrNull(Option.some(1)))
// Output: 1
console.log(Option.getOrNull(Option.none()))
// Output: null
@since ― 2.0.0
getOrNull(
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
constnone: <any>() =>Option.Option<any>
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()))
7
// Output: null
8
9
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Returns the value contained in the Option if it is Some; otherwise,
returns undefined.
Details
This function provides a way to extract the value of an Option while
falling back to undefined if the Option is None.
It is particularly useful in scenarios where undefined is an acceptable
placeholder for the absence of a value, such as when interacting with APIs or
systems that use undefined as a default for missing values.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Returns the value contained in the Option if it is Some; otherwise,
returns undefined.
Details
This function provides a way to extract the value of an Option while
falling back to undefined if the Option is None.
It is particularly useful in scenarios where undefined is an acceptable
placeholder for the absence of a value, such as when interacting with APIs or
systems that use undefined as a default for missing values.
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()))
13
// Output: undefined
getOrElse
This function allows you to specify a default value to return when the Option is None.
Example (Providing a Default Value When None)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Returns the value contained in the Option if it is Some, otherwise
evaluates and returns the result of onNone.
Details
This function allows you to provide a fallback value or computation for when
an Option is None. If the Option contains a value (Some), that value
is returned. If it is empty (None), the onNone function is executed, and
its result is returned instead.
This utility is helpful for safely handling Option values by ensuring you
always receive a meaningful result, whether or not the Option contains a
value. It is particularly useful for providing default values or alternative
logic when working with optional values.
@see ― getOrNull for a version that returns null instead of executing a function.
@see ― getOrUndefined for a version that returns undefined instead of executing a function.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Returns the value contained in the Option if it is Some, otherwise
evaluates and returns the result of onNone.
Details
This function allows you to provide a fallback value or computation for when
an Option is None. If the Option contains a value (Some), that value
is returned. If it is empty (None), the onNone function is executed, and
its result is returned instead.
This utility is helpful for safely handling Option values by ensuring you
always receive a meaningful result, whether or not the Option contains a
value. It is particularly useful for providing default values or alternative
logic when working with optional values.
@see ― getOrNull for a version that returns null instead of executing a function.
@see ― getOrUndefined for a version that returns undefined instead of executing a function.
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none(), () =>0))
7
// Output: 0
Fallback
orElse
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
// Simulating a computation that may or may not produce a result
4
const
constcomputation: () =>Option.Option<number>
computation= ():
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<number> =>
5
var Math:Math
An intrinsic object that provides basic mathematics functionality and constants.
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<number> =>
9
var Math:Math
An intrinsic object that provides basic mathematics functionality and constants.
Returns the provided Optionthat if the current Option (self) is
None; otherwise, it returns self.
Details
This function provides a fallback mechanism for Option values. If the
current Option is None (i.e., it contains no value), the that function
is evaluated, and its resulting Option is returned. If the current Option
is Some (i.e., it contains a value), the original Option is returned
unchanged.
This is particularly useful for chaining fallback values or computations,
allowing you to provide alternative Option values when the first one is
empty.
Performs pattern matching on an Option to handle both Some and None
cases.
Details
This function allows you to match against an Option and handle both
scenarios: when the Option is None (i.e., contains no value), and when
the Option is Some (i.e., contains a value). It executes one of the
provided functions based on the case:
If the Option is None, the onNone function is executed and its result
is returned.
If the Option is Some, the onSome function is executed with the
contained value, and its result is returned.
This function provides a concise and functional way to handle optional values
without resorting to if or manual checks, making your code more declarative
and readable.
@example
// Title: Pattern Matching with Option
import { Option } from"effect"
constfoo= Option.some(1)
constmessage= Option.match(foo, {
onNone: () =>"Option is empty",
onSome: (value) =>`Option has a value: ${value}`
})
console.log(message)
// Output: "Option has a value: 1"
@since ― 2.0.0
match(
constprogram:Option.Option<number>
program, {
17
onNone: LazyArg<string>
onNone: () =>"Both computations resulted in None",
18
// At least one computation succeeded
19
onSome: (a:number) => string
onSome: (
value: number
value) =>`Computed value: ${
value: number
value}`
20
})
21
22
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Returns the first Some value found in an Iterable collection of
Options, or None if no Some is found.
Details
This function iterates over a collection of Option values and returns the
first Some it encounters. If the collection contains only None values,
the result will also be None. This utility is useful for efficiently
finding the first valid value in a sequence of potentially empty or invalid
options.
The iteration stops as soon as a Some is found, making this function
efficient for large collections.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
fromNullable
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
Interop with Effect
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.
How Option Maps to Effect
Option Variant
Mapped to Effect
Description
None
Effect<never, NoSuchElementException>
Represents the absence of a value
Some<A>
Effect<A>
Represents the presence of a value
Example (Combining Option with Effect)
1
import {
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect,
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
// Function to get the head of an array, returning Option
function (typeparameter) Ain <A>(array:ReadonlyArray<A>):Option.Option<A>
A>(
array: readonly A[]
array:
interfaceReadonlyArray<T>
ReadonlyArray<
function (typeparameter) Ain <A>(array:ReadonlyArray<A>):Option.Option<A>
A>):
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<
function (typeparameter) Ain <A>(array:ReadonlyArray<A>):Option.Option<A>
A> =>
5
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
@since ― 2.0.0
@since ― 2.0.0
Option.
constsome: <A>(value:A) =>Option.Option<A>
Wraps the given value into an Option to represent its presence.
The Effect interface defines a value that describes a workflow or job,
which can succeed or fail.
Details
The Effect interface represents a computation that can model a workflow
involving various types of operations, such as synchronous, asynchronous,
concurrent, and parallel interactions. It operates within a context of type
R, and the result can either be a success with a value of type A or a
failure with an error of type E. The Effect is designed to handle complex
interactions with external resources, offering advanced features such as
fiber-based concurrency, scheduling, interruption handling, and scalability.
This makes it suitable for tasks that require fine-grained control over
concurrency and error management.
To execute an Effect value, you need a Runtime, which provides the
environment necessary to run and manage the computation.
@since ― 2.0.0
@since ― 2.0.0
Effect<string, string> => {
9
const
constsuccess:boolean
success=
var Math:Math
An intrinsic object that provides basic mathematics functionality and constants.
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
.
@see ― succeed to create an effect that represents a successful value.
Combines multiple effects into one, returning results based on the input
structure.
Details
Use this function 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
This 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.
@see ― forEach for iterating over elements and applying an effect.
@see ― allWith for a data-last version of this function.
Executes an effect and returns the result as a Promise.
Details
This function runs an effect and converts its result into a Promise. If the
effect succeeds, the Promise will resolve with the successful result. If
the effect fails, the Promise will reject with an error, which includes the
failure details of the effect.
The optional options parameter allows you to pass an AbortSignal for
cancellation, enabling more fine-grained control over asynchronous tasks.
When to Use
Use this function when you need to execute an effect and work with its result
in a promise-based system, such as when integrating with third-party
libraries that expect Promise results.
@see ― runPromiseExit for a version that returns an Exit type instead
of rejecting.
@example
// Title: Running a Successful Effect as a Promise
Attaches callbacks for the resolution and/or rejection of the Promise.
@param ― onfulfilled The callback to execute when the Promise is resolved.
@param ― onrejected The callback to execute when the Promise is rejected.
@returns ― A 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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
const
constmaybeName:Option.Option<string>
maybeName:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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”)
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.
When to Use
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”)
Combines two Option values into a new Option by applying a provided
function to their values.
Details
This function takes two Option values (self and that) and a combining
function f. If both Option values are Some, the function f is applied
to their values, and the result is wrapped in a new Some. If either
Option is None, the result is None.
This utility is useful for combining two optional computations into a single
result while maintaining type safety and avoiding explicit checks for None.
Converts all the alphabetic characters in a string to uppercase.
toUpperCase(),
9
age: number
age
10
}))
11
12
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
If either of the Option values is None, the result will be None:
Example (Handling None Values)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
const
constmaybeName:Option.Option<string>
maybeName:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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”)
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<number> =
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
constnone: <number>() =>Option.Option<number>
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()
5
6
// Since maybeAge is a None, the result will also be None
Combines two Option values into a new Option by applying a provided
function to their values.
Details
This function takes two Option values (self and that) and a combining
function f. If both Option values are Some, the function f is applied
to their values, and the result is wrapped in a new Some. If either
Option is None, the result is None.
This utility is useful for combining two optional computations into a single
result while maintaining type safety and avoiding explicit checks for None.
Converts all the alphabetic characters in a string to uppercase.
toUpperCase(),
9
age: number
age
10
}))
11
12
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
const
constmaybeName:Option.Option<string>
maybeName:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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”)
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.
When to Use
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”)
Combines a structure of Options into a single Option containing the
values with the same structure.
Details
This function takes a structure of Options (a tuple, struct, or iterable)
and produces a single Option that contains the values from the input
structure if all Options are Some. If any Option in the input is
None, the result is None. The structure of the input is preserved in the
output.
If the input is a tuple (e.g., an array), the result will be an Option
containing a tuple with the same length.
If the input is a struct (e.g., an object), the result will be an Option
containing a struct with the same keys.
If the input is an iterable, the result will be an Option containing an
array.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Combines a structure of Options into a single Option containing the
values with the same structure.
Details
This function takes a structure of Options (a tuple, struct, or iterable)
and produces a single Option that contains the values from the input
structure if all Options are Some. If any Option in the input is
None, the result is None. The structure of the input is preserved in the
output.
If the input is a tuple (e.g., an array), the result will be an Option
containing a tuple with the same length.
If the input is a struct (e.g., an object), the result will be an Option
containing a struct with the same keys.
If the input is an iterable, the result will be an Option containing an
array.
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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)
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
const
constmaybeName:Option.Option<string>
maybeName:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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”)
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.
When to Use
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”)
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
// Title: Using Option.gen to Create a Combined Value
Converts all the alphabetic characters in a string to uppercase.
toUpperCase()
8
const
constage:number
age=yield*
constmaybeAge:Option.Option<number>
maybeAge
9
return {
name: string
name,
age: number
age }
10
})
11
12
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
1
import {
import Option
@since ― 2.0.0
@since ― 2.0.0
Option } from"effect"
2
3
const
constmaybeName:Option.Option<string>
maybeName:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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
@since ― 2.0.0
@since ― 2.0.0
Option<string> =
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
constnone: <string>() =>Option.Option<string>
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()
4
const
constmaybeAge:Option.Option<number>
maybeAge:
import Option
@since ― 2.0.0
@since ― 2.0.0
Option.
typeOption<A> =Option.None<A> |Option.Some<A>
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.
When to Use
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”)
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
// Title: Using Option.gen to Create a Combined Value
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Converts all the alphabetic characters in a string to uppercase.
toUpperCase()
9
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
Equivalence
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:
Creates an Equivalence instance for comparing Option values, using a
provided Equivalence for the inner type.
Details
This function takes an Equivalence instance for a specific type A and
produces an Equivalence instance for Option<A>. The resulting
Equivalence determines whether two Option values are equivalent:
Two Nones are considered equivalent.
A Some and a None are not equivalent.
Two Some values are equivalent if their inner values are equivalent
according to the provided Equivalence.
@example
// Title: Comparing Optional Numbers for Equivalence
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
// Output: true, both options contain the number 1
7
8
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Represents the absence of a value by creating an empty Option.
Option.none returns an Option<never>, which is a subtype of Option<A>.
This means you can use it in place of any Option<A> regardless of the type
A.
@see ― some for the opposite operation.
@example
// Title: Creating an Option with No Value
import { Option } from"effect"
// An Option holding no value
//
// ┌─── Option<never>
// ▼
constnoValue= Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
@since ― 2.0.0
none()))
12
// Output: false, one is a number and the other is empty
Sorting
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:
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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.
{ _id: 'Option', _tag: 'None' }, // None appears first because it's considered the lowest
13
{ _id: 'Option', _tag: 'Some', value: 1 }, // Sorted in ascending order
14
{ _id: 'Option', _tag: 'Some', value: 2 }
15
]
16
*/
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:
1
import { Option, Array, Order } from"effect"
2
3
constitems= [
4
{ data: Option.some(newDate(10)) },
5
{ data: Option.some(newDate(20)) },
6
{ data: Option.none() }
7
]
8
9
// Define the order to sort dates within Option values in reverse