Skip to content

Pattern Matching

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

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

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

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

Example (Defining a Type Matcher)

import {
import Match
Match
} from "effect"
// Create a Matcher for objects with properties 'a' or 'b'
//
// ┌─── (u: { a: number; } | { b: string; }) => string | number
// ▼
const
const match: (u: {
a: number;
} | {
b: string;
}) => string | number
match
=
import Match
Match
.
const type: <{
a: number;
} | {
b: string;
}>() => Match.Matcher<{
a: number;
} | {
b: string;
}, Match.Types.Without<never>, {
a: number;
} | {
b: string;
}, never, never, any>

@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 ..., any>, Match.Matcher<...>, (u: {
a: number;
} | {
b: string;
}) => string | number>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (u: {
a: number;
} | {
b: string;
}) => string | number): (u: {
a: number;
} | {
b: string;
}) => string | number (+21 overloads)
pipe
(
// Match an object with a numeric property 'a'
import Match
Match
.
const when: <{
a: number;
} | {
b: string;
}, {
readonly a: Refinement<unknown, number>;
}, any, (_: {
a: number;
}) => number>(pattern: {
readonly a: Refinement<unknown, number>;
}, f: (_: {
a: number;
}) => number) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

@since1.0.0

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

@since1.0.0

number
}, (
_: {
a: number;
}
_
) =>
_: {
a: number;
}
_
.
a: number
a
),
// Match an object with a string property 'b'
import Match
Match
.
const when: <{
b: string;
}, {
readonly b: Refinement<unknown, string>;
}, any, (_: {
b: string;
}) => string>(pattern: {
readonly b: Refinement<unknown, string>;
}, f: (_: {
b: string;
}) => string) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

@since1.0.0

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

@since1.0.0

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

@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: {
a: number;
} | {
b: string;
}) => string | number
match
({
a: number
a
: 0 }))
// Output: 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: {
a: number;
} | {
b: string;
}) => string | number
match
({
b: string
b
: "hello" }))
// Output: "hello"

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

Example (Matching Against a Value)

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

@since1.0.0

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

@since1.0.0

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

@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"

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

Example (Using Predicates for Matching)

import {
import Match
Match
} from "effect"
// Define 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>

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

@since1.0.0

when
({
age: (age: number) => boolean
age
: (
age: number
age
) =>
age: number
age
>= 5 }, (
user: {
age: number;
}
user
) => `Age: ${
user: {
age: number;
}
user
.
age: number
age
}`),
// Fallback for when the age condition is not met
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<...>

@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
: 5 }))
// Output: "Age: 5"
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

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

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

Example (Excluding 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>

@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
(
// Exclude the value "hi" and return "a" for anything else
import Match
Match
.
const not: <string | number, "hi", any, (_: string | number) => string>(pattern: "hi", f: (_: string | number) => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string | number, A, Pr, any>) => Match.Matcher<...>

@since1.0.0

not
("hi", (
_: string | number
_
) => "ok"),
// Fallback 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<...>

@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 enables pattern matching against the tag within a Discriminated Union.

Example (Pattern Matching on a Discriminated Union)

import {
import Match
Match
,
import Either

@since2.0.0

@since2.0.0

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

@since1.0.0

type
<
import Either

@since2.0.0

@since2.0.0

Either
.
type Either<R, L = never> = Either.Left<L, R> | Either.Right<L, R>

@since2.0.0

@since2.0.0

Either
<number, string>>().
Pipeable.pipe<Match.Matcher<Either.Either<number, string>, Match.Types.Without<never>, Either.Either<number, string>, never, never, any>, Match.Matcher<Either.Either<number, string>, ... 4 more ..., any>, Match.Matcher<...>, (u: Either.Either<...>) => string | number>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Match.Matcher<...>, cd: (_: Match.Matcher<...>) => (u: Either.Either<...>) => string | number): (u: Either.Either<...>) => string | number (+21 overloads)
pipe
(
// Match when the tag is "Right" and extract the value
import Match
Match
.
const tag: <Either.Either<number, string>, "Right", any, number>(...pattern: [first: "Right", ...values: "Right"[], f: (_: Either.Right<string, number>) => number]) => <I, F, A, Pr>(self: Match.Matcher<I, ... 4 more ..., any>) => Match.Matcher<...>

@since1.0.0

tag
("Right", (
_: Either.Right<string, number>
_
) =>
_: Either.Right<string, number>
_
.
Right<string, number>.right: number
right
),
// Match when the tag is "Left" and extract the error
import Match
Match
.
const tag: <Either.Left<string, number>, "Left", any, string>(...pattern: [first: "Left", ...values: "Left"[], f: (_: Either.Left<string, number>) => string]) => <I, F, A, Pr>(self: Match.Matcher<I, F, Either.Left<...>, A, Pr, any>) => Match.Matcher<...>

@since1.0.0

tag
("Left", (
_: Either.Left<string, number>
_
) =>
_: Either.Left<string, number>
_
.
Left<string, number>.left: string
left
),
// 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>

@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: Either.Either<number, string>) => string | number
match
(
import Either

@since2.0.0

@since2.0.0

Either
.
const right: <number>(right: number) => Either.Either<number, never>

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

@since2.0.0

right
(123)))
// Output: 123
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: Either.Either<number, string>) => string | number
match
(
import Either

@since2.0.0

@since2.0.0

Either
.
const left: <string>(left: string) => Either.Either<never, string>

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

@since2.0.0

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

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

Example (Ensuring All Cases Are Handled)

import {
import Match
Match
,
import Either

@since2.0.0

@since2.0.0

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

@since1.0.0

value
(
import Either

@since2.0.0

@since2.0.0

Either
.
const right: <number>(right: number) => Either.Either<number, never>

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

@since2.0.0

right
(0)).
Pipeable.pipe<Match.Matcher<Either.Either<number, never>, Match.Types.Without<never>, Either.Either<number, never>, never, Either.Either<number, never>, any>, never, never>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => never, bc: (_: never) => never): never (+21 overloads)
pipe
(
import Match
Match
.
const when: <Either.Either<number, never>, {
readonly _tag: "Right";
}, any, (_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
... 14 more ...;
readonly right: number;
}) => number>(pattern: {
readonly _tag: "Right";
}, f: (_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
... 14 more ...;
readonly right: number;
}) => number) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

@since1.0.0

when
({
_tag: "Right"
_tag
: "Right" }, (
_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
[Unify.typeSymbol]?: unknown;
[Unify.unifySymbol]?: Either.EitherUnify<...>;
... 12 more ...;
readonly right: number;
}
_
) =>
_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
[Unify.typeSymbol]?: unknown;
[Unify.unifySymbol]?: Either.EitherUnify<...>;
... 12 more ...;
readonly right: number;
}
_
.
right: number
right
),
// The "Left" case is missing in the match
// Using Match.exhaustive to ensure all cases are considered
// @ts-expect-error
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>

@since1.0.0

exhaustive
// TypeError! Type 'Left<never, number>' is not assignable to type 'never'
)

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

Example (Using orElse as a Fallback)

import {
import Match
Match
} from "effect"
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>

@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<"hi">, string | number, string, never, any>, (input: string | number) => string>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => (input: string | number) => string): (input: string | number) => string (+21 overloads)
pipe
(
// Match when the value is "hi"
import Match
Match
.
const when: <string | number, "hi", any, (_: "hi") => string>(pattern: "hi", f: (_: "hi") => string) => <I, F, A, Pr>(self: Match.Matcher<I, F, string | number, A, Pr, any>) => Match.Matcher<...>

@since1.0.0

when
("hi", (
_: "hi"
_
) => "hello"),
// 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<...>

@since1.0.0

orElse
(() => "I literally do not understand")
)
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: "hello"
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: "I literally do not understand"

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

Example (Wrapping Match Result in an Option)

import {
import Match
Match
,
import Either

@since2.0.0

@since2.0.0

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

@since1.0.0

value
(
import Either

@since2.0.0

@since2.0.0

Either
.
const right: <number>(right: number) => Either.Either<number, never>

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

@since2.0.0

right
(0)).
Pipeable.pipe<Match.Matcher<Either.Either<number, never>, Match.Types.Without<never>, Either.Either<number, never>, never, Either.Either<number, never>, any>, Match.Matcher<...>, Option<...>>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => Option<...>): Option<...> (+21 overloads)
pipe
(
// Match when the tag is "Right" and extract the value
import Match
Match
.
const when: <Either.Either<number, never>, {
readonly _tag: "Right";
}, any, (_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
... 14 more ...;
readonly right: number;
}) => number>(pattern: {
readonly _tag: "Right";
}, f: (_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
... 14 more ...;
readonly right: number;
}) => number) => <I, F, A, Pr>(self: Match.Matcher<...>) => Match.Matcher<...>

@since1.0.0

when
({
_tag: "Right"
_tag
: "Right" }, (
_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
[Unify.typeSymbol]?: unknown;
[Unify.unifySymbol]?: Either.EitherUnify<...>;
... 12 more ...;
readonly right: number;
}
_
) =>
_: {
_tag: "Right";
readonly [TypeId]: {
readonly _R: Covariant<number>;
readonly _L: Covariant<never>;
};
[Unify.typeSymbol]?: unknown;
[Unify.unifySymbol]?: Either.EitherUnify<...>;
... 12 more ...;
readonly right: number;
}
_
.
right: number
right
),
// Wrap the result in an Option
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>>

@since1.0.0

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 result: Option<number>
result
)
// Output: { _id: 'Option', _tag: 'Some', value: 0 }

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

Example (Wrapping Match Result in an Either)

import {
import Match
Match
} from "effect"
const
const match: (input: string) => Either<number, string>
match
=
import Match
Match
.
const type: <string>() => Match.Matcher<string, Match.Types.Without<never>, string, never, never, any>

@since1.0.0

type
<string>().
Pipeable.pipe<Match.Matcher<string, Match.Types.Without<never>, string, never, never, any>, Match.Matcher<string, Match.Types.Without<"hi">, string, number, never, any>, (input: string) => Either<...>>(this: Match.Matcher<...>, ab: (_: Match.Matcher<...>) => Match.Matcher<...>, bc: (_: Match.Matcher<...>) => (input: string) => Either<...>): (input: string) => Either<...> (+21 overloads)
pipe
(
// Match "hi" and return the length of the string
import Match
Match
.
const when: <string, "hi", any, (_: "hi") => number>(pattern: "hi", f: (_: "hi") => number) => <I, F, A, Pr>(self: Match.Matcher<I, F, string, A, Pr, any>) => Match.Matcher<I, ... 4 more ..., any>

@since1.0.0

when
("hi", (
_: "hi"
_
) =>
_: "hi"
_
.
String.length: number

Returns the length of a String object.

length
),
// Wrap the result in an Either
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>

@since1.0.0

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 match: (input: string) => Either<number, string>
match
("hi"))
// Output: { _id: 'Either', _tag: 'Right', right: 2 }
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) => Either<number, string>
match
("shigidigi"))
// Output: { _id: 'Either', _tag: 'Left', left: 'shigidigi' }