Skip to content

Pattern Matching

Pattern matching is a method that allows developers to handle intricate conditions within a single, concise expression. It simplifies code, making it more concise and easier to understand. Additionally, it includes a process called exhaustiveness checking, which helps to ensure that no possible case has been overlooked.

Originating from functional programming languages, pattern matching stands as a powerful technique for code branching. It often offers a more potent and less verbose solution compared to imperative alternatives such as if/else or switch statements, particularly when dealing with complex conditions.

Although not yet a native feature in JavaScript, there’s an ongoing tc39 proposal in its early stages to introduce pattern matching to JavaScript. However, this proposal is at stage 1 and might take several years to be implemented. Nonetheless, developers can implement pattern matching in their codebase. The effect/Match module provides a reliable, type-safe pattern matching implementation that is available for immediate use.

Creating a Matcher involves using the Match.type constructor function with a specified type. This sets the foundation for pattern matching against that particular type. Once the Matcher is established, developers can employ various combinators like Match.when, Match.not, and Match.tag to define patterns that the Matcher will check against.

Example (Defining a Type Matcher)

1
import {
import Match
Match
} from "effect"
2
3
// Create a Matcher for objects with properties 'a' or 'b'
4
//
5
// ┌─── (u: { a: number; } | { b: string; }) => string | number
6
// ▼
7
const
const match: (u: { a: number; } | { b: string; }) => string | number
match
=
import Match
Match
.
const type: <{ a: number; } | { b: string; }>() => Match.Matcher<{ a: number; } | { b: string; }, Match.Types.Without<never>, { a: number; } | { b: string; }, never, never, any>
type
<{
(property) a: number
a
: number } | {
(property) b: string
b
: string }>().
(method) Pipeable.pipe<Match.Matcher<{ a: number; } | { b: string; }, Match.Types.Without<never>, { a: number; } | { b: string; }, never, never, any>, Match.Matcher<{ a: number; } | { b: string; }, ... 4 more ..., any>, Match.Matcher<...>, (u: { ...; } | { ...; }) => string | number>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (u: { ...; } | { ...; }) => string | number): (u: { ...; } | { ...; }) => string | number (+21 overloads)
pipe
(
8
// Match an object with a numeric property 'a'
9
import Match
Match
.
const when: <{ a: number; } | { b: string; }, { readonly a: Refinement<unknown, number>; }, any, (_: { a: number; }) => number>(pattern: { readonly a: Refinement<unknown, number>; }, f: (_: { a: number; }) => number) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>
when
({
(property) a: Refinement<unknown, number>
a
:
import Match
Match
.
const number: Refinement<unknown, number>
number
}, (
(parameter) _: { a: number; }
_
) =>
(parameter) _: { a: number; }
_
.
(property) a: number
a
),
10
// Match an object with a string property 'b'
11
import Match
Match
.
const when: <{ b: string; }, { readonly b: Refinement<unknown, string>; }, any, (_: { b: string; }) => string>(pattern: { readonly b: Refinement<unknown, string>; }, f: (_: { b: string; }) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>
when
({
(property) b: Refinement<unknown, string>
b
:
import Match
Match
.
const string: Refinement<unknown, string>
string
}, (
(parameter) _: { b: string; }
_
) =>
(parameter) _: { b: string; }
_
.
(property) b: string
b
),
12
// Ensure all cases are covered
13
import Match
Match
.
const exhaustive: <I, F, A, Pr, Ret>(self: Match.Matcher<I, F, never, A, Pr, Ret>) => [Pr] extends [never] ? (u: I) => Unify<A> : Unify<A>
exhaustive
14
)
15
16
namespace console var console: Console

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

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

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

log
(
const match: (u: { a: number; } | { b: string; }) => string | number
match
({
(property) a: number
a
: 0 }))
17
// Output: 0
18
19
namespace console var console: Console

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

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

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

log
(
const match: (u: { a: number; } | { b: string; }) => string | number
match
({
(property) b: string
b
: "hello" }))
20
// Output: "hello"

In addition to defining a Matcher based on a specific type, developers can also create a Matcher directly from a value utilizing the Match.value constructor. This method allows matching patterns against the provided value.

Example (Matching Against a Value)

1
import {
import Match
Match
} from "effect"
2
3
// Create a Matcher for a specific value
4
const
const result: string
result
=
import Match
Match
.
const value: <{ readonly name: "John"; readonly age: 30; }>(i: { readonly name: "John"; readonly age: 30; }) => Match.Matcher<{ readonly name: "John"; readonly age: 30; }, Match.Types.Without<never>, { readonly name: "John"; readonly age: 30; }, never, { readonly name: "John"; readonly age: 30; }, any>
value
({
(property) name: "John"
name
: "John",
(property) age: 30
age
: 30 }).
(method) Pipeable.pipe<Match.Matcher<{ readonly name: "John"; readonly age: 30; }, Match.Types.Without<never>, { readonly name: "John"; readonly age: 30; }, never, { readonly name: "John"; readonly age: 30; }, any>, Match.Matcher<...>, string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => string): string (+21 overloads)
pipe
(
5
// Match when the 'name' property is "John"
6
import Match
Match
.
const when: <{ readonly name: "John"; readonly age: 30; }, { readonly name: "John"; }, any, (user: { name: "John"; readonly age: 30; }) => string>(pattern: { readonly name: "John"; }, f: (user: { name: "John"; readonly age: 30; }) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>
when
(
7
{
(property) name: "John"
name
: "John" },
8
(
(parameter) user: { name: "John"; readonly age: 30; }
user
) => `${
(parameter) user: { name: "John"; readonly age: 30; }
user
.
(property) name: "John"
name
} is ${
(parameter) user: { name: "John"; readonly age: 30; }
user
.
(property) age: 30
age
} years old`
9
),
10
// Provide a fallback for unmatched cases
11
import Match
Match
.
const orElse: <never, any, () => string>(f: () => string) => <I, R, A, Pr>(self: Match.Matcher<I, R, never, A, Pr, any>) => [Pr] extends [never] ? (input: I) => Unify<...> : Unify<...>
orElse
(() => "Oh, not John")
12
)
13
14
namespace console var console: Console

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

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

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

log
(
const result: string
result
)
15
// Output: "John is 30 years old"

The Match.when function allows you to define conditions for matching values. It uses predicates to create rules that the data must satisfy.

Example (Using Predicates for Matching)

1
import {
import Match
Match
} from "effect"
2
3
// Define a matcher for objects with an "age" property
4
const
const match: (input: { age: number; }) => string
match
=
import Match
Match
.
const type: <{ age: number; }>() => Match.Matcher<{ age: number; }, Match.Types.Without<never>, { age: number; }, never, never, any>
type
<{
(property) age: number
age
: number }>().
(method) Pipeable.pipe<Match.Matcher<{ age: number; }, Match.Types.Without<never>, { age: number; }, never, never, any>, Match.Matcher<{ age: number; }, Match.Types.Without<{ readonly age: never; }>, { age: number; }, string, never, any>, (input: { ...; }) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => (input: { ...; }) => string): (input: { ...; }) => string (+21 overloads)
pipe
(
5
// Match when age is 5 or greater
6
import Match
Match
.
const when: <{ age: number; }, { readonly age: (age: number) => boolean; }, any, (user: { age: number; }) => string>(pattern: { readonly age: (age: number) => boolean; }, f: (user: { age: number; }) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>
when
({
(property) age: (age: number) => boolean
age
: (
(parameter) age: number
age
) =>
(parameter) age: number
age
>= 5 }, (
(parameter) user: { age: number; }
user
) => `Age: ${
(parameter) user: { age: number; }
user
.
(property) age: number
age
}`),
7
// Fallback for when the age condition is not met
8
import Match
Match
.
const orElse: <{ age: number; }, any, (user: { age: number; }) => string>(f: (user: { age: number; }) => string) => <I, R, A, Pr>(self: Match.Matcher<I, R, { age: number; }, A, Pr, any>) => [...] extends [...] ? (input: I) => Unify<...> : Unify<...>
orElse
((
(parameter) user: { age: number; }
user
) => `${
(parameter) user: { age: number; }
user
.
(property) age: number
age
} is too young`)
9
)
10
11
namespace console var console: Console

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

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

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

log
(
const match: (input: { age: number; }) => string
match
({
(property) age: number
age
: 5 }))
12
// Output: "Age: 5"
13
14
namespace console var console: Console

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

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

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

log
(
const match: (input: { age: number; }) => string
match
({
(property) age: number
age
: 4 }))
15
// Output: "4 is too young"

Match.not allows for excluding a specific value while matching other conditions.

Example (Excluding a Specific Value)

1
import {
import Match
Match
} from "effect"
2
3
// Create a matcher for string or number values
4
const
const match: (input: string | number) => string
match
=
import Match
Match
.
const type: <string | number>() => Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>
type
<string | number>().
(method) Pipeable.pipe<Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>, Match.Matcher<string | number, Match.Types.Only<"hi">, "hi", string, never, any>, (input: string | number) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => (input: string | number) => string): (input: string | number) => string (+21 overloads)
pipe
(
5
// Exclude the value "hi" and return "a" for anything else
6
import Match
Match
.
const not: <string | number, "hi", any, (_: string | number) => string>(pattern: "hi", f: (_: string | number) => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string | number, A, Pr, any>) => Match.Matcher<...>
not
("hi", (
(parameter) _: string | number
_
) => "ok"),
7
// Fallback for when the value is "hi"
8
import Match
Match
.
const orElse: <"hi", any, () => string>(f: () => string) => <I, R, A, Pr>(self: Match.Matcher<I, R, "hi", A, Pr, any>) => [Pr] extends [never] ? (input: I) => Unify<...> : Unify<...>
orElse
(() => "fallback")
9
)
10
11
namespace console var console: Console

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

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

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

log
(
const match: (input: string | number) => string
match
("hello"))
12
// Output: "ok"
13
14
namespace console var console: Console

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

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

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

log
(
const match: (input: string | number) => string
match
("hi"))
15
// Output: "fallback"

The Match.tag function enables pattern matching against the tag within a Discriminated Union.

Example (Pattern Matching on a Discriminated Union)

1
import {
import Match
Match
,
import Either
Either
} from "effect"
2
3
// Create a Matcher for Either<number, string>
4
const
const match: (u: Either.Either<number, string>) => string | number
match
=
import Match
Match
.
const type: <Either.Either<number, string>>() => Match.Matcher<Either.Either<number, string>, Match.Types.Without<never>, Either.Either<number, string>, never, never, any>
type
<
import Either
Either
.
type Either<R, L = never> = Either.Left<L, R> | Either.Right<L, R> namespace Either
Either
<number, string>>().
(method) Pipeable.pipe<Match.Matcher<Either.Either<number, string>, Match.Types.Without<never>, Either.Either<number, string>, never, never, any>, Match.Matcher<Either.Either<number, string>, ... 4 more ..., any>, Match.Matcher<...>, (u: Either.Either<...>) => string | number>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (u: Either.Either<...>) => string | number): (u: Either.Either<...>) => string | number (+21 overloads)
pipe
(
5
// Match when the tag is "Right" and extract the value
6
import Match
Match
.
const tag: <Either.Either<number, string>, "Right", any, number>(...pattern: [first: "Right", ...values: "Right"[], f: (_: Either.Right<string, number>) => number]) => <I, F, A, Pr>(self: Match.Matcher<I, ... 4 more ..., any>) => Match.Matcher<...>
tag
("Right", (
(parameter) _: Either.Right<string, number>
_
) =>
(parameter) _: Either.Right<string, number>
_
.
(property) Right<string, number>.right: number
right
),
7
// Match when the tag is "Left" and extract the error
8
import Match
Match
.
const tag: <Either.Left<string, number>, "Left", any, string>(...pattern: [first: "Left", ...values: "Left"[], f: (_: Either.Left<string, number>) => string]) => <I, F, A, Pr>(self: Match.Matcher<I, F, Either.Left<...>, A, Pr, any>) => Match.Matcher<...>
tag
("Left", (
(parameter) _: Either.Left<string, number>
_
) =>
(parameter) _: Either.Left<string, number>
_
.
(property) Left<string, number>.left: string
left
),
9
// Ensure all possible cases are covered
10
import Match
Match
.
const exhaustive: <I, F, A, Pr, Ret>(self: Match.Matcher<I, F, never, A, Pr, Ret>) => [Pr] extends [never] ? (u: I) => Unify<A> : Unify<A>
exhaustive
11
)
12
13
namespace console var console: Console

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

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

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

log
(
const match: (u: Either.Either<number, string>) => string | number
match
(
import Either
Either
.
const right: <number>(right: number) => Either.Either<number, never>

Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias of this structure.

right
(123)))
14
// Output: 123
15
16
namespace console var console: Console

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

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

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

log
(
const match: (u: Either.Either<number, string>) => string | number
match
(
import Either
Either
.
const left: <string>(left: string) => Either.Either<never, string>

Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this structure.

left
("Oh no!")))
17
// Output: "Oh no!"

The Match.exhaustive transformation is used to signal the end of the matching process. It ensures that all possible patterns have been accounted for, either by returning the match (when using Match.value) or the evaluation function (when using Match.type). This check helps prevent situations where a possible case is left unhandled.

Example (Ensuring All Cases Are Handled)

1
import {
import Match
Match
,
import Either
Either
} from "effect"
2
3
const
const result: never
result
=
import Match
Match
.
const value: <Either.Either<number, never>>(i: Either.Either<number, never>) => Match.Matcher<Either.Either<number, never>, Match.Types.Without<never>, Either.Either<number, never>, never, Either.Either<...>, any>
value
(
import Either
Either
.
const right: <number>(right: number) => Either.Either<number, never>

Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias of this structure.

right
(0)).
(method) Pipeable.pipe<Match.Matcher<Either.Either<number, never>, Match.Types.Without<never>, Either.Either<number, never>, never, Either.Either<number, never>, any>, never, never>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => never, bc: (_: never) => never): never (+21 overloads)
pipe
(
4
import Match
Match
.
const when: <Either.Either<number, never>, { readonly _tag: "Right"; }, any, (_: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; ... 14 more ...; readonly right: number; }) => number>(pattern: { ...; }, f: (_: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; ... 14 more ...; readonly right: number; }) => number) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>
when
({
(property) _tag: "Right"
_tag
: "Right" }, (
(parameter) _: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; [Unify.typeSymbol]?: unknown; [Unify.unifySymbol]?: Either.EitherUnify<...>; ... 12 more ...; readonly right: number; }
_
) =>
(parameter) _: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; [Unify.typeSymbol]?: unknown; [Unify.unifySymbol]?: Either.EitherUnify<...>; ... 12 more ...; readonly right: number; }
_
.
(property) right: number
right
),
5
// The "Left" case is missing in the match
6
// Using Match.exhaustive to ensure all cases are considered
7
// @ts-expect-error
8
import Match
Match
.
const exhaustive: <I, F, A, Pr, Ret>(self: Match.Matcher<I, F, never, A, Pr, Ret>) => [Pr] extends [never] ? (u: I) => Unify<A> : Unify<A>
exhaustive
9
// TypeError! Type 'Left<never, number>' is not assignable to type 'never'
10
)

The Match.orElse transformation ends the matching process by providing a fallback value if no specific patterns are matched. This can return either the match (for Match.value) or the evaluation function (for Match.type).

Example (Using orElse as a Fallback)

1
import {
import Match
Match
} from "effect"
2
3
const
const match: (input: string | number) => string
match
=
import Match
Match
.
const type: <string | number>() => Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>
type
<string | number>().
(method) Pipeable.pipe<Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>, Match.Matcher<string | number, Match.Types.Without<"hi">, string | number, string, never, any>, (input: string | number) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => (input: string | number) => string): (input: string | number) => string (+21 overloads)
pipe
(
4
// Match when the value is "hi"
5
import Match
Match
.
const when: <string | number, "hi", any, (_: "hi") => string>(pattern: "hi", f: (_: "hi") => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string | number, A, Pr, any>) => Match.Matcher<...>
when
("hi", (
(parameter) _: "hi"
_
) => "hello"),
6
// Fallback when no patterns match
7
import Match
Match
.
const orElse: <string | number, any, () => string>(f: () => string) => <I, R, A, Pr>(self: Match.Matcher<I, R, string | number, A, Pr, any>) => [Pr] extends [...] ? (input: I) => Unify<...> : Unify<...>
orElse
(() => "I literally do not understand")
8
)
9
10
namespace console var console: Console

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

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

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

log
(
const match: (input: string | number) => string
match
("hi"))
11
// Output: "hello"
12
13
namespace console var console: Console

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

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

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

log
(
const match: (input: string | number) => string
match
("hello"))
14
// Output: "I literally do not understand"

The Match.option transformation wraps the result in an Option. If the match is successful, the result is wrapped in Some, if no match is found, it returns None.

Example (Wrapping Match Result in an Option)

1
import {
import Match
Match
,
import Either
Either
} from "effect"
2
3
const
const result: Option<number>
result
=
import Match
Match
.
const value: <Either.Either<number, never>>(i: Either.Either<number, never>) => Match.Matcher<Either.Either<number, never>, Match.Types.Without<never>, Either.Either<number, never>, never, Either.Either<...>, any>
value
(
import Either
Either
.
const right: <number>(right: number) => Either.Either<number, never>

Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias of this structure.

right
(0)).
(method) Pipeable.pipe<Match.Matcher<Either.Either<number, never>, Match.Types.Without<never>, Either.Either<number, never>, never, Either.Either<number, never>, any>, Match.Matcher<...>, Option<...>>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Option<...>): Option<...> (+21 overloads)
pipe
(
4
// Match when the tag is "Right" and extract the value
5
import Match
Match
.
const when: <Either.Either<number, never>, { readonly _tag: "Right"; }, any, (_: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; ... 14 more ...; readonly right: number; }) => number>(pattern: { ...; }, f: (_: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; ... 14 more ...; readonly right: number; }) => number) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>
when
({
(property) _tag: "Right"
_tag
: "Right" }, (
(parameter) _: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; [Unify.typeSymbol]?: unknown; [Unify.unifySymbol]?: Either.EitherUnify<...>; ... 12 more ...; readonly right: number; }
_
) =>
(parameter) _: { _tag: "Right"; readonly [TypeId]: { readonly _R: Covariant<number>; readonly _L: Covariant<never>; }; [Unify.typeSymbol]?: unknown; [Unify.unifySymbol]?: Either.EitherUnify<...>; ... 12 more ...; readonly right: number; }
_
.
(property) right: number
right
),
6
// Wrap the result in an Option
7
import Match
Match
.
const option: <I, F, R, A, Pr, Ret>(self: Match.Matcher<I, F, R, A, Pr, Ret>) => [Pr] extends [never] ? (input: I) => Option<Unify<A>> : Option<Unify<A>>
option
8
)
9
10
namespace console var console: Console

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

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

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

log
(
const result: Option<number>
result
)
11
// Output: { _id: 'Option', _tag: 'Some', value: 0 }

The Match.either transformation attempts to match a value, returning an Either in the format Either<MatchResult, NoMatchResult>. This way, if a match is successful, the result will be wrapped in Right. If it fails, the unmatched value will be wrapped in Left.

Example (Wrapping Match Result in an Either)

1
import {
import Match
Match
} from "effect"
2
3
const
const match: (input: string) => Either<number, string>
match
=
import Match
Match
.
const type: <string>() => Match.Matcher<string, Match.Types.Without<never>, string, never, never, any>
type
<string>().
(method) Pipeable.pipe<Match.Matcher<string, Match.Types.Without<never>, string, never, never, any>, Match.Matcher<string, Match.Types.Without<"hi">, string, number, never, any>, (input: string) => Either<...>>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => (input: string) => Either<...>): (input: string) => Either<...> (+21 overloads)
pipe
(
4
// Match "hi" and return the length of the string
5
import Match
Match
.
const when: <string, "hi", any, (_: "hi") => number>(pattern: "hi", f: (_: "hi") => number) => <I, F, A, Pr>(self: Match.Matcher<I, F, string, A, Pr, any>) => Match.Matcher<I, ... 4 more ..., any>
when
("hi", (
(parameter) _: "hi"
_
) =>
(parameter) _: "hi"
_
.
(property) String.length: number

Returns the length of a String object.

length
),
6
// Wrap the result in an Either
7
import Match
Match
.
const either: <I, F, R, A, Pr, Ret>(self: Match.Matcher<I, F, R, A, Pr, Ret>) => [Pr] extends [never] ? (input: I) => Either<Unify<A>, R> : Either<Unify<A>, R>
either
8
)
9
10
namespace console var console: Console

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

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

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

log
(
const match: (input: string) => Either<number, string>
match
("hi"))
11
// Output: { _id: 'Either', _tag: 'Right', right: 2 }
12
13
namespace console var console: Console

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

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

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

log
(
const match: (input: string) => Either<number, string>
match
("shigidigi"))
14
// Output: { _id: 'Either', _tag: 'Left', left: 'shigidigi' }