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.

Example (Handling Different Data Types with Pattern Matching)

import {
import Match
Match
} from "effect"
// Simulated dynamic input that can be a string or a number
const
const input: string | number
input
: string | number = "some input"
// ┌─── string
// ▼
const
const result: string
result
=
import Match
Match
.
const value: <string>(i: string) => Match.Matcher<string, Match.Types.Without<never>, string, never, string, any>

Creates a matcher from a specific value.

Details

This function allows you to define a Matcher directly from a given value, rather than from a type. This is useful when working with known values, enabling structured pattern matching on objects, primitives, or any data structure.

Once the matcher is created, you can use pattern-matching functions like

when

to define how different cases should be handled.

@seetype for creating a matcher from a specific type.

@example

// Title: Matching an Object by Property
import { Match } from "effect"
const input = { name: "John", age: 30 }
// Create a matcher for the specific object
const result = Match.value(input).pipe(
// Match when the 'name' property is "John"
Match.when(
{ name: "John" },
(user) => `${user.name} is ${user.age} years old`
),
// Provide a fallback if no match is found
Match.orElse(() => "Oh, not John")
)
console.log(result)
// Output: "John is 30 years old"

@since1.0.0

value
(
const input: string
input
).
Pipeable.pipe<Match.Matcher<string, Match.Types.Without<never>, string, never, string, any>, Match.Matcher<string, Match.Types.Without<number>, string, string, string, any>, Match.Matcher<...>, string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => string): string (+21 overloads)
pipe
(
// Match if the value is a number
import Match
Match
.
const when: <string, Refinement<unknown, number>, any, (n: number) => string>(pattern: Refinement<unknown, number>, f: (n: number) => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const number: Refinement<unknown, number>

Matches values of type number.

@since1.0.0

number
, (
n: number
n
) => `number: ${
n: number
n
}`),
// Match if the value is a string
import Match
Match
.
const when: <string, Refinement<unknown, string>, any, (s: string) => string>(pattern: Refinement<unknown, string>, f: (s: string) => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const string: Refinement<unknown, string>

Matches values of type string.

@since1.0.0

string
, (
s: string
s
) => `string: ${
s: string
s
}`),
// Ensure all possible cases are covered
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>

The Match.exhaustive method finalizes the pattern matching process by ensuring that all possible cases are accounted for. If any case is missing, TypeScript will produce a type error. This is particularly useful when working with unions, as it helps prevent unintended gaps in pattern matching.

@example

// Title: Ensuring All Cases Are Covered
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Mark the match as exhaustive, ensuring all cases are handled
// TypeScript will throw an error if any case is missing
//

@since1.0.0

exhaustive
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const result: string
result
)
// Output: "string: some input"

Pattern matching follows a structured process:

  1. Creating a matcher. Define a Matcher that operates on either a specific type or value.

  2. Defining patterns. Use combinators such as Match.when, Match.not, and Match.tag to specify matching conditions.

  3. Completing the match. Apply a finalizer such as Match.exhaustive, Match.orElse, or Match.option to determine how unmatched cases should be handled.

You can create a Matcher using either:

  • Match.type<T>(): Matches against a specific type.
  • Match.value(value): Matches against a specific value.

The Match.type constructor defines a Matcher that operates on a specific type. Once created, you can use patterns like Match.when to define conditions for handling different cases.

Example (Matching Numbers and Strings)

import {
import Match
Match
} from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const
const match: (u: string | number) => string
match
=
import Match
Match
.
const type: <string | number>() => Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<string | number>().
Pipeable.pipe<Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>, Match.Matcher<string | number, Match.Types.Without<number>, string, string, never, any>, Match.Matcher<...>, (u: string | number) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (u: string | number) => string): (u: string | number) => string (+21 overloads)
pipe
(
// Match when the value is a number
import Match
Match
.
const when: <string | number, Refinement<unknown, number>, any, (n: number) => string>(pattern: Refinement<unknown, number>, f: (n: number) => string) => <I, F, A, Pr>(self: Match.Matcher<I, ... 4 more ..., any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const number: Refinement<unknown, number>

Matches values of type number.

@since1.0.0

number
, (
n: number
n
) => `number: ${
n: number
n
}`),
// Match when the value is a string
import Match
Match
.
const when: <string, Refinement<unknown, string>, any, (s: string) => string>(pattern: Refinement<unknown, string>, f: (s: string) => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const string: Refinement<unknown, string>

Matches values of type string.

@since1.0.0

string
, (
s: string
s
) => `string: ${
s: string
s
}`),
// Ensure all possible cases are handled
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>

The Match.exhaustive method finalizes the pattern matching process by ensuring that all possible cases are accounted for. If any case is missing, TypeScript will produce a type error. This is particularly useful when working with unions, as it helps prevent unintended gaps in pattern matching.

@example

// Title: Ensuring All Cases Are Covered
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Mark the match as exhaustive, ensuring all cases are handled
// TypeScript will throw an error if any case is missing
//

@since1.0.0

exhaustive
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const match: (u: string | number) => string
match
(0))
// Output: "number: 0"
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const match: (u: string | number) => string
match
("hello"))
// Output: "string: hello"

Instead of creating a matcher for a type, you can define one directly from a specific value using Match.value.

Example (Matching an Object by Property)

import {
import Match
Match
} from "effect"
const
const input: {
name: string;
age: number;
}
input
= {
name: string
name
: "John",
age: number
age
: 30 }
// Create a matcher for the specific object
const
const result: string
result
=
import Match
Match
.
const value: <{
name: string;
age: number;
}>(i: {
name: string;
age: number;
}) => Match.Matcher<{
name: string;
age: number;
}, Match.Types.Without<never>, {
name: string;
age: number;
}, never, {
name: string;
age: number;
}, any>

Creates a matcher from a specific value.

Details

This function allows you to define a Matcher directly from a given value, rather than from a type. This is useful when working with known values, enabling structured pattern matching on objects, primitives, or any data structure.

Once the matcher is created, you can use pattern-matching functions like

when

to define how different cases should be handled.

@seetype for creating a matcher from a specific type.

@example

// Title: Matching an Object by Property
import { Match } from "effect"
const input = { name: "John", age: 30 }
// Create a matcher for the specific object
const result = Match.value(input).pipe(
// Match when the 'name' property is "John"
Match.when(
{ name: "John" },
(user) => `${user.name} is ${user.age} years old`
),
// Provide a fallback if no match is found
Match.orElse(() => "Oh, not John")
)
console.log(result)
// Output: "John is 30 years old"

@since1.0.0

value
(
const input: {
name: string;
age: number;
}
input
).
Pipeable.pipe<Match.Matcher<{
name: string;
age: number;
}, Match.Types.Without<never>, {
name: string;
age: number;
}, never, {
name: string;
age: number;
}, any>, Match.Matcher<{
name: string;
age: number;
}, ... 4 more ..., any>, string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => string): string (+21 overloads)
pipe
(
// Match when the 'name' property is "John"
import Match
Match
.
const when: <{
name: string;
age: number;
}, {
readonly name: "John";
}, any, (user: {
name: "John";
age: number;
}) => string>(pattern: {
readonly name: "John";
}, f: (user: {
name: "John";
age: number;
}) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
{
name: "John"
name
: "John" },
(
user: {
name: "John";
age: number;
}
user
) => `${
user: {
name: "John";
age: number;
}
user
.
name: "John"
name
} is ${
user: {
name: "John";
age: number;
}
user
.
age: number
age
} years old`
),
// Provide a fallback if no match is found
import Match
Match
.
const orElse: <{
name: string;
age: number;
}, any, () => string>(f: () => string) => <I, R, A, Pr>(self: Match.Matcher<I, R, {
name: string;
age: number;
}, A, Pr, any>) => [Pr] extends [...] ? (input: I) => Unify<...> : Unify<...>

Provides a fallback value when no patterns match.

Details

This function ensures that a matcher always returns a valid result, even if no defined patterns match. It acts as a default case, similar to the default clause in a switch statement or the final else in an if-else chain.

@example

// Title: Providing a Default Value When No Patterns Match
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is "a"
Match.when("a", () => "ok"),
// Fallback when no patterns match
Match.orElse(() => "fallback")
)
console.log(match("a"))
// Output: "ok"
console.log(match("b"))
// Output: "fallback"

@since1.0.0

orElse
(() => "Oh, not John")
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

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

You can use Match.withReturnType<T>() to ensure that all branches return a specific type.

Example (Validating Return Type Consistency)

This example enforces that every matching branch returns a string.

import {
import Match
Match
} from "effect"
const
const match: (u: {
a: number;
} | {
b: string;
}) => string
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>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<{
a: number
a
: number } | {
b: string
b
: string }>().
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 ..., string>, Match.Matcher<...>, Match.Matcher<...>, (u: {
a: number;
} | {
b: string;
}) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => Match.Matcher<...>, de: (_: Match.Matcher<...>) => (u: {
a: number;
} | {
b: string;
}) => string): (u: {
a: number;
} | {
b: string;
}) => string (+21 overloads)
pipe
(
// Ensure all branches return a string
import Match
Match
.
const withReturnType: <string>() => <I, F, R, A, Pr, _>(self: Match.Matcher<I, F, R, A, Pr, _>) => [string] extends [[A] extends [never] ? any : A] ? Match.Matcher<...> : "withReturnType constraint does not extend Result type"

Ensures that all branches of a matcher return a specific type.

Details

This function enforces a consistent return type across all pattern-matching branches. By specifying a return type, TypeScript will check that every matching condition produces a value of the expected type.

Important: This function must be the first step in the matcher pipeline. If used later, TypeScript will not enforce type consistency correctly.

@example

// Title: Validating Return Type Consistency
import { Match } from "effect"
const match = Match.type<{ a: number } | { b: string }>().pipe(
// Ensure all branches return a string
Match.withReturnType<string>(),
// ❌ Type error: 'number' is not assignable to type 'string'
//

@since1.0.0

withReturnType
<string>(),
// ❌ Type error: 'number' is not assignable to type 'string'
// @ts-expect-error
import Match
Match
.
const when: <{
a: number;
} | {
b: string;
}, {
readonly a: Refinement<unknown, number>;
}, string, (_: {
a: number;
}) => string>(pattern: {
readonly a: Refinement<unknown, number>;
}, f: (_: {
a: number;
}) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
a: Refinement<unknown, number>
a
:
import Match
Match
.
const number: Refinement<unknown, number>

Matches values of type number.

@since1.0.0

number
}, (
_: {
a: number;
}
_
) =>
_: {
a: number;
}
_
.
a: number
a
),
// ✅ Correct: returns a string
import Match
Match
.
const when: <{
b: string;
}, {
readonly b: Refinement<unknown, string>;
}, string, (_: {
b: string;
}) => string>(pattern: {
readonly b: Refinement<unknown, string>;
}, f: (_: {
b: string;
}) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
b: Refinement<unknown, string>
b
:
import Match
Match
.
const string: Refinement<unknown, string>

Matches values of type string.

@since1.0.0

string
}, (
_: {
b: string;
}
_
) =>
_: {
b: string;
}
_
.
b: string
b
),
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>

The Match.exhaustive method finalizes the pattern matching process by ensuring that all possible cases are accounted for. If any case is missing, TypeScript will produce a type error. This is particularly useful when working with unions, as it helps prevent unintended gaps in pattern matching.

@example

// Title: Ensuring All Cases Are Covered
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Mark the match as exhaustive, ensuring all cases are handled
// TypeScript will throw an error if any case is missing
//

@since1.0.0

exhaustive
)

The Match.when function allows you to define conditions for matching values. It supports both direct value comparisons and predicate functions.

Example (Matching with Values and Predicates)

import {
import Match
Match
} from "effect"
// Create a matcher for objects with an "age" property
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>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<{
age: number
age
: number }>().
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>, Match.Matcher<...>, (input: {
age: number;
}) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (input: {
age: number;
}) => string): (input: {
age: number;
}) => string (+21 overloads)
pipe
(
// Match when age is greater than 18
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<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
age: (age: number) => boolean
age
: (
age: number
age
) =>
age: number
age
> 18 }, (
user: {
age: number;
}
user
) => `Age: ${
user: {
age: number;
}
user
.
age: number
age
}`),
// Match when age is exactly 18
import Match
Match
.
const when: <{
age: number;
}, {
readonly age: 18;
}, any, () => string>(pattern: {
readonly age: 18;
}, f: () => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, {
age: number;
}, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
age: 18
age
: 18 }, () => "You can vote"),
// Fallback case for all other ages
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<...>

Provides a fallback value when no patterns match.

Details

This function ensures that a matcher always returns a valid result, even if no defined patterns match. It acts as a default case, similar to the default clause in a switch statement or the final else in an if-else chain.

@example

// Title: Providing a Default Value When No Patterns Match
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is "a"
Match.when("a", () => "ok"),
// Fallback when no patterns match
Match.orElse(() => "fallback")
)
console.log(match("a"))
// Output: "ok"
console.log(match("b"))
// Output: "fallback"

@since1.0.0

orElse
((
user: {
age: number;
}
user
) => `${
user: {
age: number;
}
user
.
age: number
age
} is too young`)
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const match: (input: {
age: number;
}) => string
match
({
age: number
age
: 20 }))
// Output: "Age: 20"
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const match: (input: {
age: number;
}) => string
match
({
age: number
age
: 18 }))
// Output: "You can vote"
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

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

The Match.not function allows you to exclude specific values while matching all others.

Example (Ignoring a Specific Value)

import {
import Match
Match
} from "effect"
// Create a matcher for string or number values
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>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<string | number>().
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
(
// Match any value except "hi", returning "ok"
import Match
Match
.
const not: <string | number, "hi", any, () => string>(pattern: "hi", f: () => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string | number, A, Pr, any>) => Match.Matcher<I, ... 4 more ..., any>

Excludes a specific value from matching while allowing all others.

Details

This function is useful when you need to handle all values except one or more specific cases. Instead of listing all possible matches manually, this function simplifies the logic by allowing you to specify values to exclude. Any excluded value will bypass the provided function and continue matching through other cases.

@example

// Title: Ignoring a Specific Value
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match any value except "hi", returning "ok"
Match.not("hi", () => "ok"),
// Fallback case for when the value is "hi"
Match.orElse(() => "fallback")
)
console.log(match("hello"))
// Output: "ok"
console.log(match("hi"))
// Output: "fallback"

@since1.0.0

not
("hi", () => "ok"),
// Fallback case for when the value is "hi"
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<...>

Provides a fallback value when no patterns match.

Details

This function ensures that a matcher always returns a valid result, even if no defined patterns match. It acts as a default case, similar to the default clause in a switch statement or the final else in an if-else chain.

@example

// Title: Providing a Default Value When No Patterns Match
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is "a"
Match.when("a", () => "ok"),
// Fallback when no patterns match
Match.orElse(() => "fallback")
)
console.log(match("a"))
// Output: "ok"
console.log(match("b"))
// Output: "fallback"

@since1.0.0

orElse
(() => "fallback")
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

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

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

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

The Match.tag function allows pattern matching based on the _tag field in a Discriminated Union. You can specify multiple tags to match within a single pattern.

Example (Matching a Discriminated Union by Tag)

import {
import Match
Match
} from "effect"
type
type Event = {
readonly _tag: "fetch";
} | {
readonly _tag: "success";
readonly data: string;
} | {
readonly _tag: "error";
readonly error: Error;
} | {
readonly _tag: "cancel";
}
Event
=
| { readonly
_tag: "fetch"
_tag
: "fetch" }
| { readonly
_tag: "success"
_tag
: "success"; readonly
data: string
data
: string }
| { readonly
_tag: "error"
_tag
: "error"; readonly
error: Error
error
:
interface Error
Error
}
| { readonly
_tag: "cancel"
_tag
: "cancel" }
// Create a Matcher for Either<number, string>
const
const match: (u: Event) => string
match
=
import Match
Match
.
const type: <Event>() => Match.Matcher<Event, Match.Types.Without<never>, Event, never, never, any>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<
type Event = {
readonly _tag: "fetch";
} | {
readonly _tag: "success";
readonly data: string;
} | {
readonly _tag: "error";
readonly error: Error;
} | {
readonly _tag: "cancel";
}
Event
>().
Pipeable.pipe<Match.Matcher<Event, Match.Types.Without<never>, Event, never, never, any>, Match.Matcher<Event, Match.Types.Without<{
readonly _tag: "fetch";
} | {
readonly _tag: "success";
readonly data: string;
}>, {
...;
} | {
...;
}, string, never, any>, Match.Matcher<...>, Match.Matcher<...>, (u: Event) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => Match.Matcher<...>, de: (_: Match.Matcher<...>) => (u: Event) => string): (u: Event) => string (+21 overloads)
pipe
(
// Match either "fetch" or "success"
import Match
Match
.
const tag: <Event, "fetch" | "success", any, string>(...pattern: [first: "fetch" | "success", ...values: ("fetch" | "success")[], f: (_: {
readonly _tag: "fetch";
} | {
readonly _tag: "success";
readonly data: string;
}) => string]) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

The Match.tag function allows pattern matching based on the _tag field in a Discriminated Union. You can specify multiple tags to match within a single pattern.

Note

The Match.tag function relies on the convention within the Effect ecosystem of naming the tag field as "_tag". Ensure that your discriminated unions follow this naming convention for proper functionality.

@example

// Title: Matching a Discriminated Union by Tag
import { Match } from "effect"
type Event =
| { readonly _tag: "fetch" }
| { readonly _tag: "success"; readonly data: string }
| { readonly _tag: "error"; readonly error: Error }
| { readonly _tag: "cancel" }
// Create a Matcher for Either<number, string>
const match = Match.type<Event>().pipe(
// Match either "fetch" or "success"
Match.tag("fetch", "success", () => `Ok!`),
// Match "error" and extract the error message
Match.tag("error", (event) => `Error: ${event.error.message}`),
// Match "cancel"
Match.tag("cancel", () => "Cancelled"),
Match.exhaustive
)
console.log(match({ _tag: "success", data: "Hello" }))
// Output: "Ok!"
console.log(match({ _tag: "error", error: new Error("Oops!") }))
// Output: "Error: Oops!"

@since1.0.0

tag
("fetch", "success", () => `Ok!`),
// Match "error" and extract the error message
import Match
Match
.
const tag: <{
readonly _tag: "error";
readonly error: Error;
} | {
readonly _tag: "cancel";
}, "error", any, string>(...pattern: [first: "error", ...values: "error"[], f: (_: {
readonly _tag: "error";
readonly error: Error;
}) => string]) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

The Match.tag function allows pattern matching based on the _tag field in a Discriminated Union. You can specify multiple tags to match within a single pattern.

Note

The Match.tag function relies on the convention within the Effect ecosystem of naming the tag field as "_tag". Ensure that your discriminated unions follow this naming convention for proper functionality.

@example

// Title: Matching a Discriminated Union by Tag
import { Match } from "effect"
type Event =
| { readonly _tag: "fetch" }
| { readonly _tag: "success"; readonly data: string }
| { readonly _tag: "error"; readonly error: Error }
| { readonly _tag: "cancel" }
// Create a Matcher for Either<number, string>
const match = Match.type<Event>().pipe(
// Match either "fetch" or "success"
Match.tag("fetch", "success", () => `Ok!`),
// Match "error" and extract the error message
Match.tag("error", (event) => `Error: ${event.error.message}`),
// Match "cancel"
Match.tag("cancel", () => "Cancelled"),
Match.exhaustive
)
console.log(match({ _tag: "success", data: "Hello" }))
// Output: "Ok!"
console.log(match({ _tag: "error", error: new Error("Oops!") }))
// Output: "Error: Oops!"

@since1.0.0

tag
("error", (
event: {
readonly _tag: "error";
readonly error: Error;
}
event
) => `Error: ${
event: {
readonly _tag: "error";
readonly error: Error;
}
event
.
error: Error
error
.
Error.message: string
message
}`),
// Match "cancel"
import Match
Match
.
const tag: <{
readonly _tag: "cancel";
}, "cancel", any, string>(...pattern: [first: "cancel", ...values: "cancel"[], f: (_: {
readonly _tag: "cancel";
}) => string]) => <I, F, A, Pr>(self: Match.Matcher<I, F, {
readonly _tag: "cancel";
}, A, Pr, any>) => Match.Matcher<...>

The Match.tag function allows pattern matching based on the _tag field in a Discriminated Union. You can specify multiple tags to match within a single pattern.

Note

The Match.tag function relies on the convention within the Effect ecosystem of naming the tag field as "_tag". Ensure that your discriminated unions follow this naming convention for proper functionality.

@example

// Title: Matching a Discriminated Union by Tag
import { Match } from "effect"
type Event =
| { readonly _tag: "fetch" }
| { readonly _tag: "success"; readonly data: string }
| { readonly _tag: "error"; readonly error: Error }
| { readonly _tag: "cancel" }
// Create a Matcher for Either<number, string>
const match = Match.type<Event>().pipe(
// Match either "fetch" or "success"
Match.tag("fetch", "success", () => `Ok!`),
// Match "error" and extract the error message
Match.tag("error", (event) => `Error: ${event.error.message}`),
// Match "cancel"
Match.tag("cancel", () => "Cancelled"),
Match.exhaustive
)
console.log(match({ _tag: "success", data: "Hello" }))
// Output: "Ok!"
console.log(match({ _tag: "error", error: new Error("Oops!") }))
// Output: "Error: Oops!"

@since1.0.0

tag
("cancel", () => "Cancelled"),
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>

The Match.exhaustive method finalizes the pattern matching process by ensuring that all possible cases are accounted for. If any case is missing, TypeScript will produce a type error. This is particularly useful when working with unions, as it helps prevent unintended gaps in pattern matching.

@example

// Title: Ensuring All Cases Are Covered
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Mark the match as exhaustive, ensuring all cases are handled
// TypeScript will throw an error if any case is missing
//

@since1.0.0

exhaustive
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const match: (u: Event) => string
match
({
_tag: "success"
_tag
: "success",
data: string
data
: "Hello" }))
// Output: "Ok!"
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const match: (u: Event) => string
match
({
_tag: "error"
_tag
: "error",
error: Error
error
: new
var Error: ErrorConstructor
new (message?: string) => Error
Error
("Oops!") }))
// Output: "Error: Oops!"

The Match module provides built-in predicates for common types, such as Match.number, Match.string, and Match.boolean. These predicates simplify the process of matching against primitive types.

Example (Using Built-in Predicates for Property Keys)

import {
import Match
Match
} from "effect"
const
const matchPropertyKey: (u: PropertyKey) => string
matchPropertyKey
=
import Match
Match
.
const type: <PropertyKey>() => Match.Matcher<PropertyKey, Match.Types.Without<never>, PropertyKey, never, never, any>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<
type PropertyKey = string | number | symbol
PropertyKey
>().
Pipeable.pipe<Match.Matcher<PropertyKey, Match.Types.Without<never>, PropertyKey, never, never, any>, Match.Matcher<PropertyKey, Match.Types.Without<number>, string | symbol, string, never, any>, Match.Matcher<...>, Match.Matcher<...>, (u: PropertyKey) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => Match.Matcher<...>, de: (_: Match.Matcher<...>) => (u: PropertyKey) => string): (u: PropertyKey) => string (+21 overloads)
pipe
(
// Match when the value is a number
import Match
Match
.
const when: <PropertyKey, Refinement<unknown, number>, any, (n: number) => string>(pattern: Refinement<unknown, number>, f: (n: number) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const number: Refinement<unknown, number>

Matches values of type number.

@since1.0.0

number
, (
n: number
n
) => `Key is a number: ${
n: number
n
}`),
// Match when the value is a string
import Match
Match
.
const when: <string | symbol, Refinement<unknown, string>, any, (s: string) => string>(pattern: Refinement<unknown, string>, f: (s: string) => string) => <I, F, A, Pr>(self: Match.Matcher<I, ... 4 more ..., any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const string: Refinement<unknown, string>

Matches values of type string.

@since1.0.0

string
, (
s: string
s
) => `Key is a string: ${
s: string
s
}`),
// Match when the value is a symbol
import Match
Match
.
const when: <symbol, Refinement<unknown, symbol>, any, (s: symbol) => string>(pattern: Refinement<unknown, symbol>, f: (s: symbol) => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, symbol, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const symbol: Refinement<unknown, symbol>

Matches values of type symbol.

@since1.0.0

symbol
, (
s: symbol
s
) => `Key is a symbol: ${
var String: StringConstructor
(value?: any) => string

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

String
(
s: symbol
s
)}`),
// Ensure all possible cases are handled
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>

The Match.exhaustive method finalizes the pattern matching process by ensuring that all possible cases are accounted for. If any case is missing, TypeScript will produce a type error. This is particularly useful when working with unions, as it helps prevent unintended gaps in pattern matching.

@example

// Title: Ensuring All Cases Are Covered
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Mark the match as exhaustive, ensuring all cases are handled
// TypeScript will throw an error if any case is missing
//

@since1.0.0

exhaustive
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const matchPropertyKey: (u: PropertyKey) => string
matchPropertyKey
(42))
// Output: "Key is a number: 42"
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const matchPropertyKey: (u: PropertyKey) => string
matchPropertyKey
("username"))
// Output: "Key is a string: username"
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const matchPropertyKey: (u: PropertyKey) => string
matchPropertyKey
(
var Symbol: SymbolConstructor
(description?: string | number) => symbol

Returns a new unique Symbol value.

@paramdescription Description of the new Symbol object.

Symbol
("id")))
// Output: "Key is a symbol: Symbol(id)"
PredicateDescription
Match.stringMatches values of type string.
Match.nonEmptyStringMatches non-empty strings.
Match.numberMatches values of type number.
Match.booleanMatches values of type boolean.
Match.bigintMatches values of type bigint.
Match.symbolMatches values of type symbol.
Match.dateMatches values that are instances of Date.
Match.recordMatches objects where keys are string or symbol and values are unknown.
Match.nullMatches the value null.
Match.undefinedMatches the value undefined.
Match.definedMatches any defined (non-null and non-undefined) value.
Match.anyMatches any value without restrictions.
Match.is(...values)Matches a specific set of literal values (e.g., Match.is("a", 42, true)).
Match.instanceOf(Class)Matches instances of a given class.

The Match.exhaustive method finalizes the pattern matching process by ensuring that all possible cases are accounted for. If any case is missing, TypeScript will produce a type error. This is particularly useful when working with unions, as it helps prevent unintended gaps in pattern matching.

Example (Ensuring All Cases Are Covered)

import {
import Match
Match
} from "effect"
// Create a matcher for string or number values
const
const match: never
match
=
import Match
Match
.
const type: <string | number>() => Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<string | number>().
Pipeable.pipe<Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>, never, never>(this: Match.Matcher<...>, ab: (_: Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>) => never, bc: (_: never) => never): never (+21 overloads)
pipe
(
// Match when the value is a number
import Match
Match
.
const when: <string | number, Refinement<unknown, number>, any, (n: number) => string>(pattern: Refinement<unknown, number>, f: (n: number) => string) => <I, F, A, Pr>(self: Match.Matcher<I, ... 4 more ..., any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
(
import Match
Match
.
const number: Refinement<unknown, number>

Matches values of type number.

@since1.0.0

number
, (
n: number
n
) => `number: ${
n: number
n
}`),
// Mark the match as exhaustive, ensuring all cases are handled
// TypeScript will throw an error if any case is missing
// @ts-expect-error Type 'string' is not assignable to type 'never'
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>

The Match.exhaustive method finalizes the pattern matching process by ensuring that all possible cases are accounted for. If any case is missing, TypeScript will produce a type error. This is particularly useful when working with unions, as it helps prevent unintended gaps in pattern matching.

@example

// Title: Ensuring All Cases Are Covered
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Mark the match as exhaustive, ensuring all cases are handled
// TypeScript will throw an error if any case is missing
//

@since1.0.0

exhaustive
)

The Match.orElse method defines a fallback value to return when no other patterns match. This ensures that the matcher always produces a valid result.

Example (Providing a Default Value When No Patterns Match)

import {
import Match
Match
} from "effect"
// Create a matcher for string or number values
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>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<string | number>().
Pipeable.pipe<Match.Matcher<string | number, Match.Types.Without<never>, string | number, never, never, any>, Match.Matcher<string | number, Match.Types.Without<"a">, 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
(
// Match when the value is "a"
import Match
Match
.
const when: <string | number, "a", any, () => string>(pattern: "a", f: () => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string | number, A, Pr, any>) => Match.Matcher<I, ... 4 more ..., any>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
("a", () => "ok"),
// Fallback when no patterns match
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<...>

Provides a fallback value when no patterns match.

Details

This function ensures that a matcher always returns a valid result, even if no defined patterns match. It acts as a default case, similar to the default clause in a switch statement or the final else in an if-else chain.

@example

// Title: Providing a Default Value When No Patterns Match
import { Match } from "effect"
// Create a matcher for string or number values
const match = Match.type<string | number>().pipe(
// Match when the value is "a"
Match.when("a", () => "ok"),
// Fallback when no patterns match
Match.orElse(() => "fallback")
)
console.log(match("a"))
// Output: "ok"
console.log(match("b"))
// Output: "fallback"

@since1.0.0

orElse
(() => "fallback")
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const match: (input: string | number) => string
match
("a"))
// Output: "ok"
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

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

Match.option wraps the match result in an Option. If a match is found, it returns Some(value), otherwise, it returns None.

Example (Extracting a User Role with Option)

import {
import Match
Match
} from "effect"
type
type User = {
readonly role: "admin" | "editor" | "viewer";
}
User
= { readonly
role: "admin" | "editor" | "viewer"
role
: "admin" | "editor" | "viewer" }
// Create a matcher to extract user roles
const
const getRole: (input: User) => Option<string>
getRole
=
import Match
Match
.
const type: <User>() => Match.Matcher<User, Match.Types.Without<never>, User, never, never, any>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<
type User = {
readonly role: "admin" | "editor" | "viewer";
}
User
>().
Pipeable.pipe<Match.Matcher<User, Match.Types.Without<never>, User, never, never, any>, Match.Matcher<User, Match.Types.Without<{
readonly role: "admin";
}>, User, string, never, any>, Match.Matcher<...>, (input: User) => Option<...>>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (input: User) => Option<...>): (input: User) => Option<...> (+21 overloads)
pipe
(
import Match
Match
.
const when: <User, {
readonly role: "admin";
}, any, () => string>(pattern: {
readonly role: "admin";
}, f: () => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, User, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
role: "admin"
role
: "admin" }, () => "Has full access"),
import Match
Match
.
const when: <User, {
readonly role: "editor";
}, any, () => string>(pattern: {
readonly role: "editor";
}, f: () => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, User, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
role: "editor"
role
: "editor" }, () => "Can edit content"),
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>>

Wraps the match result in an Option, representing an optional match.

Details

This function ensures that the result of a matcher is wrapped in an Option, making it easy to handle cases where no pattern matches. If a match is found, it returns Some(value), otherwise, it returns None.

This is useful in cases where a missing match is expected and should be handled explicitly rather than throwing an error or returning a default value.

@example

// Title: Extracting a User Role with Option
import { Match } from "effect"
type User = { readonly role: "admin" | "editor" | "viewer" }
// Create a matcher to extract user roles
const getRole = Match.type<User>().pipe(
Match.when({ role: "admin" }, () => "Has full access"),
Match.when({ role: "editor" }, () => "Can edit content"),
Match.option // Wrap the result in an Option
)
console.log(getRole({ role: "admin" }))
// Output: { _id: 'Option', _tag: 'Some', value: 'Has full access' }
console.log(getRole({ role: "viewer" }))
// Output: { _id: 'Option', _tag: 'None' }

@since1.0.0

option
// Wrap the result in an Option
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const getRole: (input: User) => Option<string>
getRole
({
role: "admin" | "editor" | "viewer"
role
: "admin" }))
// Output: { _id: 'Option', _tag: 'Some', value: 'Has full access' }
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const getRole: (input: User) => Option<string>
getRole
({
role: "admin" | "editor" | "viewer"
role
: "viewer" }))
// Output: { _id: 'Option', _tag: 'None' }

The Match.either method wraps the result in an Either, providing a structured way to distinguish between matched and unmatched cases. If a match is found, it returns Right(value), otherwise, it returns Left(no match).

Example (Extracting a User Role with Either)

import {
import Match
Match
} from "effect"
type
type User = {
readonly role: "admin" | "editor" | "viewer";
}
User
= { readonly
role: "admin" | "editor" | "viewer"
role
: "admin" | "editor" | "viewer" }
// Create a matcher to extract user roles
const
const getRole: (input: User) => Either<string, User>
getRole
=
import Match
Match
.
const type: <User>() => Match.Matcher<User, Match.Types.Without<never>, User, never, never, any>

Creates a matcher for a specific type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like

when

to define how different values should be processed.

@seevalue for creating a matcher from a specific value.

@example

// Title: Matching Numbers and Strings
import { Match } from "effect"
// Create a matcher for values that are either strings or numbers
//
// ┌─── (u: string | number) => string
// ▼
const match = Match.type<string | number>().pipe(
// Match when the value is a number
Match.when(Match.number, (n) => `number: ${n}`),
// Match when the value is a string
Match.when(Match.string, (s) => `string: ${s}`),
// Ensure all possible cases are handled
Match.exhaustive
)
console.log(match(0))
// Output: "number: 0"
console.log(match("hello"))
// Output: "string: hello"

@since1.0.0

type
<
type User = {
readonly role: "admin" | "editor" | "viewer";
}
User
>().
Pipeable.pipe<Match.Matcher<User, Match.Types.Without<never>, User, never, never, any>, Match.Matcher<User, Match.Types.Without<{
readonly role: "admin";
}>, User, string, never, any>, Match.Matcher<...>, (input: User) => Either<...>>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (input: User) => Either<...>): (input: User) => Either<...> (+21 overloads)
pipe
(
import Match
Match
.
const when: <User, {
readonly role: "admin";
}, any, () => string>(pattern: {
readonly role: "admin";
}, f: () => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, User, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
role: "admin"
role
: "admin" }, () => "Has full access"),
import Match
Match
.
const when: <User, {
readonly role: "editor";
}, any, () => string>(pattern: {
readonly role: "editor";
}, f: () => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, User, A, Pr, any>) => Match.Matcher<...>

Defines a condition for matching values.

Details

This function enables pattern matching by checking whether a given value satisfies a condition. It supports both direct value comparisons and predicate functions. If the condition is met, the associated function is executed.

This function is useful when defining matchers that need to check for specific values or apply logical conditions to determine a match. It works well with structured objects and primitive types.

@seewhenOr Use this when multiple patterns should match in a single condition.

@seewhenAnd Use this when a value must match all provided patterns.

@seeorElse Provides a fallback when no patterns match.

@example

// Title: Matching with Values and Predicates
import { Match } from "effect"
// Create a matcher for objects with an "age" property
const match = Match.type<{ age: number }>().pipe(
// Match when age is greater than 18
Match.when({ age: (age) => age > 18 }, (user) => `Age: ${user.age}`),
// Match when age is exactly 18
Match.when({ age: 18 }, () => "You can vote"),
// Fallback case for all other ages
Match.orElse((user) => `${user.age} is too young`)
)
console.log(match({ age: 20 }))
// Output: "Age: 20"
console.log(match({ age: 18 }))
// Output: "You can vote"
console.log(match({ age: 4 }))
// Output: "4 is too young"

@since1.0.0

when
({
role: "editor"
role
: "editor" }, () => "Can edit content"),
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>

Wraps the match result in an Either, distinguishing matched and unmatched cases.

Details

This function ensures that the result of a matcher is always wrapped in an Either, allowing clear differentiation between successful matches (Right(value)) and cases where no pattern matched (Left(unmatched value)).

This approach is particularly useful when handling optional values or when an unmatched case should be explicitly handled rather than returning a default value or throwing an error.

@example

// Title: Extracting a User Role with Either
import { Match } from "effect"
type User = { readonly role: "admin" | "editor" | "viewer" }
// Create a matcher to extract user roles
const getRole = Match.type<User>().pipe(
Match.when({ role: "admin" }, () => "Has full access"),
Match.when({ role: "editor" }, () => "Can edit content"),
Match.either // Wrap the result in an Either
)
console.log(getRole({ role: "admin" }))
// Output: { _id: 'Either', _tag: 'Right', right: 'Has full access' }
console.log(getRole({ role: "viewer" }))
// Output: { _id: 'Either', _tag: 'Left', left: { role: 'viewer' } }

@since1.0.0

either
// Wrap the result in an Either
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const getRole: (input: User) => Either<string, User>
getRole
({
role: "admin" | "editor" | "viewer"
role
: "admin" }))
// Output: { _id: 'Either', _tag: 'Right', right: 'Has full access' }
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const getRole: (input: User) => Either<string, User>
getRole
({
role: "admin" | "editor" | "viewer"
role
: "viewer" }))
// Output: { _id: 'Either', _tag: 'Left', left: { role: 'viewer' } }