Skip to content

Getting Started

You can import the necessary types and functions from the effect/Schema module:

Example (Namespace Import)

import * as Schema from "effect/Schema"

Example (Named Import)

import { Schema } from "effect"

One common way to define a Schema is by utilizing the Struct constructor. This constructor allows you to create a new schema that outlines an object with specific properties. Each property in the object is defined by its own schema, which specifies the data type and any validation rules.

Example (Defining a Simple Object Schema)

This Person schema describes an object with a name (string) and age (number) property:

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})

Once you’ve defined a schema (Schema<Type, Encoded, Context>), you can extract the inferred type Type in two ways:

  1. Using the Schema.Type utility
  2. Accessing the Type field directly on your schema

Example (Extracting Inferred Type)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// 1. Using the Schema.Type utility
type
type Person = {
readonly name: string;
readonly age: number;
}
Person
=
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Type<S> = S extends Schema.Schema.Variance<infer A, infer _I, infer _R> ? A : never

@since3.10.0

Type
<typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
>
// 2. Accessing the Type field directly
type
type Person2 = {
readonly name: string;
readonly age: number;
}
Person2
= typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
.
Schema<{ readonly name: string; readonly age: number; }, { readonly name: string; readonly age: number; }, never>.Type: {
readonly name: string;
readonly age: number;
}
Type

The resulting type will look like this:

type Person = {
readonly name: string
readonly age: number
}

Alternatively, you can extract the Person type using the interface keyword, which may improve readability and performance in some cases.

Example (Extracting Type with an Interface)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
interface
interface Person
Person
extends
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Type<S> = S extends Schema.Schema.Variance<infer A, infer _I, infer _R> ? A : never

@since3.10.0

Type
<typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
> {}

Both approaches yield the same result, but using an interface provides benefits such as performance advantages and improved readability.

In a Schema<Type, Encoded, Context>, the Encoded type can differ from the Type type, representing the format in which data is encoded. You can extract the Encoded type in two ways:

  1. Using the Schema.Encoded utility
  2. Accessing the Encoded field directly on the schema

Example (Extracting the Encoded Type)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
// a schema that decodes a string to a number
age: typeof Schema.NumberFromString
age
:
import Schema
Schema
.
class NumberFromString

This schema transforms a string into a number by parsing the string using the parse function of the effect/Number module.

It returns an error if the value can't be converted (for example when non-numeric characters are provided).

The following special string values are supported: "NaN", "Infinity", "-Infinity".

@since3.10.0

NumberFromString
})
// 1. Using the Schema.Encoded utility
type
type PersonEncoded = {
readonly name: string;
readonly age: string;
}
PersonEncoded
=
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Encoded<S> = S extends Schema.Schema.Variance<infer _A, infer I, infer _R> ? I : never

@since3.10.0

Encoded
<typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
Person
>
// 2. Accessing the Encoded field directly
type
type PersonEncoded2 = {
readonly name: string;
readonly age: string;
}
PersonEncoded2
= typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
Person
.
Schema<{ readonly name: string; readonly age: number; }, { readonly name: string; readonly age: string; }, never>.Encoded: {
readonly name: string;
readonly age: string;
}
Encoded

The resulting type is:

type PersonEncoded = {
readonly name: string
readonly age: string
}

Note that age is of type string in the Encoded type of the schema and is of type number in the Type type of the schema.

Alternatively, you can define the PersonEncoded type using the interface keyword, which can enhance readability and performance.

Example (Extracting Encoded Type with an Interface)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
// a schema that decodes a string to a number
age: typeof Schema.NumberFromString
age
:
import Schema
Schema
.
class NumberFromString

This schema transforms a string into a number by parsing the string using the parse function of the effect/Number module.

It returns an error if the value can't be converted (for example when non-numeric characters are provided).

The following special string values are supported: "NaN", "Infinity", "-Infinity".

@since3.10.0

NumberFromString
})
interface
interface PersonEncoded
PersonEncoded
extends
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Encoded<S> = S extends Schema.Schema.Variance<infer _A, infer I, infer _R> ? I : never

@since3.10.0

Encoded
<typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
Person
> {}

Both approaches yield the same result, but using an interface provides benefits such as performance advantages and improved readability.

In a Schema<Type, Encoded, Context>, the Context type represents any external data or dependencies that the schema requires to perform encoding or decoding. You can extract the inferred Context type in two ways:

  1. Using the Schema.Context utility.
  2. Accessing the Context field on the schema.

Example (Extracting the Context Type)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// 1. Using the Schema.Context utility
type
type PersonContext = never
PersonContext
=
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Context<S> = S extends Schema.Schema.Variance<infer _A, infer _I, infer R> ? R : never

@since3.10.0

Context
<typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
>
// 2. Accessing the Context field directly
type
type PersonContext2 = never
PersonContext2
= typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
.
Schema<{ readonly name: string; readonly age: number; }, { readonly name: string; readonly age: number; }, never>.Context: never
Context

When defining a schema, you may want to create a schema with an opaque type. This is useful when you want to hide the internal structure of the schema and only expose the type of the schema.

Example (Creating an Opaque Schema)

To create a schema with an opaque type, you can use the following technique that re-declares the schema:

import {
import Schema
Schema
} from "effect"
// Define the schema structure
const
const _Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
_Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Declare the type interface to make it opaque
interface
interface Person
Person
extends
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Type<S> = S extends Schema.Schema.Variance<infer A, infer _I, infer _R> ? A : never

@since3.10.0

Type
<typeof
const _Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
_Person
> {}
// Re-declare the schema as opaque
const
const Person: Schema.Schema<Person, Person, never>
Person
:
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface Person
Person
> =
const _Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
_Person

Alternatively, you can use the Class APIs (see the Class APIs section for more details).

Note that the technique shown above becomes more complex when the schema is defined such that Type is different from Encoded.

Example (Opaque Schema with Different Type and Encoded)

import {
import Schema
Schema
} from "effect"
// Define the schema structure, with a field that
// decodes a string to a number
const
const _Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
_Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.NumberFromString
age
:
import Schema
Schema
.
class NumberFromString

This schema transforms a string into a number by parsing the string using the parse function of the effect/Number module.

It returns an error if the value can't be converted (for example when non-numeric characters are provided).

The following special string values are supported: "NaN", "Infinity", "-Infinity".

@since3.10.0

NumberFromString
})
// Create the `Type` interface for an opaque schema
interface
interface Person
Person
extends
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Type<S> = S extends Schema.Schema.Variance<infer A, infer _I, infer _R> ? A : never

@since3.10.0

Type
<typeof
const _Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
_Person
> {}
// Create the `Encoded` interface for an opaque schema
interface
interface PersonEncoded
PersonEncoded
extends
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.Encoded<S> = S extends Schema.Schema.Variance<infer _A, infer I, infer _R> ? I : never

@since3.10.0

Encoded
<typeof
const _Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
_Person
> {}
// Re-declare the schema with opaque Type and Encoded
const
const Person: Schema.Schema<Person, PersonEncoded, never>
Person
:
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface Person
Person
,
interface PersonEncoded
PersonEncoded
> =
const _Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.NumberFromString;
}>
_Person

In this case, the field "age" is of type string in the Encoded type of the schema and is of type number in the Type type of the schema. Therefore, we need to define two interfaces (PersonEncoded and Person) and use both to redeclare our final schema Person.

It’s important to note that by default, most constructors exported by effect/Schema return readonly types.

Example (Readonly Types in a Schema)

For instance, in the Person schema below:

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})

the resulting inferred Type would be:

{
readonly name: string;
readonly age: number;
}

When working with unknown data types in TypeScript, decoding them into a known structure can be challenging. Luckily, effect/Schema provides several functions to help with this process. Let’s explore how to decode unknown values using these functions.

APIDescription
decodeUnknownSyncSynchronously decodes a value and throws an error if parsing fails.
decodeUnknownOptionDecodes a value and returns an Option type.
decodeUnknownEitherDecodes a value and returns an Either type.
decodeUnknownPromiseDecodes a value and returns a Promise.
decodeUnknownDecodes a value and returns an Effect.

The Schema.decodeUnknownSync function is useful when you want to parse a value and immediately throw an error if the parsing fails.

Example (Using decodeUnknownSync for Immediate Decoding)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Simulate an unknown input
const
const input: unknown
input
: unknown = {
name: string
name
: "Alice",
age: number
age
: 30 }
// Example of valid input matching the schema
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)(
const input: unknown
input
))
// Output: { name: 'Alice', age: 30 }
// Example of invalid input that does not match the schema
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)(null))
/*
throws:
ParseError: Expected { readonly name: string; readonly age: number }, actual null
*/

The Schema.decodeUnknownEither function allows you to parse a value and receive the result as an Either, representing success (Right) or failure (Left). This approach lets you handle parsing errors more gracefully without throwing exceptions.

Example (Using Schema.decodeUnknownEither for Error Handling)

import {
import Schema
Schema
} from "effect"
import {
import Either

@since2.0.0

@since2.0.0

Either
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>
decode
=
import Schema
Schema
.
const decodeUnknownEither: <{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Either.Either<...>

@since3.10.0

decodeUnknownEither
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)
// Simulate an unknown input
const
const input: unknown
input
: unknown = {
name: string
name
: "Alice",
age: number
age
: 30 }
// Attempt decoding a valid input
const
const result1: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>
result1
=
const decode: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>
decode
(
const input: unknown
input
)
if (
import Either

@since2.0.0

@since2.0.0

Either
.
const isRight: <{
readonly name: string;
readonly age: number;
}, ParseError>(self: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>) => self is Either.Right<...>

Determine if a Either is a Right.

@paramself - The Either to check.

@example

import { Either } from "effect"
assert.deepStrictEqual(Either.isRight(Either.right(1)), true)
assert.deepStrictEqual(Either.isRight(Either.left("a")), false)

@since2.0.0

isRight
(
const result1: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>
result1
)) {
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 result1: Either.Right<ParseError, {
readonly name: string;
readonly age: number;
}>
result1
.
Right<ParseError, { readonly name: string; readonly age: number; }>.right: {
readonly name: string;
readonly age: number;
}
right
)
/*
Output:
{ name: "Alice", age: 30 }
*/
}
// Simulate decoding an invalid input
const
const result2: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>
result2
=
const decode: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>
decode
(null)
if (
import Either

@since2.0.0

@since2.0.0

Either
.
const isLeft: <{
readonly name: string;
readonly age: number;
}, ParseError>(self: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>) => self is Either.Left<...>

Determine if a Either is a Left.

@paramself - The Either to check.

@example

import { Either } from "effect"
assert.deepStrictEqual(Either.isLeft(Either.right(1)), false)
assert.deepStrictEqual(Either.isLeft(Either.left("a")), true)

@since2.0.0

isLeft
(
const result2: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseError>
result2
)) {
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 result2: Either.Left<ParseError, {
readonly name: string;
readonly age: number;
}>
result2
.
Left<ParseError, { readonly name: string; readonly age: number; }>.left: ParseError
left
)
/*
Output:
{
_id: 'ParseError',
message: 'Expected { readonly name: string; readonly age: number }, actual null'
}
*/
}

If your schema involves asynchronous transformations, the Schema.decodeUnknownSync and Schema.decodeUnknownEither functions will not be suitable. In such cases, you should use the Schema.decodeUnknown function, which returns an Effect.

Example (Handling Asynchronous Decoding)

import {
import Schema
Schema
} from "effect"
import {
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
} from "effect"
const
const PersonId: typeof Schema.Number
PersonId
=
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
const
const Person: Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
id: typeof Schema.Number
id
:
const PersonId: typeof Schema.Number
PersonId
,
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
const
const asyncSchema: Schema.transformOrFail<typeof Schema.Number, Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>, never>
asyncSchema
=
import Schema
Schema
.
const transformOrFail: <Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>, typeof Schema.Number, never, never>(from: typeof Schema.Number, to: Schema.Struct<...>, options: {
...;
} | {
...;
}) => Schema.transformOrFail<...> (+1 overload)

Create a new Schema by transforming the input and output of an existing Schema using the provided decoding functions.

@since3.10.0

transformOrFail
(
const PersonId: typeof Schema.Number
PersonId
,
const Person: Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
, {
strict?: true
strict
: true,
// Decode with simulated async transformation
decode: (fromA: number, options: ParseOptions, ast: Transformation, fromI: number) => Effect.Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseIssue, never>
decode
: (
id: number
id
) =>
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const succeed: <{
id: number;
name: string;
age: number;
}>(value: {
id: number;
name: string;
age: number;
}) => Effect.Effect<{
id: number;
name: string;
age: number;
}, never, never>

Creates an Effect that always succeeds with a given value.

When to Use

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

@seefail to create an effect that represents a failure.

@example

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

@since2.0.0

succeed
({
id: number
id
,
name: string
name
: "name",
age: number
age
: 18 }).
Pipeable.pipe<Effect.Effect<{
id: number;
name: string;
age: number;
}, never, never>, Effect.Effect<{
id: number;
name: string;
age: number;
}, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<{
id: number;
name: string;
age: number;
}, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const delay: (duration: DurationInput) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R> (+1 overload)

Returns an effect that is delayed from this effect by the specified Duration.

@since2.0.0

delay
("10 millis")
),
encode: (toI: {
readonly id: number;
readonly name: string;
readonly age: number;
}, options: ParseOptions, ast: Transformation, toA: {
...;
}) => Effect.Effect<...>
encode
: (
person: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person
) =>
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

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

Creates an Effect that always succeeds with a given value.

When to Use

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

@seefail to create an effect that represents a failure.

@example

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

@since2.0.0

succeed
(
person: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person
.
id: number
id
).
Pipeable.pipe<Effect.Effect<number, never, never>, Effect.Effect<number, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<number, never, never>) => Effect.Effect<number, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const delay: (duration: DurationInput) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R> (+1 overload)

Returns an effect that is delayed from this effect by the specified Duration.

@since2.0.0

delay
("10 millis"))
})
// Attempting to use a synchronous decoder on an async schema
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
const decodeUnknownEither: <{
readonly id: number;
readonly name: string;
readonly age: number;
}, number>(schema: Schema.Schema<{
readonly id: number;
readonly name: string;
readonly age: number;
}, number, never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Either<...>

@since3.10.0

decodeUnknownEither
(
const asyncSchema: Schema.transformOrFail<typeof Schema.Number, Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>, never>
asyncSchema
)(1))
/*
Output:
{
_id: 'Either',
_tag: 'Left',
left: {
_id: 'ParseError',
message: '(number <-> { readonly id: number; readonly name: string; readonly age: number })\n' +
'└─ cannot be be resolved synchronously, this is caused by using runSync on an effect that performs async work'
}
}
*/
// Decoding asynchronously with `Schema.decodeUnknown`
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const runPromise: <{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError>(effect: Effect.Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<...>

Executes an effect and returns the result as a Promise.

When to Use

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

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

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

@example

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

@example

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

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

@since2.0.0

runPromise
(
import Schema
Schema
.
const decodeUnknown: <{
readonly id: number;
readonly name: string;
readonly age: number;
}, number, never>(schema: Schema.Schema<{
readonly id: number;
readonly name: string;
readonly age: number;
}, number, never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Effect.Effect<...>

@since3.10.0

decodeUnknown
(
const asyncSchema: Schema.transformOrFail<typeof Schema.Number, Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>, never>
asyncSchema
)(1)).
Promise<{ readonly id: number; readonly name: string; readonly age: number; }>.then<void, never>(onfulfilled?: ((value: {
readonly id: number;
readonly name: string;
readonly age: number;
}) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<...>) | null | undefined): Promise<...>

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

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

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

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

then
(
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
)
/*
Output:
{ id: 1, name: 'name', age: 18 }
*/

In the code above, the first approach using Schema.decodeUnknownEither results in an error indicating that the transformation cannot be resolved synchronously. This occurs because Schema.decodeUnknownEither is not designed for async operations. The second approach, which uses Schema.decodeUnknown, works correctly, allowing you to handle asynchronous transformations and return the expected result.

The Schema module provides several encode* functions to encode data according to a schema:

APIDescription
encodeSyncSynchronously encodes data and throws an error if encoding fails.
encodeOptionEncodes data and returns an Option type.
encodeEitherEncodes data and returns an Either type representing success or failure.
encodePromiseEncodes data and returns a Promise.
encodeEncodes data and returns an Effect.

Example (Using Schema.encodeSync for Immediate Encoding)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: typeof Schema.NumberFromString;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.NonEmptyString;
age: typeof Schema.NumberFromString;
}>(fields: {
name: typeof Schema.NonEmptyString;
age: typeof Schema.NumberFromString;
}): Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: typeof Schema.NumberFromString;
}> (+1 overload)

@since3.10.0

Struct
({
// Ensure name is a non-empty string
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
,
// Allow age to be decoded from a string and encoded to a string
age: typeof Schema.NumberFromString
age
:
import Schema
Schema
.
class NumberFromString

This schema transforms a string into a number by parsing the string using the parse function of the effect/Number module.

It returns an error if the value can't be converted (for example when non-numeric characters are provided).

The following special string values are supported: "NaN", "Infinity", "-Infinity".

@since3.10.0

NumberFromString
})
// Valid input: encoding succeeds and returns expected types
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
encodeSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: string;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: string;
}, never>, options?: ParseOptions): (a: {
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: string;
}
export encodeSync

@since3.10.0

encodeSync
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: typeof Schema.NumberFromString;
}>
Person
)({
name: string
name
: "Alice",
age: number
age
: 30 }))
// Output: { name: 'Alice', age: '30' }
// Invalid input: encoding fails due to empty name string
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
encodeSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: string;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: string;
}, never>, options?: ParseOptions): (a: {
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: string;
}
export encodeSync

@since3.10.0

encodeSync
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: typeof Schema.NumberFromString;
}>
Person
)({
name: string
name
: "",
age: number
age
: 30 }))
/*
throws:
ParseError: { readonly name: NonEmptyString; readonly age: NumberFromString }
└─ ["name"]
└─ NonEmptyString
└─ Predicate refinement failure
└─ Expected NonEmptyString, actual ""
*/

Note that during encoding, the number value 30 was converted to a string "30".

In certain cases, it may not be feasible to support encoding for a schema. While it is generally advised to define schemas that allow both decoding and encoding, there are situations where encoding a particular type is either unsupported or unnecessary. In these instances, the Forbidden issue can signal that encoding is not available for certain values.

Example (Using Forbidden to Indicate Unsupported Encoding)

Here is an example of a transformation that never fails during decoding. It returns an Either containing either the decoded value or the original input. For encoding, it is reasonable to not support it and use Forbidden as the result.

import {
import Either

@since2.0.0

@since2.0.0

Either
,
import ParseResult
ParseResult
,
import Schema
Schema
} from "effect"
// Define a schema that safely decodes to Either type
export const
const SafeDecode: <A, I>(self: Schema.Schema<A, I, never>) => Schema.transformOrFail<typeof Schema.Unknown, Schema.EitherFromSelf<Schema.SchemaClass<A, A, never>, typeof Schema.Unknown>, never>
SafeDecode
= <
function (type parameter) A in <A, I>(self: Schema.Schema<A, I, never>): Schema.transformOrFail<typeof Schema.Unknown, Schema.EitherFromSelf<Schema.SchemaClass<A, A, never>, typeof Schema.Unknown>, never>
A
,
function (type parameter) I in <A, I>(self: Schema.Schema<A, I, never>): Schema.transformOrFail<typeof Schema.Unknown, Schema.EitherFromSelf<Schema.SchemaClass<A, A, never>, typeof Schema.Unknown>, never>
I
>(
self: Schema.Schema<A, I, never>
self
:
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
function (type parameter) A in <A, I>(self: Schema.Schema<A, I, never>): Schema.transformOrFail<typeof Schema.Unknown, Schema.EitherFromSelf<Schema.SchemaClass<A, A, never>, typeof Schema.Unknown>, never>
A
,
function (type parameter) I in <A, I>(self: Schema.Schema<A, I, never>): Schema.transformOrFail<typeof Schema.Unknown, Schema.EitherFromSelf<Schema.SchemaClass<A, A, never>, typeof Schema.Unknown>, never>
I
, never>) => {
const
const decodeUnknownEither: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<A, ParseResult.ParseError>
decodeUnknownEither
=
import Schema
Schema
.
const decodeUnknownEither: <A, I>(schema: Schema.Schema<A, I, never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Either.Either<...>

@since3.10.0

decodeUnknownEither
(
self: Schema.Schema<A, I, never>
self
)
return
import Schema
Schema
.
const transformOrFail: <Schema.EitherFromSelf<Schema.SchemaClass<A, A, never>, typeof Schema.Unknown>, typeof Schema.Unknown, never, never>(from: typeof Schema.Unknown, to: Schema.EitherFromSelf<...>, options: {
...;
} | {
...;
}) => Schema.transformOrFail<...> (+1 overload)

Create a new Schema by transforming the input and output of an existing Schema using the provided decoding functions.

@since3.10.0

transformOrFail
(
import Schema
Schema
.
class Unknown

@since3.10.0

Unknown
,
import Schema
Schema
.
const EitherFromSelf: <Schema.SchemaClass<A, A, never>, typeof Schema.Unknown>({ left, right }: {
readonly left: typeof Schema.Unknown;
readonly right: Schema.SchemaClass<A, A, never>;
}) => Schema.EitherFromSelf<...>

@since3.10.0

EitherFromSelf
({
left: typeof Schema.Unknown
left
:
import Schema
Schema
.
class Unknown

@since3.10.0

Unknown
,
right: Schema.SchemaClass<A, A, never>
right
:
import Schema
Schema
.
const typeSchema: <A, I, never>(schema: Schema.Schema<A, I, never>) => Schema.SchemaClass<A, A, never>

The typeSchema function allows you to extract the Type portion of a schema, creating a new schema that conforms to the properties defined in the original schema without considering the initial encoding or transformation processes.

@since3.10.0

typeSchema
(
self: Schema.Schema<A, I, never>
self
)
}),
{
strict?: true
strict
: true,
// Decode: map a failed result to the input as Left,
// successful result as Right
decode: (fromA: unknown, options: ParseOptions, ast: Transformation, fromI: unknown) => Effect<Either.Either<A, unknown>, ParseResult.ParseIssue, never>
decode
: (
input: unknown
input
) =>
import ParseResult
ParseResult
.
const succeed: <Either.Either<A, unknown>>(a: Either.Either<A, unknown>) => Either.Either<Either.Either<A, unknown>, ParseResult.ParseIssue>

@since3.10.0

succeed
(
import Either

@since2.0.0

@since2.0.0

Either
.
const mapLeft: <A, ParseResult.ParseError, unknown>(self: Either.Either<A, ParseResult.ParseError>, f: (left: ParseResult.ParseError) => unknown) => Either.Either<...> (+1 overload)

Maps the Left side of an Either value to a new Either value.

@paramself - The input Either value to map.

@paramf - A transformation function to apply to the Left value of the input Either.

@since2.0.0

mapLeft
(
const decodeUnknownEither: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<A, ParseResult.ParseError>
decodeUnknownEither
(
input: unknown
input
), () =>
input: unknown
input
)
),
// Encode: only support encoding Right values,
// Left values raise Forbidden error
encode: (toI: Either.Either<A, unknown>, options: ParseOptions, ast: Transformation, toA: Either.Either<A, unknown>) => Effect<...>
encode
: (
actual: Either.Either<A, unknown>
actual
,
_: ParseOptions
_
,
ast: Transformation
ast
) =>
import Either

@since2.0.0

@since2.0.0

Either
.
const match: <A, unknown, Either.Either<never, ParseResult.ParseIssue>, Either.Either<A, ParseResult.ParseIssue>>(self: Either.Either<A, unknown>, options: {
...;
}) => Either.Either<...> | Either.Either<...> (+1 overload)

Takes two functions and an Either value, if the value is a Left the inner value is applied to the onLeft function, if the value is a Rightthe inner value is applied to theonRight` function.

@example

import { pipe, Either } from "effect"
const onLeft = (strings: ReadonlyArray<string>): string => `strings: ${strings.join(', ')}`
const onRight = (value: number): string => `Ok: ${value}`
assert.deepStrictEqual(pipe(Either.right(1), Either.match({ onLeft, onRight })), 'Ok: 1')
assert.deepStrictEqual(
pipe(Either.left(['string 1', 'string 2']), Either.match({ onLeft, onRight })),
'strings: string 1, string 2'
)

@since2.0.0

match
(
actual: Either.Either<A, unknown>
actual
, {
onLeft: (left: unknown) => Either.Either<never, ParseResult.ParseIssue>
onLeft
: () =>
import ParseResult
ParseResult
.
const fail: (issue: ParseResult.ParseIssue) => Either.Either<never, ParseResult.ParseIssue>

@since3.10.0

fail
(
new
import ParseResult
ParseResult
.
constructor Forbidden(ast: AST, actual: unknown, message?: string | undefined): ParseResult.Forbidden

The Forbidden variant of the ParseIssue type represents a forbidden operation, such as when encountering an Effect that is not allowed to execute (e.g., using runSync).

@since3.10.0

Forbidden
(
ast: Transformation
ast
,
actual: Either.Either<A, unknown>
actual
,
"cannot encode a Left"
)
),
// Successfully encode a Right value
onRight: (right: A) => Either.Either<A, ParseResult.ParseIssue>
onRight
:
import ParseResult
ParseResult
.
const succeed: <A>(a: A) => Either.Either<A, ParseResult.ParseIssue>

@since3.10.0

succeed
})
}
)
}

Explanation

  • Decoding: The SafeDecode function ensures that decoding never fails. It wraps the decoded value in an Either, where a successful decoding results in a Right and a failed decoding results in a Left containing the original input.
  • Encoding: The encoding process uses the Forbidden error to indicate that encoding a Left value is not supported. Only Right values are successfully encoded.

The Schema.decodeUnknownEither and Schema.encodeEither functions returns a Either:

Either<Type, ParseError>

where ParseError is defined as follows (simplified):

interface ParseError {
readonly _tag: "ParseError"
readonly issue: ParseIssue
}

In this structure, ParseIssue represents an error that might occur during the parsing process. It is wrapped in a tagged error to make it easier to catch errors using Effect.catchTag. The result Either<Type, ParseError> contains the inferred data type described by the schema (Type). A successful parse yields a Right value with the parsed data Type, while a failed parse results in a Left value containing a ParseError.

The options below provide control over both decoding and encoding behaviors.

By default, any properties not defined in the schema are removed from the output when parsing a value. This ensures the parsed data conforms strictly to the expected structure.

If you want to detect and handle unexpected properties, use the onExcessProperty option (default value: "ignore"), which allows you to raise an error for excess properties. This can be helpful when you need to validate and catch unanticipated properties.

Example (Setting onExcessProperty to "error")

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Excess properties are ignored by default
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)({
name: string
name
: "Bob",
age: number
age
: 40,
email: string
email
: "bob@example.com" // Ignored
})
)
/*
Output:
{ name: 'Bob', age: 40 }
*/
// With `onExcessProperty` set to "error",
// an error is thrown for excess properties
import Schema
Schema
.
decodeUnknownSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)(
{
name: string
name
: "Bob",
age: number
age
: 40,
email: string
email
: "bob@example.com" // Will raise an error
},
{
ParseOptions.onExcessProperty?: "ignore" | "error" | "preserve" | undefined

When using a Schema to parse a value, by default any properties that are not specified in the Schema will be stripped out from the output. This is because the Schema is expecting a specific shape for the parsed value, and any excess properties do not conform to that shape.

However, you can use the onExcessProperty option (default value: "ignore") to trigger a parsing error. This can be particularly useful in cases where you need to detect and handle potential errors or unexpected values.

If you want to allow excess properties to remain, you can use onExcessProperty set to "preserve".

default: "ignore"

@since3.10.0

onExcessProperty
: "error" }
)
/*
throws
ParseError: { readonly name: string; readonly age: number }
└─ ["email"]
└─ is unexpected, expected: "name" | "age"
*/

To retain extra properties, set onExcessProperty to "preserve".

Example (Setting onExcessProperty to "preserve")

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Excess properties are preserved in the output
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)(
{
name: string
name
: "Bob",
age: number
age
: 40,
email: string
email
: "bob@example.com"
},
{
ParseOptions.onExcessProperty?: "ignore" | "error" | "preserve" | undefined

When using a Schema to parse a value, by default any properties that are not specified in the Schema will be stripped out from the output. This is because the Schema is expecting a specific shape for the parsed value, and any excess properties do not conform to that shape.

However, you can use the onExcessProperty option (default value: "ignore") to trigger a parsing error. This can be particularly useful in cases where you need to detect and handle potential errors or unexpected values.

If you want to allow excess properties to remain, you can use onExcessProperty set to "preserve".

default: "ignore"

@since3.10.0

onExcessProperty
: "preserve" }
)
)
/*
{ email: 'bob@example.com', name: 'Bob', age: 40 }
*/

The errors option enables you to retrieve all errors encountered during parsing. By default, only the first error is returned. Setting errors to "all" provides comprehensive error feedback, which can be useful for debugging or offering detailed validation feedback.

Example (Setting errors to "all")

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Attempt to parse with multiple issues in the input data
import Schema
Schema
.
decodeUnknownSync<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly name: string;
readonly age: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)(
{
name: string
name
: "Bob",
age: string
age
: "abc",
email: string
email
: "bob@example.com"
},
{
ParseOptions.errors?: "first" | "all" | undefined

The errors option allows you to receive all parsing errors when attempting to parse a value using a schema. By default only the first error is returned, but by setting the errors option to "all", you can receive all errors that occurred during the parsing process. This can be useful for debugging or for providing more comprehensive error messages to the user.

default: "first"

@since3.10.0

errors
: "all",
ParseOptions.onExcessProperty?: "ignore" | "error" | "preserve" | undefined

When using a Schema to parse a value, by default any properties that are not specified in the Schema will be stripped out from the output. This is because the Schema is expecting a specific shape for the parsed value, and any excess properties do not conform to that shape.

However, you can use the onExcessProperty option (default value: "ignore") to trigger a parsing error. This can be particularly useful in cases where you need to detect and handle potential errors or unexpected values.

If you want to allow excess properties to remain, you can use onExcessProperty set to "preserve".

default: "ignore"

@since3.10.0

onExcessProperty
: "error" }
)
/*
throws
ParseError: { readonly name: string; readonly age: number }
├─ ["email"]
│ └─ is unexpected, expected: "name" | "age"
└─ ["age"]
└─ Expected number, actual "abc"
*/

The propertyOrder option provides control over the order of object fields in the output. This feature is particularly useful when the sequence of keys is important for the consuming processes or when maintaining the input order enhances readability and usability.

By default, the propertyOrder option is set to "none". This means that the internal system decides the order of keys to optimize parsing speed. The order of keys in this mode should not be considered stable, and it’s recommended not to rely on key ordering as it may change in future updates.

Setting propertyOrder to "original" ensures that the keys are ordered as they appear in the input during the decoding/encoding process.

Example (Synchronous Decoding)

import {
import Schema
Schema
} from "effect"
const
const schema: Schema.Struct<{
a: typeof Schema.Number;
b: Schema.Literal<["b"]>;
c: typeof Schema.Number;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: typeof Schema.Number;
b: Schema.Literal<["b"]>;
c: typeof Schema.Number;
}>(fields: {
a: typeof Schema.Number;
b: Schema.Literal<["b"]>;
c: typeof Schema.Number;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
a: typeof Schema.Number
a
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
,
b: Schema.Literal<["b"]>
b
:
import Schema
Schema
.
function Literal<["b"]>(literals_0: "b"): Schema.Literal<["b"]> (+2 overloads)

@since3.10.0

Literal
("b"),
c: typeof Schema.Number
c
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Default decoding, where property order is system-defined
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly a: number;
readonly b: "b";
readonly c: number;
}, {
readonly a: number;
readonly b: "b";
readonly c: number;
}>(schema: Schema.Schema<{
readonly a: number;
readonly b: "b";
readonly c: number;
}, {
readonly a: number;
readonly b: "b";
readonly c: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: number;
readonly b: "b";
readonly c: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
a: typeof Schema.Number;
b: Schema.Literal<["b"]>;
c: typeof Schema.Number;
}>
schema
)({
b: string
b
: "b",
c: number
c
: 2,
a: number
a
: 1 }))
// Output may vary: { a: 1, b: 'b', c: 2 }
// Decoding while preserving input order
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly a: number;
readonly b: "b";
readonly c: number;
}, {
readonly a: number;
readonly b: "b";
readonly c: number;
}>(schema: Schema.Schema<{
readonly a: number;
readonly b: "b";
readonly c: number;
}, {
readonly a: number;
readonly b: "b";
readonly c: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: number;
readonly b: "b";
readonly c: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
a: typeof Schema.Number;
b: Schema.Literal<["b"]>;
c: typeof Schema.Number;
}>
schema
)(
{
b: string
b
: "b",
c: number
c
: 2,
a: number
a
: 1 },
{
ParseOptions.propertyOrder?: "none" | "original" | undefined

The propertyOrder option provides control over the order of object fields in the output. This feature is particularly useful when the sequence of keys is important for the consuming processes or when maintaining the input order enhances readability and usability.

By default, the propertyOrder option is set to "none". This means that the internal system decides the order of keys to optimize parsing speed. The order of keys in this mode should not be considered stable, and it's recommended not to rely on key ordering as it may change in future updates without notice.

Setting propertyOrder to "original" ensures that the keys are ordered as they appear in the input during the decoding/encoding process.

default: "none"

@since3.10.0

propertyOrder
: "original" }
)
)
// Output preserves input order: { b: 'b', c: 2, a: 1 }

Example (Asynchronous Decoding)

import type {
import Duration
Duration
} from "effect"
import {
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
,
import ParseResult
ParseResult
,
import Schema
Schema
} from "effect"
// Helper function to simulate an async operation in schema
const
const effectify: (duration: Duration.DurationInput) => Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>
effectify
= (
duration: Duration.DurationInput
duration
:
import Duration
Duration
.
type DurationInput = number | bigint | Duration.Duration | [seconds: number, nanos: number] | `${number} nano` | `${number} nanos` | `${number} micro` | `${number} micros` | `${number} milli` | `${number} millis` | `${number} second` | `${number} seconds` | `${number} minute` | `${number} minutes` | ... 5 more ... | `${number} weeks`

@since2.0.0

DurationInput
) =>
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>): Schema.transformOrFail<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const transformOrFail: <typeof Schema.Number, typeof Schema.Number, never, never>(to: typeof Schema.Number, options: {
readonly decode: (fromA: number, options: ParseOptions, ast: Transformation, fromI: number) => Effect.Effect<...>;
readonly encode: (toI: number, options: ParseOptions, ast: Transformation, toA: number) => Effect.Effect<...>;
readonly strict?: true;
} | {
...;
}) => (from: typeof Schema.Number) => Schema.transformOrFail<...> (+1 overload)

Create a new Schema by transforming the input and output of an existing Schema using the provided decoding functions.

@since3.10.0

transformOrFail
(
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
, {
strict?: true
strict
: true,
decode: (fromA: number, options: ParseOptions, ast: Transformation, fromI: number) => Effect.Effect<number, ParseResult.ParseIssue, never>
decode
: (
x: number
x
) =>
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const sleep: (duration: Duration.DurationInput) => Effect.Effect<void>

Returns an effect that suspends for the specified duration. This method is asynchronous, and does not actually block the fiber executing the effect.

@since2.0.0

sleep
(
duration: Duration.DurationInput
duration
).
Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<number, ParseResult.ParseIssue, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const andThen: <Either<number, ParseResult.ParseIssue>>(f: Either<number, ParseResult.ParseIssue>) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<...> (+3 overloads)

Chains two actions, where the second action can depend on the result of the first.

Syntax

const transformedEffect = pipe(myEffect, Effect.andThen(anotherEffect))
// or
const transformedEffect = Effect.andThen(myEffect, anotherEffect)
// or
const transformedEffect = myEffect.pipe(Effect.andThen(anotherEffect))

When to Use

Use andThen when you need to run multiple actions in sequence, with the second action depending on the result of the first. This is useful for combining effects or handling computations that must happen in order.

Details

The second action can be:

  • A constant value (similar to

as

)

  • A function returning a value (similar to

map

)

  • A Promise
  • A function returning a Promise
  • An Effect
  • A function returning an Effect (similar to

flatMap

)

Note: andThen works well with both Option and Either types, treating them as effects.

@example

// Title: Applying a Discount Based on Fetched Amount
import { pipe, Effect } from "effect"
// Function to apply a discount safely to a transaction amount
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, Error> =>
discountRate === 0
? Effect.fail(new Error("Discount rate cannot be zero"))
: Effect.succeed(total - (total * discountRate) / 100)
// Simulated asynchronous task to fetch a transaction amount from database
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
// Using Effect.map and Effect.flatMap
const result1 = pipe(
fetchTransactionAmount,
Effect.map((amount) => amount * 2),
Effect.flatMap((amount) => applyDiscount(amount, 5))
)
Effect.runPromise(result1).then(console.log)
// Output: 190
// Using Effect.andThen
const result2 = pipe(
fetchTransactionAmount,
Effect.andThen((amount) => amount * 2),
Effect.andThen((amount) => applyDiscount(amount, 5))
)
Effect.runPromise(result2).then(console.log)
// Output: 190

@since2.0.0

andThen
(
import ParseResult
ParseResult
.
const succeed: <number>(a: number) => Either<number, ParseResult.ParseIssue>

@since3.10.0

succeed
(
x: number
x
))
),
encode: (toI: number, options: ParseOptions, ast: Transformation, toA: number) => Effect.Effect<number, ParseResult.ParseIssue, never>
encode
:
import ParseResult
ParseResult
.
const succeed: <A>(a: A) => Either<A, ParseResult.ParseIssue>

@since3.10.0

succeed
})
)
// Define a structure with asynchronous behavior in each field
const
const schema: Schema.Struct<{
a: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
b: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
c: Schema.transformOrFail<...>;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
b: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
c: Schema.transformOrFail<...>;
}>(fields: {
a: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
b: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
c: Schema.transformOrFail<...>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
a: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>
a
:
const effectify: (duration: Duration.DurationInput) => Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>
effectify
("200 millis"),
b: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>
b
:
const effectify: (duration: Duration.DurationInput) => Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>
effectify
("300 millis"),
c: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>
c
:
const effectify: (duration: Duration.DurationInput) => Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>
effectify
("100 millis")
}).
Struct<{ a: transformOrFail<typeof Number$, typeof Number$, never>; b: transformOrFail<typeof Number$, typeof Number$, never>; c: transformOrFail<typeof Number$, typeof Number$, never>; }>.annotations(annotations: Schema.Annotations.Schema<{
readonly a: number;
readonly b: number;
readonly c: number;
}, readonly []>): Schema.Struct<{
a: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
b: Schema.transformOrFail<...>;
c: Schema.transformOrFail<...>;
}>

Merges a set of new annotations with existing ones, potentially overwriting any duplicates.

annotations
({
Annotations.Schema<{ readonly a: number; readonly b: number; readonly c: number; }, readonly []>.concurrency?: ConcurrencyAnnotation
concurrency
: 3 })
// Default decoding, where property order is system-defined
import Schema
Schema
.
const decode: <{
readonly a: number;
readonly b: number;
readonly c: number;
}, {
readonly a: number;
readonly b: number;
readonly c: number;
}, never>(schema: Schema.Schema<{
readonly a: number;
readonly b: number;
readonly c: number;
}, {
readonly a: number;
readonly b: number;
readonly c: number;
}, never>, options?: ParseOptions) => (i: {
readonly a: number;
readonly b: number;
readonly c: number;
}, overrideOptions?: ParseOptions) => Effect.Effect<...>

@since3.10.0

decode
(
const schema: Schema.Struct<{
a: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
b: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
c: Schema.transformOrFail<...>;
}>
schema
)({
a: number
a
: 1,
b: number
b
: 2,
c: number
c
: 3 })
.
Pipeable.pipe<Effect.Effect<{
readonly a: number;
readonly b: number;
readonly c: number;
}, ParseResult.ParseError, never>, Promise<{
readonly a: number;
readonly b: number;
readonly c: number;
}>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<...>) => Promise<...>): Promise<...> (+21 overloads)
pipe
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const runPromise: <A, E>(effect: Effect.Effect<A, E, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<A>

Executes an effect and returns the result as a Promise.

When to Use

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

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

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

@example

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

@example

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

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

@since2.0.0

runPromise
)
.
Promise<{ readonly a: number; readonly b: number; readonly c: number; }>.then<void, never>(onfulfilled?: ((value: {
readonly a: number;
readonly b: number;
readonly c: number;
}) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<...>) | null | undefined): Promise<...>

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

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

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

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

then
(
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
)
// Output decided internally: { c: 3, a: 1, b: 2 }
// Decoding while preserving input order
import Schema
Schema
.
const decode: <{
readonly a: number;
readonly b: number;
readonly c: number;
}, {
readonly a: number;
readonly b: number;
readonly c: number;
}, never>(schema: Schema.Schema<{
readonly a: number;
readonly b: number;
readonly c: number;
}, {
readonly a: number;
readonly b: number;
readonly c: number;
}, never>, options?: ParseOptions) => (i: {
readonly a: number;
readonly b: number;
readonly c: number;
}, overrideOptions?: ParseOptions) => Effect.Effect<...>

@since3.10.0

decode
(
const schema: Schema.Struct<{
a: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
b: Schema.transformOrFail<typeof Schema.Number, typeof Schema.Number, never>;
c: Schema.transformOrFail<...>;
}>
schema
)({
a: number
a
: 1,
b: number
b
: 2,
c: number
c
: 3 }, {
ParseOptions.propertyOrder?: "none" | "original" | undefined

The propertyOrder option provides control over the order of object fields in the output. This feature is particularly useful when the sequence of keys is important for the consuming processes or when maintaining the input order enhances readability and usability.

By default, the propertyOrder option is set to "none". This means that the internal system decides the order of keys to optimize parsing speed. The order of keys in this mode should not be considered stable, and it's recommended not to rely on key ordering as it may change in future updates without notice.

Setting propertyOrder to "original" ensures that the keys are ordered as they appear in the input during the decoding/encoding process.

default: "none"

@since3.10.0

propertyOrder
: "original" })
.
Pipeable.pipe<Effect.Effect<{
readonly a: number;
readonly b: number;
readonly c: number;
}, ParseResult.ParseError, never>, Promise<{
readonly a: number;
readonly b: number;
readonly c: number;
}>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<...>) => Promise<...>): Promise<...> (+21 overloads)
pipe
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const runPromise: <A, E>(effect: Effect.Effect<A, E, never>, options?: {
readonly signal?: AbortSignal;
} | undefined) => Promise<A>

Executes an effect and returns the result as a Promise.

When to Use

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

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

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

@example

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

@example

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

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

@since2.0.0

runPromise
)
.
Promise<{ readonly a: number; readonly b: number; readonly c: number; }>.then<void, never>(onfulfilled?: ((value: {
readonly a: number;
readonly b: number;
readonly c: number;
}) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<...>) | null | undefined): Promise<...>

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

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

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

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

then
(
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
)
// Output preserving input order: { a: 1, b: 2, c: 3 }

The parseOptions annotation allows you to customize parsing behavior at different schema levels, enabling you to apply unique parsing settings to nested schemas within a structure. Options defined within a schema override parent-level settings and apply to all nested schemas.

Example (Using parseOptions to Customize Error Handling)

import {
import Schema
Schema
} from "effect"
import {
import Either

@since2.0.0

@since2.0.0

Either
} from "effect"
const
const schema: Schema.Struct<{
a: Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>;
d: typeof Schema.String;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>;
d: typeof Schema.String;
}>(fields: {
a: Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>;
d: typeof Schema.String;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
a: Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>
a
:
import Schema
Schema
.
function Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>(fields: {
b: typeof Schema.String;
c: typeof Schema.String;
}): Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}> (+1 overload)

@since3.10.0

Struct
({
b: typeof Schema.String
b
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
c: typeof Schema.String
c
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
}).
Struct<{ b: typeof String$; c: typeof String$; }>.annotations(annotations: Schema.Annotations.Schema<{
readonly b: string;
readonly c: string;
}, readonly []>): Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>

Merges a set of new annotations with existing ones, potentially overwriting any duplicates.

annotations
({
Annotations.Doc<{ readonly b: string; readonly c: string; }>.title?: string
title
: "first error only",
// Limit errors to the first in this sub-schema
Annotations.Schema<{ readonly b: string; readonly c: string; }, readonly []>.parseOptions?: ParseOptions
parseOptions
: {
ParseOptions.errors?: "first" | "all" | undefined

The errors option allows you to receive all parsing errors when attempting to parse a value using a schema. By default only the first error is returned, but by setting the errors option to "all", you can receive all errors that occurred during the parsing process. This can be useful for debugging or for providing more comprehensive error messages to the user.

default: "first"

@since3.10.0

errors
: "first" }
}),
d: typeof Schema.String
d
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
}).
Struct<{ a: Struct<{ b: typeof String$; c: typeof String$; }>; d: typeof String$; }>.annotations(annotations: Schema.Annotations.Schema<{
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, readonly []>): Schema.Struct<{
a: Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>;
d: typeof Schema.String;
}>

Merges a set of new annotations with existing ones, potentially overwriting any duplicates.

annotations
({
Annotations.Doc<A>.title?: string
title
: "all errors",
// Capture all errors for the main schema
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.parseOptions?: ParseOptions
parseOptions
: {
ParseOptions.errors?: "first" | "all" | undefined

The errors option allows you to receive all parsing errors when attempting to parse a value using a schema. By default only the first error is returned, but by setting the errors option to "all", you can receive all errors that occurred during the parsing process. This can be useful for debugging or for providing more comprehensive error messages to the user.

default: "first"

@since3.10.0

errors
: "all" }
})
// Decode input with custom error-handling behavior
const
const result: Either.Either<{
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, ParseError>
result
=
import Schema
Schema
.
const decodeUnknownEither: <{
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, {
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}>(schema: Schema.Schema<{
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, {
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Either.Either<...>

@since3.10.0

decodeUnknownEither
(
const schema: Schema.Struct<{
a: Schema.Struct<{
b: typeof Schema.String;
c: typeof Schema.String;
}>;
d: typeof Schema.String;
}>
schema
)(
{
a: {}
a
: {} },
{
ParseOptions.errors?: "first" | "all" | undefined

The errors option allows you to receive all parsing errors when attempting to parse a value using a schema. By default only the first error is returned, but by setting the errors option to "all", you can receive all errors that occurred during the parsing process. This can be useful for debugging or for providing more comprehensive error messages to the user.

default: "first"

@since3.10.0

errors
: "first" }
)
if (
import Either

@since2.0.0

@since2.0.0

Either
.
const isLeft: <{
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, ParseError>(self: Either.Either<{
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, ParseError>) => self is Either.Left<...>

Determine if a Either is a Left.

@paramself - The Either to check.

@example

import { Either } from "effect"
assert.deepStrictEqual(Either.isLeft(Either.right(1)), false)
assert.deepStrictEqual(Either.isLeft(Either.left("a")), true)

@since2.0.0

isLeft
(
const result: Either.Either<{
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}, ParseError>
result
)) {
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: Either.Left<ParseError, {
readonly a: {
readonly b: string;
readonly c: string;
};
readonly d: string;
}>
result
.
Left<ParseError, { readonly a: { readonly b: string; readonly c: string; }; readonly d: string; }>.left: ParseError
left
.
ParseError.message: string
message
)
}
/*
all errors
├─ ["a"]
│ └─ first error only
│ └─ ["b"]
│ └─ is missing
└─ ["d"]
└─ is missing
*/

Detailed Output Explanation:

In this example:

  • The main schema is configured to display all errors. Hence, you will see errors related to both the d field (since it’s missing) and any errors from the a subschema.
  • The subschema (a) is set to display only the first error. Although both b and c fields are missing, only the first missing field (b) is reported.

The Schema.is function provides a way to verify if a value conforms to a given schema. It acts as a type guard, taking a value of type unknown and determining if it matches the structure and type constraints defined in the schema.

Here’s how the Schema.is function works:

  1. Schema Definition: Define a schema to describe the structure and constraints of the data type you expect. For instance, Schema<Type, Encoded, Context>, where Type is the target type you want to validate against.

  2. Type Guard Creation: Use the schema to create a user-defined type guard, (u: unknown) => u is Type. This function can be used at runtime to check if a value meets the requirements of the schema.

Example (Creating and Using a Type Guard)

import {
import Schema
Schema
} from "effect"
// Define a schema for a Person object
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Generate a type guard from the schema
const
const isPerson: (u: unknown, overrideOptions?: ParseOptions | number) => u is {
readonly name: string;
readonly age: number;
}
isPerson
=
import Schema
Schema
.
is<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions | number) => u is {
readonly name: string;
readonly age: number;
}
export is

By default the option exact is set to true.

@since3.10.0

is
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)
// Test the type guard with various inputs
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 isPerson: (u: unknown, overrideOptions?: ParseOptions | number) => u is {
readonly name: string;
readonly age: number;
}
isPerson
({
name: string
name
: "Alice",
age: number
age
: 30 }))
// Output: true
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 isPerson: (u: unknown, overrideOptions?: ParseOptions | number) => u is {
readonly name: string;
readonly age: number;
}
isPerson
(null))
// Output: false
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 isPerson: (u: unknown, overrideOptions?: ParseOptions | number) => u is {
readonly name: string;
readonly age: number;
}
isPerson
({}))
// Output: false

The generated isPerson function has the following signature:

const isPerson: (
u: unknown,
overrideOptions?: number | ParseOptions
) => u is {
readonly name: string
readonly age: number
}

While type guards verify whether a value conforms to a specific type, the Schema.asserts function goes further by asserting that an input matches the schema type Type (from Schema<Type, Encoded, Context>). If the input does not match the schema, it throws a detailed error, making it useful for runtime validation.

Example (Creating and Using an Assertion)

import {
import Schema
Schema
} from "effect"
// Define a schema for a Person object
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>(fields: {
name: typeof Schema.String;
age: typeof Schema.Number;
}): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
// Generate an assertion function from the schema
const
const assertsPerson: Schema.Schema.ToAsserts<Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>>
assertsPerson
:
import Schema
Schema
.
namespace Schema

@since3.10.0

@since3.10.0

Schema
.
type Schema<in out A, in out I = A, out R = never>.ToAsserts<S extends Schema.Schema.AnyNoContext> = (input: unknown, options?: ParseOptions) => asserts input is Schema.Schema.Type<S>

@since3.10.0

ToAsserts
<typeof
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
> =
import Schema
Schema
.
asserts<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => asserts u is {
readonly name: string;
readonly age: number;
}
export asserts

By default the option exact is set to true.

@throwsParseError

@since3.10.0

asserts
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)
try {
// Attempt to assert that the input matches the Person schema
const assertsPerson: (input: unknown, options?: ParseOptions) => asserts input is {
readonly name: string;
readonly age: number;
}
assertsPerson
({
name: string
name
: "Alice",
age: string
age
: "30" })
} catch (
var e: unknown
e
) {
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.error(message?: any, ...optionalParams: any[]): void

Prints to stderr 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 code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.

@sincev0.1.100

error
("The input does not match the schema:")
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.error(message?: any, ...optionalParams: any[]): void

Prints to stderr 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 code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.

@sincev0.1.100

error
(
var e: unknown
e
)
}
/*
throws:
The input does not match the schema:
{
_id: 'ParseError',
message: '{ readonly name: string; readonly age: number }\n' +
'└─ ["age"]\n' +
' └─ Expected number, actual "30"'
}
*/
// This input matches the schema and will not throw an error
const assertsPerson: (input: unknown, options?: ParseOptions) => asserts input is {
readonly name: string;
readonly age: number;
}
assertsPerson
({
name: string
name
: "Alice",
age: number
age
: 30 })

The assertsPerson function generated from the schema has the following signature:

const assertsPerson: (
input: unknown,
overrideOptions?: number | ParseOptions
) => asserts input is {
readonly name: string
readonly age: number
}

When decoding, it’s important to understand how missing properties are processed. By default, if a property is not present in the input, it is treated as if it were present with an undefined value.

Example (Default Behavior of Missing Properties)

import {
import Schema
Schema
} from "effect"
const
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: typeof Schema.Unknown;
}>(fields: {
a: typeof Schema.Unknown;
}): Schema.Struct<{
a: typeof Schema.Unknown;
}> (+1 overload)

@since3.10.0

Struct
({
a: typeof Schema.Unknown
a
:
import Schema
Schema
.
class Unknown

@since3.10.0

Unknown
})
const
const input: {}
input
= {}
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly a: unknown;
}, {
readonly a: unknown;
}>(schema: Schema.Schema<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: unknown;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
)(
const input: {}
input
))
// Output: { a: undefined }

In this example, although the key "a" is not present in the input, it is treated as { a: undefined } by default.

If you need your validation logic to differentiate between genuinely missing properties and those explicitly set to undefined, you can enable the exact option.

Example (Setting exact: true to Distinguish Missing Properties)

import {
import Schema
Schema
} from "effect"
const
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: typeof Schema.Unknown;
}>(fields: {
a: typeof Schema.Unknown;
}): Schema.Struct<{
a: typeof Schema.Unknown;
}> (+1 overload)

@since3.10.0

Struct
({
a: typeof Schema.Unknown
a
:
import Schema
Schema
.
class Unknown

@since3.10.0

Unknown
})
const
const input: {}
input
= {}
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
decodeUnknownSync<{
readonly a: unknown;
}, {
readonly a: unknown;
}>(schema: Schema.Schema<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: unknown;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
)(
const input: {}
input
, {
ParseOptions.exact?: boolean | undefined

Handles missing properties in data structures. By default, missing properties are treated as if present with an undefined value. To treat missing properties as errors, set the exact option to true. This setting is already enabled by default for is and asserts functions, treating absent properties strictly unless overridden.

default: false

@since3.10.0

exact
: true }))
/*
throws
ParseError: { readonly a: unknown }
└─ ["a"]
└─ is missing
*/

For the APIs Schema.is and Schema.asserts, however, the default behavior is to treat missing properties strictly, where the default for exact is true:

Example (Strict Handling of Missing Properties with Schema.is and Schema.asserts)

import type {
import SchemaAST
SchemaAST
} from "effect"
import {
import Schema
Schema
} from "effect"
const
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: typeof Schema.Unknown;
}>(fields: {
a: typeof Schema.Unknown;
}): Schema.Struct<{
a: typeof Schema.Unknown;
}> (+1 overload)

@since3.10.0

Struct
({
a: typeof Schema.Unknown
a
:
import Schema
Schema
.
class Unknown

@since3.10.0

Unknown
})
const
const input: {}
input
= {}
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
is<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>(schema: Schema.Schema<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>, options?: SchemaAST.ParseOptions): (u: unknown, overrideOptions?: SchemaAST.ParseOptions | number) => u is {
readonly a: unknown;
}
export is

By default the option exact is set to true.

@since3.10.0

is
(
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
)(
const input: {}
input
))
// Output: false
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
import Schema
Schema
.
is<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>(schema: Schema.Schema<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>, options?: SchemaAST.ParseOptions): (u: unknown, overrideOptions?: SchemaAST.ParseOptions | number) => u is {
readonly a: unknown;
}
export is

By default the option exact is set to true.

@since3.10.0

is
(
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
)(
const input: {}
input
, {
ParseOptions.exact?: boolean | undefined

Handles missing properties in data structures. By default, missing properties are treated as if present with an undefined value. To treat missing properties as errors, set the exact option to true. This setting is already enabled by default for is and asserts functions, treating absent properties strictly unless overridden.

default: false

@since3.10.0

exact
: false }))
// Output: true
const
const asserts: (u: unknown, overrideOptions?: SchemaAST.ParseOptions) => asserts u is {
readonly a: unknown;
}
asserts
: (
u: unknown
u
: unknown,
overrideOptions: SchemaAST.ParseOptions | undefined
overrideOptions
?:
import SchemaAST
SchemaAST
.
interface ParseOptions

@since3.10.0

ParseOptions
) => asserts
u: unknown
u
is {
readonly
a: unknown
a
: unknown
} =
import Schema
Schema
.
asserts<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>(schema: Schema.Schema<{
readonly a: unknown;
}, {
readonly a: unknown;
}, never>, options?: SchemaAST.ParseOptions): (u: unknown, overrideOptions?: SchemaAST.ParseOptions) => asserts u is {
readonly a: unknown;
}
export asserts

By default the option exact is set to true.

@throwsParseError

@since3.10.0

asserts
(
const schema: Schema.Struct<{
a: typeof Schema.Unknown;
}>
schema
)
try {
const asserts: (u: unknown, overrideOptions?: SchemaAST.ParseOptions) => asserts u is {
readonly a: unknown;
}
asserts
(
const input: {}
input
)
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
("asserts passed")
} catch (
var e: any
e
: any) {
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.error(message?: any, ...optionalParams: any[]): void

Prints to stderr 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 code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.

@sincev0.1.100

error
("asserts failed")
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.error(message?: any, ...optionalParams: any[]): void

Prints to stderr 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 code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.

@sincev0.1.100

error
(
var e: any
e
.
any
message
)
}
/*
Output:
asserts failed
{ readonly a: unknown }
└─ ["a"]
└─ is missing
*/
try {
const asserts: (u: unknown, overrideOptions?: SchemaAST.ParseOptions) => asserts u is {
readonly a: unknown;
}
asserts
(
const input: {}
input
, {
ParseOptions.exact?: boolean | undefined

Handles missing properties in data structures. By default, missing properties are treated as if present with an undefined value. To treat missing properties as errors, set the exact option to true. This setting is already enabled by default for is and asserts functions, treating absent properties strictly unless overridden.

default: false

@since3.10.0

exact
: false })
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
("asserts passed")
} catch (
var e: any
e
: any) {
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.error(message?: any, ...optionalParams: any[]): void

Prints to stderr 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 code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.

@sincev0.1.100

error
("asserts failed")
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.error(message?: any, ...optionalParams: any[]): void

Prints to stderr 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 code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.

@sincev0.1.100

error
(
var e: any
e
.
any
message
)
}
// Output: asserts passed

The naming conventions in effect/Schema are designed to be straightforward and logical, focusing primarily on compatibility with JSON serialization. This approach simplifies the understanding and use of schemas, especially for developers who are integrating web technologies where JSON is a standard data interchange format.

JSON-Compatible Types

Schemas that naturally serialize to JSON-compatible formats are named directly after their data types.

For instance:

  • Schema.Date: serializes JavaScript Date objects to ISO-formatted strings, a typical method for representing dates in JSON.
  • Schema.Number: used directly as it maps precisely to the JSON number type, requiring no special transformation to remain JSON-compatible.

Non-JSON-Compatible Types

When dealing with types that do not have a direct representation in JSON, the naming strategy incorporates additional details to indicate the necessary transformation. This helps in setting clear expectations about the schema’s behavior:

For instance:

  • Schema.DateFromSelf: indicates that the schema handles Date objects, which are not natively JSON-serializable.
  • Schema.NumberFromString: this naming suggests that the schema processes numbers that are initially represented as strings, emphasizing the transformation from string to number when decoding.

The primary goal of these schemas is to ensure that domain objects can be easily serialized (“encoded”) and deserialized (“decoded”) for transmission over network connections, thus facilitating their transfer between different parts of an application or across different applications.

While JSON’s ubiquity justifies its primary consideration in naming, the conventions also accommodate serialization for other types of transport. For instance, converting a Date to a string is a universally useful method for various communication protocols, not just JSON. Thus, the selected naming conventions serve as sensible defaults that prioritize clarity and ease of use, facilitating the serialization and deserialization processes across diverse technological environments.