Skip to content

Advanced Usage

To declare a schema for a primitive data type, such as File, you can use the Schema.declare function along with a type guard.

Example (Declaring a Schema for File)

import {
import Schema
Schema
} from "effect"
// Declare a schema for the File type using a type guard
const
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
=
import Schema
Schema
.
const declare: <File>(is: (input: unknown) => input is File, annotations?: Schema.Annotations.Schema<File, readonly []> | undefined) => Schema.SchemaClass<File, File, never> (+1 overload)

The constraint R extends Schema.Context<P[number]> enforces dependencies solely from typeParameters. This ensures that when you call Schema.to or Schema.from, you receive a schema with a never context.

@since3.10.0

declare
(
(
input: unknown
input
: unknown):
input: unknown
input
is
interface File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
=>
input: unknown
input
instanceof
var File: typeof File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
)
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
=
import Schema
Schema
.
decodeUnknownSync<File, File>(schema: Schema.Schema<File, File, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => File
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
)
// Decoding a valid File object
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 decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
(new
var File: new (sources: Array<BinaryLike | Blob>, fileName: string, options?: FileOptions) => File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
([], "")))
/*
Output:
File { size: 0, type: '', name: '', lastModified: 1724774163056 }
*/
// Decoding an invalid input
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
(null)
/*
throws
ParseError: Expected <declaration schema>, actual null
*/

To enhance the default error message, you can add annotations, particularly the identifier, title, and description annotations (none of these annotations are required, but they are encouraged for good practice and can make your schema “self-documenting”). These annotations will be utilized by the messaging system to return more meaningful messages.

  • Identifier: a unique name for the schema
  • Title: a brief, descriptive title
  • Description: a detailed explanation of the schema’s purpose

Example (Declaring a Schema with Annotations)

import {
import Schema
Schema
} from "effect"
// Declare a schema for the File type with additional annotations
const
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
=
import Schema
Schema
.
const declare: <File>(is: (input: unknown) => input is File, annotations?: Schema.Annotations.Schema<File, readonly []> | undefined) => Schema.SchemaClass<File, File, never> (+1 overload)

The constraint R extends Schema.Context<P[number]> enforces dependencies solely from typeParameters. This ensures that when you call Schema.to or Schema.from, you receive a schema with a never context.

@since3.10.0

declare
(
(
input: unknown
input
: unknown):
input: unknown
input
is
interface File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
=>
input: unknown
input
instanceof
var File: typeof File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
,
{
// A unique identifier for the schema
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "FileFromSelf",
// Detailed description of the schema
Annotations.Doc<A>.description?: string
description
: "The `File` type in JavaScript"
}
)
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
=
import Schema
Schema
.
decodeUnknownSync<File, File>(schema: Schema.Schema<File, File, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => File
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
)
// Decoding a valid File object
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 decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
(new
var File: new (sources: Array<BinaryLike | Blob>, fileName: string, options?: FileOptions) => File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
([], "")))
/*
Output:
File { size: 0, type: '', name: '', lastModified: 1724774163056 }
*/
// Decoding an invalid input
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
(null)
/*
throws
ParseError: Expected FileFromSelf, actual null
*/

Type constructors are generic types that take one or more types as arguments and return a new type. To define a schema for a type constructor, you can use the Schema.declare function.

Example (Declaring a Schema for ReadonlySet<A>)

import {
import ParseResult
ParseResult
,
import Schema
Schema
} from "effect"
export const
const MyReadonlySet: <A, I, R>(item: Schema.Schema<A, I, R>) => Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
MyReadonlySet
= <
function (type parameter) A in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
A
,
function (type parameter) I in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
I
,
function (type parameter) R in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
R
>(
// Schema for the elements of the Set
item: Schema.Schema<A, I, R>
item
:
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, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
A
,
function (type parameter) I in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
I
,
function (type parameter) R in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
R
>
):
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface ReadonlySet<T>
ReadonlySet
<
function (type parameter) A in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
A
>,
interface ReadonlySet<T>
ReadonlySet
<
function (type parameter) I in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
I
>,
function (type parameter) R in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
R
> =>
import Schema
Schema
.
const declare: <readonly [Schema.Schema<A, I, R>], ReadonlySet<I>, ReadonlySet<A>>(typeParameters: readonly [Schema.Schema<A, I, R>], options: {
...;
}, annotations?: Schema.Annotations.Schema<...> | undefined) => Schema.SchemaClass<...> (+1 overload)

The constraint R extends Schema.Context<P[number]> enforces dependencies solely from typeParameters. This ensures that when you call Schema.to or Schema.from, you receive a schema with a never context.

@since3.10.0

declare
(
// Store the schema for the Set's elements
[
item: Schema.Schema<A, I, R>
item
],
{
// Decoding function
decode: (typeParameters_0: Schema.Schema<A, I, never>) => (input: unknown, options: ParseOptions, ast: Declaration) => Effect<...>
decode
: (
item: Schema.Schema<A, I, never>
item
) => (
input: unknown
input
,
parseOptions: ParseOptions
parseOptions
,
ast: Declaration
ast
) => {
if (
input: unknown
input
instanceof
var Set: SetConstructor
Set
) {
// Decode each element in the Set
const
const elements: Effect<readonly A[], ParseResult.ParseIssue, never>
elements
=
import ParseResult
ParseResult
.
const decodeUnknown: <readonly A[], readonly I[], never>(schema: Schema.Schema<readonly A[], readonly I[], never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Effect<...>

@since3.10.0

decodeUnknown
(
import Schema
Schema
.
Array<Schema.Schema<A, I, never>>(value: Schema.Schema<A, I, never>): Schema.Array$<Schema.Schema<A, I, never>>
export Array

@since3.10.0

Array
(
item: Schema.Schema<A, I, never>
item
))(
var Array: ArrayConstructor
Array
.
ArrayConstructor.from<any>(iterable: Iterable<any> | ArrayLike<any>): any[] (+3 overloads)

Creates an array from an iterable object.

@paramiterable An iterable object to convert to an array.

from
(
input: Set<any>
input
.
Set<any>.values(): SetIterator<any>

Returns an iterable of values in the set.

values
()),
parseOptions: ParseOptions
parseOptions
)
// Return a ReadonlySet containing the decoded elements
return
import ParseResult
ParseResult
.
const map: <readonly A[], ParseResult.ParseIssue, never, ReadonlySet<A>>(self: Effect<readonly A[], ParseResult.ParseIssue, never>, f: (a: readonly A[]) => ReadonlySet<A>) => Effect<...> (+1 overload)

@since3.10.0

map
(
const elements: Effect<readonly A[], ParseResult.ParseIssue, never>
elements
,
(
as: readonly A[]
as
):
interface ReadonlySet<T>
ReadonlySet
<
function (type parameter) A in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
A
> => new
var Set: SetConstructor
new <A>(iterable?: Iterable<A> | null | undefined) => Set<A> (+1 overload)
Set
(
as: readonly A[]
as
)
)
}
// Handle invalid input
return
import ParseResult
ParseResult
.
const fail: (issue: ParseResult.ParseIssue) => Either<never, ParseResult.ParseIssue>

@since3.10.0

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

The Type variant of the ParseIssue type represents an error that occurs when the actual value is not of the expected type. The ast field specifies the expected type, and the actual field contains the value that caused the error.

@since3.10.0

Type
(
ast: Declaration
ast
,
input: unknown
input
))
},
// Encoding function
encode: (typeParameters_0: Schema.Schema<A, I, never>) => (input: unknown, options: ParseOptions, ast: Declaration) => Effect<...>
encode
: (
item: Schema.Schema<A, I, never>
item
) => (
input: unknown
input
,
parseOptions: ParseOptions
parseOptions
,
ast: Declaration
ast
) => {
if (
input: unknown
input
instanceof
var Set: SetConstructor
Set
) {
// Encode each element in the Set
const
const elements: Effect<readonly I[], ParseResult.ParseIssue, never>
elements
=
import ParseResult
ParseResult
.
const encodeUnknown: <readonly A[], readonly I[], never>(schema: Schema.Schema<readonly A[], readonly I[], never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Effect<...>

@since3.10.0

encodeUnknown
(
import Schema
Schema
.
Array<Schema.Schema<A, I, never>>(value: Schema.Schema<A, I, never>): Schema.Array$<Schema.Schema<A, I, never>>
export Array

@since3.10.0

Array
(
item: Schema.Schema<A, I, never>
item
))(
var Array: ArrayConstructor
Array
.
ArrayConstructor.from<any>(iterable: Iterable<any> | ArrayLike<any>): any[] (+3 overloads)

Creates an array from an iterable object.

@paramiterable An iterable object to convert to an array.

from
(
input: Set<any>
input
.
Set<any>.values(): SetIterator<any>

Returns an iterable of values in the set.

values
()),
parseOptions: ParseOptions
parseOptions
)
// Return a ReadonlySet containing the encoded elements
return
import ParseResult
ParseResult
.
const map: <readonly I[], ParseResult.ParseIssue, never, ReadonlySet<I>>(self: Effect<readonly I[], ParseResult.ParseIssue, never>, f: (a: readonly I[]) => ReadonlySet<I>) => Effect<...> (+1 overload)

@since3.10.0

map
(
const elements: Effect<readonly I[], ParseResult.ParseIssue, never>
elements
,
(
is: readonly I[]
is
):
interface ReadonlySet<T>
ReadonlySet
<
function (type parameter) I in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
I
> => new
var Set: SetConstructor
new <I>(iterable?: Iterable<I> | null | undefined) => Set<I> (+1 overload)
Set
(
is: readonly I[]
is
)
)
}
// Handle invalid input
return
import ParseResult
ParseResult
.
const fail: (issue: ParseResult.ParseIssue) => Either<never, ParseResult.ParseIssue>

@since3.10.0

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

The Type variant of the ParseIssue type represents an error that occurs when the actual value is not of the expected type. The ast field specifies the expected type, and the actual field contains the value that caused the error.

@since3.10.0

Type
(
ast: Declaration
ast
,
input: unknown
input
))
}
},
{
Annotations.Doc<A>.description?: string
description
: `ReadonlySet<${
import Schema
Schema
.
const format: <Schema.Schema<A, I, R>>(schema: Schema.Schema<A, I, R>) => string

@since3.10.0

format
(
item: Schema.Schema<A, I, R>
item
)}>`
}
)
// Define a schema for a ReadonlySet of numbers
const
const setOfNumbers: Schema.Schema<ReadonlySet<number>, ReadonlySet<string>, never>
setOfNumbers
=
const MyReadonlySet: <number, string, never>(item: Schema.Schema<number, string, never>) => Schema.Schema<ReadonlySet<number>, ReadonlySet<string>, never>
MyReadonlySet
(
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
)
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<number>
decode
=
import Schema
Schema
.
decodeUnknownSync<ReadonlySet<number>, ReadonlySet<string>>(schema: Schema.Schema<ReadonlySet<number>, ReadonlySet<string>, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<...>
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const setOfNumbers: Schema.Schema<ReadonlySet<number>, ReadonlySet<string>, never>
setOfNumbers
)
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 decode: (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<number>
decode
(new
var Set: SetConstructor
new <string>(iterable?: Iterable<string> | null | undefined) => Set<string> (+1 overload)
Set
(["1", "2", "3"]))) // Set(3) { 1, 2, 3 }
// Decode an invalid input
const decode: (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<number>
decode
(null)
/*
throws
ParseError: Expected ReadonlySet<NumberFromString>, actual null
*/
// Decode a Set with an invalid element
const decode: (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<number>
decode
(new
var Set: SetConstructor
new <string | null>(iterable?: Iterable<string | null> | null | undefined) => Set<string | null> (+1 overload)
Set
(["1", null, "3"]))
/*
throws
ParseError: ReadonlyArray<NumberFromString>
└─ [1]
└─ NumberFromString
└─ Encoded side transformation failure
└─ Expected string, actual null
*/

When defining a new data type, some compilers like Arbitrary or Pretty may not know how to handle the new type. This can result in an error, as the compiler may lack the necessary information for generating instances or producing readable output:

Example (Attempting to Generate Arbitrary Values Without Required Annotations)

import {
import Arbitrary
Arbitrary
,
import Schema
Schema
} from "effect"
// Define a schema for the File type
const
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
=
import Schema
Schema
.
const declare: <File>(is: (input: unknown) => input is File, annotations?: Schema.Annotations.Schema<File, readonly []> | undefined) => Schema.SchemaClass<File, File, never> (+1 overload)

The constraint R extends Schema.Context<P[number]> enforces dependencies solely from typeParameters. This ensures that when you call Schema.to or Schema.from, you receive a schema with a never context.

@since3.10.0

declare
(
(
input: unknown
input
: unknown):
input: unknown
input
is
interface File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
=>
input: unknown
input
instanceof
var File: typeof File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
,
{
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "FileFromSelf"
}
)
// Try creating an Arbitrary instance for the schema
const
const arb: Arbitrary<File>
arb
=
import Arbitrary
Arbitrary
.
const make: <File, File, never>(schema: Schema.Schema<File, File, never>) => Arbitrary<File>

Returns a fast-check Arbitrary for the A type of the provided schema.

@since3.10.0

make
(
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
)
/*
throws:
Error: Missing annotation
details: Generating an Arbitrary for this schema requires an "arbitrary" annotation
schema (Declaration): FileFromSelf
*/

In the above example, attempting to generate arbitrary values for the FileFromSelf schema fails because the compiler lacks necessary annotations. To resolve this, you need to provide annotations for generating arbitrary data:

Example (Adding Arbitrary Annotation for Custom File Schema)

import {
import Arbitrary
Arbitrary
,
import FastCheck
FastCheck
,
import Pretty
Pretty
,
import Schema
Schema
} from "effect"
const
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
=
import Schema
Schema
.
const declare: <File>(is: (input: unknown) => input is File, annotations?: Schema.Annotations.Schema<File, readonly []> | undefined) => Schema.SchemaClass<File, File, never> (+1 overload)

The constraint R extends Schema.Context<P[number]> enforces dependencies solely from typeParameters. This ensures that when you call Schema.to or Schema.from, you receive a schema with a never context.

@since3.10.0

declare
(
(
input: unknown
input
: unknown):
input: unknown
input
is
interface File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
=>
input: unknown
input
instanceof
var File: typeof File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
,
{
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "FileFromSelf",
// Provide a function to generate random File instances
Annotations.Schema<File, readonly []>.arbitrary?: Arbitrary.ArbitraryAnnotation<File, readonly []>
arbitrary
: () => (
fc: typeof FastCheck
fc
) =>
fc: typeof FastCheck
fc
.
tuple<[string, string]>(arbs_0: FastCheck.Arbitrary<string>, arbs_1: FastCheck.Arbitrary<string>): FastCheck.Arbitrary<[string, string]>
export tuple

For tuples produced using the provided arbs

@paramarbs - Ordered list of arbitraries

@public

tuple
(
fc: typeof FastCheck
fc
.
function string(constraints?: FastCheck.StringConstraints): FastCheck.Arbitrary<string>
export string

For strings of

char

@paramconstraints - Constraints to apply when building instances (since 2.4.0)

@public

string
(),
fc: typeof FastCheck
fc
.
function string(constraints?: FastCheck.StringConstraints): FastCheck.Arbitrary<string>
export string

For strings of

char

@paramconstraints - Constraints to apply when building instances (since 2.4.0)

@public

string
())
.
Arbitrary<[string, string]>.map<File>(mapper: (t: [string, string]) => File, unmapper?: ((possiblyU: unknown) => [string, string]) | undefined): FastCheck.Arbitrary<File>

Create another arbitrary by mapping all produced values using the provided mapper Values produced by the new arbitrary are the result of applying mapper value by value

@example

const rgbChannels: Arbitrary<{r:number,g:number,b:number}> = ...;
const color: Arbitrary<string> = rgbChannels.map(ch => `#${(ch.r*65536 + ch.g*256 + ch.b).toString(16).padStart(6, '0')}`);
// transform an Arbitrary producing {r,g,b} integers into an Arbitrary of '#rrggbb'

@parammapper - Map function, to produce a new element based on an old one

@paramunmapper - Optional unmap function, it will never be used except when shrinking user defined values. Must throw if value is not compatible (since 3.0.0)

@returnsNew arbitrary with mapped elements

map
(([
content: string
content
,
path: string
path
]) => new
var File: new (sources: Array<BinaryLike | Blob>, fileName: string, options?: FileOptions) => File

File class is a global reference for import { File } from 'node:buffer' https://nodejs.org/api/buffer.html#class-file

@sincev20.0.0

File
([
content: string
content
],
path: string
path
))
}
)
// Create an Arbitrary instance for the schema
const
const arb: FastCheck.Arbitrary<File>
arb
=
import Arbitrary
Arbitrary
.
const make: <File, File, never>(schema: Schema.Schema<File, File, never>) => FastCheck.Arbitrary<File>

Returns a fast-check Arbitrary for the A type of the provided schema.

@since3.10.0

make
(
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
)
// Generate sample files using the Arbitrary instance
const
const files: File[]
files
=
import FastCheck
FastCheck
.
sample<File>(generator: FastCheck.Arbitrary<File> | FastCheck.IRawProperty<File, boolean>, params?: number | FastCheck.Parameters<File> | undefined): File[]
export sample

Generate an array containing all the values that would have been generated during

assert

or

check

@example

fc.sample(fc.nat(), 10); // extract 10 values from fc.nat() Arbitrary
fc.sample(fc.nat(), {seed: 42}); // extract values from fc.nat() as if we were running fc.assert with seed=42

@paramgenerator - IProperty or Arbitrary to extract the values from

@paramparams - Integer representing the number of values to generate or Parameters as in assert

@public

sample
(
const arb: FastCheck.Arbitrary<File>
arb
, 2)
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const files: File[]
files
)
/*
Example Output:
[
File { size: 5, type: '', name: 'C', lastModified: 1706435571176 },
File { size: 1, type: '', name: '98Ggmc', lastModified: 1706435571176 }
]
*/

For more details on how to add annotations for the Arbitrary compiler, refer to the Arbitrary documentation.

TypeScript’s type system is structural, which means that any two types that are structurally equivalent are considered the same. This can cause issues when types that are semantically different are treated as if they were the same.

Example (Structural Typing Issue)

type
type UserId = string
UserId
= string
type
type Username = string
Username
= string
declare const
const getUser: (id: UserId) => object
getUser
: (
id: string
id
:
type UserId = string
UserId
) => object
const
const myUsername: string
myUsername
:
type Username = string
Username
= "gcanti"
const getUser: (id: UserId) => object
getUser
(
const myUsername: string
myUsername
) // This erroneously works

In the above example, UserId and Username are both aliases for the same type, string. This means that the getUser function can mistakenly accept a Username as a valid UserId, causing bugs and errors.

To prevent this, Effect introduces branded types. These types attach a unique identifier (or “brand”) to a type, allowing you to differentiate between structurally similar but semantically distinct types.

Example (Defining Branded Types)

import {
import Brand
Brand
} from "effect"
type
type UserId = string & Brand.Brand<"UserId">
UserId
= string &
import Brand
Brand
.
interface Brand<in out K extends string | symbol>

A generic interface that defines a branded type.

@since2.0.0

@since2.0.0

Brand
<"UserId">
type
type Username = string
Username
= string
declare const
const getUser: (id: UserId) => object
getUser
: (
id: UserId
id
:
type UserId = string & Brand.Brand<"UserId">
UserId
) => object
const
const myUsername: string
myUsername
:
type Username = string
Username
= "gcanti"
// @ts-expect-error
const getUser: (id: UserId) => object
getUser
(
const myUsername: string
myUsername
)
/*
Argument of type 'string' is not assignable to parameter of type 'UserId'.
Type 'string' is not assignable to type 'Brand<"UserId">'.ts(2345)
*/

By defining UserId as a branded type, the getUser function can accept only values of type UserId, and not plain strings or other types that are compatible with strings. This helps to prevent bugs caused by accidentally passing the wrong type of value to the function.

There are two ways to define a schema for a branded type, depending on whether you:

  • want to define the schema from scratch
  • have already defined a branded type via effect/Brand and want to reuse it to define a schema

To define a schema for a branded type from scratch, use the Schema.brand function.

Example (Creating a schema for a Branded Type)

import {
import Schema
Schema
} from "effect"
const
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
=
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.brand<typeof Schema.String, "UserId">>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.brand<typeof Schema.String, "UserId">): Schema.brand<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const brand: <typeof Schema.String, "UserId">(brand: "UserId", annotations?: Schema.Annotations.Schema<string & Brand<"UserId">, readonly []> | undefined) => (self: typeof Schema.String) => Schema.brand<...>

Returns a nominal branded schema by applying a brand to a given schema.

Schema<A> + B -> Schema<A & Brand<B>>

@paramself - The input schema to be combined with the brand.

@parambrand - The brand to apply.

@example

import * as Schema from "effect/Schema"
const Int = Schema.Number.pipe(Schema.int(), Schema.brand("Int"))
type Int = Schema.Schema.Type<typeof Int> // number & Brand<"Int">

@since3.10.0

brand
("UserId"))
// string & Brand<"UserId">
type
type UserId = string & Brand<"UserId">
UserId
= typeof
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
.
Schema<string & Brand<"UserId">, string, never>.Type: string & Brand<"UserId">
Type

Note that you can use unique symbols as brands to ensure uniqueness across modules / packages.

Example (Using a unique symbol as a Brand)

import {
import Schema
Schema
} from "effect"
const
const UserIdBrand: typeof UserIdBrand
UserIdBrand
: unique symbol =
var Symbol: SymbolConstructor
Symbol
.
SymbolConstructor.for(key: string): symbol

Returns a Symbol object from the global symbol registry matching the given key if found. Otherwise, returns a new symbol with this key.

@paramkey key to search for.

for
("UserId")
const
const UserId: Schema.brand<typeof Schema.String, typeof UserIdBrand>
UserId
=
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.brand<typeof Schema.String, typeof UserIdBrand>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.brand<typeof Schema.String, typeof UserIdBrand>): Schema.brand<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const brand: <typeof Schema.String, typeof UserIdBrand>(brand: typeof UserIdBrand, annotations?: Schema.Annotations.Schema<string & Brand<typeof UserIdBrand>, readonly []> | undefined) => (self: typeof Schema.String) => Schema.brand<...>

Returns a nominal branded schema by applying a brand to a given schema.

Schema<A> + B -> Schema<A & Brand<B>>

@paramself - The input schema to be combined with the brand.

@parambrand - The brand to apply.

@example

import * as Schema from "effect/Schema"
const Int = Schema.Number.pipe(Schema.int(), Schema.brand("Int"))
type Int = Schema.Schema.Type<typeof Int> // number & Brand<"Int">

@since3.10.0

brand
(
const UserIdBrand: typeof UserIdBrand
UserIdBrand
))
// string & Brand<typeof UserIdBrand>
type
type UserId = string & Brand<typeof UserIdBrand>
UserId
= typeof
const UserId: Schema.brand<typeof Schema.String, typeof UserIdBrand>
UserId
.
Schema<string & Brand<typeof UserIdBrand>, string, never>.Type: string & Brand<typeof UserIdBrand>
Type

If you have already defined a branded type using the effect/Brand module, you can reuse it to define a schema using the Schema.fromBrand function.

Example (Reusing an Existing Branded Type)

import {
import Schema
Schema
} from "effect"
import {
import Brand
Brand
} from "effect"
// the existing branded type
type
type UserId = string & Brand.Brand<"UserId">
UserId
= string &
import Brand
Brand
.
interface Brand<in out K extends string | symbol>

A generic interface that defines a branded type.

@since2.0.0

@since2.0.0

Brand
<"UserId">
const
const UserId: Brand.Brand.Constructor<UserId>
UserId
=
import Brand
Brand
.
const nominal: <UserId>() => Brand.Brand<in out K extends string | symbol>.Constructor<UserId>

This function returns a Brand.Constructor that does not apply any runtime checks, it just returns the provided value. It can be used to create nominal types that allow distinguishing between two values of the same type but with different meanings.

If you also want to perform some validation, see

refined

.

@example

import { Brand } from "effect"
type UserId = number & Brand.Brand<"UserId">
const UserId = Brand.nominal<UserId>()
assert.strictEqual(UserId(1), 1)

@since2.0.0

nominal
<
type UserId = string & Brand.Brand<"UserId">
UserId
>()
// Define a schema for the branded type
const
const UserIdSchema: Schema.BrandSchema<string & Brand.Brand<"UserId">, string, never>
UserIdSchema
=
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.BrandSchema<string & Brand.Brand<"UserId">, string, never>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.BrandSchema<string & Brand.Brand<"UserId">, string, never>): Schema.BrandSchema<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const fromBrand: <UserId, string>(constructor: Brand.Brand<in out K extends string | symbol>.Constructor<UserId>, annotations?: Schema.Annotations.Filter<UserId, string> | undefined) => <I, R>(self: Schema.Schema<...>) => Schema.BrandSchema<...>

@since3.10.0

fromBrand
(
const UserId: Brand.Brand.Constructor<UserId>
UserId
))

The Schema.brand function includes a default constructor to facilitate the creation of branded values.

import {
import Schema
Schema
} from "effect"
const
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
=
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.brand<typeof Schema.String, "UserId">>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.brand<typeof Schema.String, "UserId">): Schema.brand<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const brand: <typeof Schema.String, "UserId">(brand: "UserId", annotations?: Schema.Annotations.Schema<string & Brand<"UserId">, readonly []> | undefined) => (self: typeof Schema.String) => Schema.brand<...>

Returns a nominal branded schema by applying a brand to a given schema.

Schema<A> + B -> Schema<A & Brand<B>>

@paramself - The input schema to be combined with the brand.

@parambrand - The brand to apply.

@example

import * as Schema from "effect/Schema"
const Int = Schema.Number.pipe(Schema.int(), Schema.brand("Int"))
type Int = Schema.Schema.Type<typeof Int> // number & Brand<"Int">

@since3.10.0

brand
("UserId"))
const
const userId: string & Brand<"UserId">
userId
=
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
.
BrandSchema<string & Brand<"UserId">, string, never>.make(a: string, options?: MakeOptions): string & Brand<"UserId">
make
("123") // Creates a branded UserId

A PropertySignature represents a transformation from a “From” field to a “To” field. This allows you to define mappings between incoming data fields and your internal model.

A property signature can be defined with annotations to provide additional context about a field.

Example (Adding Annotations to a Property Signature)

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

@since3.10.0

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

@since3.10.0

String
,
age: Schema.propertySignature<typeof Schema.NumberFromString>
age
:
import Schema
Schema
.
const propertySignature: <typeof Schema.NumberFromString>(self: typeof Schema.NumberFromString) => Schema.propertySignature<typeof Schema.NumberFromString>

Lifts a Schema into a PropertySignature.

@since3.10.0

propertySignature
(
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
).
propertySignature<typeof NumberFromString>.annotations(annotations: Schema.PropertySignature<TypeToken extends Schema.PropertySignature.Token, Type, Key extends PropertyKey, EncodedToken extends Schema.PropertySignature.Token, Encoded, HasDefault extends boolean = false, R = never>.Annotations<number>): Schema.propertySignature<...>
annotations
({
Annotations.Doc<number>.title?: string
title
: "Age" // Annotation to label the age field
})
})

A PropertySignature type contains several parameters, each providing details about the transformation between the source field (From) and the target field (To). Let’s take a look at what each of these parameters represents:

age: PropertySignature<
ToToken,
ToType,
FromKey,
FromToken,
FromType,
HasDefault,
Context
>
ParameterDescription
ageKey of the “To” field
ToTokenIndicates field requirement: "?:" for optional, ":" for required
ToTypeType of the “To” field
FromKey(Optional, default = never) Indicates the source field key, typically the same as “To” field key unless specified
FormTokenIndicates source field requirement: "?:" for optional, ":" for required
FromTypeType of the “From” field
HasDefaultIndicates if there is a constructor default value (Boolean)

In the example above, the PropertySignature type for age is:

PropertySignature<":", number, never, ":", string, false, never>

This means:

ParameterDescription
ageKey of the “To” field
ToToken":" indicates that the age field is required
ToTypeType of the age field is number
FromKeynever indicates that the decoding occurs from the same field named age
FormToken":" indicates that the decoding occurs from a required age field
FromTypeType of the “From” field is string
HasDefaultfalse: indicates there is no default value

Sometimes, the source field (the “From” field) may have a different name from the field in your internal model. You can map between these fields using the Schema.fromKey function.

Example (Mapping from a Different Key)

import {
import Schema
Schema
} from "effect"
const
const Person: Schema.Struct<{
name: typeof Schema.String;
age: Schema.PropertySignature<":", number, "AGE", ":", string, false, never>;
}>
Person
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
age: Schema.PropertySignature<":", number, "AGE", ":", string, false, never>;
}>(fields: {
name: typeof Schema.String;
age: Schema.PropertySignature<":", number, "AGE", ":", string, false, never>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

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

@since3.10.0

String
,
age: Schema.PropertySignature<":", number, "AGE", ":", string, false, never>
age
:
import Schema
Schema
.
const propertySignature: <typeof Schema.NumberFromString>(self: typeof Schema.NumberFromString) => Schema.propertySignature<typeof Schema.NumberFromString>

Lifts a Schema into a PropertySignature.

@since3.10.0

propertySignature
(
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
).
Pipeable.pipe<Schema.propertySignature<typeof Schema.NumberFromString>, Schema.PropertySignature<":", number, "AGE", ":", string, false, never>>(this: Schema.propertySignature<...>, ab: (_: Schema.propertySignature<typeof Schema.NumberFromString>) => Schema.PropertySignature<...>): Schema.PropertySignature<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const fromKey: <"AGE">(key: "AGE") => <TypeToken, Type, EncodedToken, Encoded, HasDefault, R>(self: Schema.PropertySignature<TypeToken, Type, PropertyKey, EncodedToken, Encoded, HasDefault, R>) => Schema.PropertySignature<...> (+1 overload)

Enhances a property signature by specifying a different key for it in the Encoded type.

@since3.10.0

fromKey
("AGE") // Maps from "AGE" to "age"
)
})
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: string;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly AGE: string;
}, 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: Schema.PropertySignature<":", number, "AGE", ":", string, false, never>;
}>
Person
)({
name: string
name
: "name",
type AGE: string
AGE
: "18" }))
// Output: { name: 'name', age: 18 }

When you map from "AGE" to "age", the PropertySignature type changes to:

PropertySignature<":", number, never, ":", string, false, never>
PropertySignature<":", number, "AGE", ":", string, false, never>

The syntax:

Schema.optional(schema: Schema<A, I, R>)

creates an optional property within a schema, allowing fields to be omitted or set to undefined.

InputOutput
<missing value>remains <missing value>
undefinedremains undefined
i: Itransforms to a: A
InputOutput
<missing value>remains <missing value>
undefinedremains undefined
a: Atransforms back to i: I

Example (Defining an Optional Number Field)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>(fields: {
quantity: Schema.optional<typeof Schema.NumberFromString>;
}): Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optional<typeof Schema.NumberFromString>
quantity
:
import Schema
Schema
.
const optional: <typeof Schema.NumberFromString>(self: typeof Schema.NumberFromString) => Schema.optional<typeof Schema.NumberFromString>

@since3.10.0

optional
(
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
)
})
// ┌─── { readonly quantity?: string | undefined; }
// ▼
type
type Encoded = {
readonly quantity?: string | undefined;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
.
Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>.Encoded: {
readonly quantity?: string | undefined;
}
Encoded
// ┌─── { readonly quantity?: number | undefined; }
// ▼
type
type Type = {
readonly quantity?: number | undefined;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
.
Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>.Type: {
readonly quantity?: number | undefined;
}
Type
// Decoding examples
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 quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number | undefined;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
)({
quantity: string
quantity
: "1" }))
// Output: { quantity: 1 }
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 quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number | undefined;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
)({}))
// 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 quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number | undefined;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: undefined }
// Encoding examples
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 quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number | undefined;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | undefined;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
)({
quantity?: number | undefined
quantity
: 1 }))
// Output: { quantity: "1" }
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 quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number | undefined;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | undefined;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
)({}))
// 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
.
encodeSync<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number | undefined;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | undefined;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
)({
quantity?: number | undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: undefined }

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>(fields: {
quantity: Schema.optional<typeof Schema.NumberFromString>;
}): Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optional<typeof Schema.NumberFromString>
quantity
:
import Schema
Schema
.
const optional: <typeof Schema.NumberFromString>(self: typeof Schema.NumberFromString) => Schema.optional<typeof Schema.NumberFromString>

@since3.10.0

optional
(
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
)
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.NumberFromString>;
}>
Product
.
TypeLiteral<{ quantity: optional<typeof NumberFromString>; }, []>.fields: {
readonly quantity: Schema.optional<typeof Schema.NumberFromString>;
}
fields
.
quantity: Schema.optional<typeof Schema.NumberFromString>
quantity
.
optional<typeof NumberFromString>.from: typeof Schema.NumberFromString
from

The syntax:

Schema.optionalWith(schema: Schema<A, I, R>, { nullable: true })

creates an optional property within a schema, treating null values the same as missing values.

InputOutput
<missing value>remains <missing value>
undefinedremains undefined
nulltransforms to <missing value>
i: Itransforms to a: A
InputOutput
<missing value>remains <missing value>
undefinedremains undefined
a: Atransforms back to i: I

Example (Handling Null as Missing Value)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
nullable: true
nullable
: true
})
})
// ┌─── { readonly quantity?: string | null | undefined; }
// ▼
type
type Encoded = {
readonly quantity?: string | null | undefined;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>.Encoded: {
readonly quantity?: string | null | undefined;
}
Encoded
// ┌─── { readonly quantity?: number | undefined; }
// ▼
type
type Type = {
readonly quantity?: number | undefined;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>.Type: {
readonly quantity?: number | undefined;
}
Type
// Decoding examples
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 quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number | undefined;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
)({
quantity: string
quantity
: "1" }))
// Output: { quantity: 1 }
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 quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number | undefined;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
)({}))
// 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 quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number | undefined;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: undefined }
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 quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number | undefined;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
)({
quantity: null
quantity
: null }))
// Output: {}
// Encoding examples
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 quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number | undefined;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | null | undefined;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
)({
quantity?: number | undefined
quantity
: 1 }))
// Output: { quantity: "1" }
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 quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number | undefined;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | null | undefined;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
)({}))
// 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
.
encodeSync<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity?: number | undefined;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number | undefined;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | null | undefined;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
)({
quantity?: number | undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: undefined }

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
nullable: true
nullable
: true
})
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { nullable: true; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
nullable: true;
}>
quantity
.
optionalWith<typeof NumberFromString, { nullable: true; }>.from: typeof Schema.NumberFromString
from

The syntax:

Schema.optionalWith(schema: Schema<A, I, R>, { exact: true })

creates an optional property while enforcing strict typing. This means that only the specified type (excluding undefined) is accepted. Any attempt to decode undefined results in an error.

InputOutput
<missing value>remains <missing value>
undefinedParseError
i: Itransforms to a: A
InputOutput
<missing value>remains <missing value>
a: Atransforms back to i: I

Example (Using Exactness with Optional Field)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
exact: true;
}>(self: typeof Schema.NumberFromString, options: {
exact: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
exact: true
exact
: true })
})
// ┌─── { readonly quantity?: string; }
// ▼
type
type Encoded = {
readonly quantity?: string;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>.Encoded: {
readonly quantity?: string;
}
Encoded
// ┌─── { readonly quantity?: number; }
// ▼
type
type Type = {
readonly quantity?: number;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>.Type: {
readonly quantity?: number;
}
Type
// Decoding examples
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 quantity?: number;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
)({
quantity: string
quantity
: "1" }))
// Output: { quantity: 1 }
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 quantity?: number;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
)({}))
// 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 quantity?: number;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
/*
throws:
ParseError: { readonly quantity?: NumberFromString }
└─ ["quantity"]
└─ NumberFromString
└─ Encoded side transformation failure
└─ Expected string, actual undefined
*/
// Encoding examples
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 quantity?: number;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
)({
quantity?: number
quantity
: 1 }))
// Output: { quantity: "1" }
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 quantity?: number;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
)({}))
// Output: {}

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
exact: true;
}>(self: typeof Schema.NumberFromString, options: {
exact: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
exact: true
exact
: true })
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { exact: true; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
}>
quantity
.
optionalWith<typeof NumberFromString, { exact: true; }>.from: typeof Schema.NumberFromString
from

The syntax:

Schema.optionalWith(schema: Schema<A, I, R>, { exact: true, nullable: true })

allows you to define an optional property that enforces strict typing (exact type only) while also treating null as equivalent to a missing value.

InputOutput
<missing value>remains <missing value>
nulltransforms to <missing value>
undefinedParseError
i: Itransforms to a: A
InputOutput
<missing value>remains <missing value>
a: Atransforms back to i: I

Example (Using Exactness and Handling Null as Missing Value with Optional Field)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
exact: true;
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
exact: true
exact
: true,
nullable: true
nullable
: true
})
})
// ┌─── { readonly quantity?: string | null; }
// ▼
type
type Encoded = {
readonly quantity?: string | null;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>.Encoded: {
readonly quantity?: string | null;
}
Encoded
// ┌─── { readonly quantity?: number; }
// ▼
type
type Type = {
readonly quantity?: number;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>.Type: {
readonly quantity?: number;
}
Type
// Decoding examples
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 quantity?: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: string
quantity
: "1" }))
// Output: { quantity: 1 }
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 quantity?: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
)({}))
// 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 quantity?: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
/*
throws:
ParseError: (Struct (Encoded side) <-> Struct (Type side))
└─ Encoded side transformation failure
└─ Struct (Encoded side)
└─ ["quantity"]
└─ NumberFromString | null
├─ NumberFromString
│ └─ Encoded side transformation failure
│ └─ Expected string, actual undefined
└─ Expected null, actual undefined
*/
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 quantity?: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity?: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: null
quantity
: null }))
// Output: {}
// Encoding examples
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 quantity?: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | null;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity?: number
quantity
: 1 }))
// Output: { quantity: "1" }
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 quantity?: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity?: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (a: {
readonly quantity?: number;
}, overrideOptions?: ParseOptions) => {
readonly quantity?: string | null;
}
export encodeSync

@since3.10.0

encodeSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
)({}))
// Output: {}

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
exact: true;
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
exact: true
exact
: true,
nullable: true
nullable
: true
})
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { exact: true; nullable: true; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
exact: true;
nullable: true;
}>
quantity
.
optionalWith<typeof NumberFromString, { exact: true; nullable: true; }>.from: typeof Schema.NumberFromString
from

When creating a schema to replicate a TypeScript type that includes optional fields with the never type, like:

type MyType = {
readonly quantity?: never
}

the handling of these fields depends on the exactOptionalPropertyTypes setting in your tsconfig.json. This setting affects whether the schema should treat optional never-typed fields as simply absent or allow undefined as a value.

Example (exactOptionalPropertyTypes: false)

When this feature is turned off, you can employ the Schema.optional function. This approach allows the field to implicitly accept undefined as a value.

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.Never>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optional<typeof Schema.Never>;
}>(fields: {
quantity: Schema.optional<typeof Schema.Never>;
}): Schema.Struct<{
quantity: Schema.optional<typeof Schema.Never>;
}> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optional<typeof Schema.Never>
quantity
:
import Schema
Schema
.
const optional: <typeof Schema.Never>(self: typeof Schema.Never) => Schema.optional<typeof Schema.Never>

@since3.10.0

optional
(
import Schema
Schema
.
class Never

@since3.10.0

Never
)
})
// ┌─── { readonly quantity?: undefined; }
// ▼
type
type Encoded = {
readonly quantity?: undefined;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.Never>;
}>
Product
.
Schema<{ readonly quantity?: undefined; }, { readonly quantity?: undefined; }, never>.Encoded: {
readonly quantity?: undefined;
}
Encoded
// ┌─── { readonly quantity?: undefined; }
// ▼
type
type Type = {
readonly quantity?: undefined;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optional<typeof Schema.Never>;
}>
Product
.
Schema<{ readonly quantity?: undefined; }, { readonly quantity?: undefined; }, never>.Type: {
readonly quantity?: undefined;
}
Type

Example (exactOptionalPropertyTypes: true)

When this feature is turned on, the Schema.optionalWith function is recommended. It ensures stricter enforcement of the field’s absence.

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.Never, {
exact: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.Never, {
exact: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.Never, {
exact: true;
}>;
}): Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.Never, {
exact: true;
}>;
}> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.Never, {
exact: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.Never, {
exact: true;
}>(self: typeof Schema.Never, options: {
exact: true;
}) => Schema.optionalWith<typeof Schema.Never, {
exact: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
import Schema
Schema
.
class Never

@since3.10.0

Never
, {
exact: true
exact
: true })
})
// ┌─── { readonly quantity?: never; }
// ▼
type
type Encoded = {
readonly quantity?: never;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.Never, {
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: never; }, { readonly quantity?: never; }, never>.Encoded: {
readonly quantity?: never;
}
Encoded
// ┌─── { readonly quantity?: never; }
// ▼
type
type Type = {
readonly quantity?: never;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.Never, {
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity?: never; }, { readonly quantity?: never; }, never>.Type: {
readonly quantity?: never;
}
Type

The default option in Schema.optionalWith allows you to set default values that are applied during both decoding and object construction phases. This feature ensures that even if certain properties are not provided by the user, the system will automatically use the specified default values.

The Schema.optionalWith function offers several ways to control how defaults are applied during decoding and encoding. You can fine-tune whether defaults are applied only when the input is completely missing, or even when null or undefined values are provided.

This is the simplest use case. If the input is missing or undefined, the default value will be applied.

Syntax

Schema.optionalWith(schema: Schema<A, I, R>, { default: () => A })
OperationBehavior
DecodingApplies the default value if the input is missing or undefined
EncodingTransforms the input a: A back to i: I

Example (Applying Default When Field Is Missing or undefined)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
default: () => number;
}>(self: typeof Schema.NumberFromString, options: {
default: () => number;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
default: () => number
default
: () => 1 // Default value for quantity
})
})
// ┌─── { readonly quantity?: string | undefined; }
// ▼
type
type Encoded = {
readonly quantity?: string | undefined;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }, never>.Encoded: {
readonly quantity?: string | undefined;
}
Encoded
// ┌─── { readonly quantity: number; }
// ▼
type
type Type = {
readonly quantity: number;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }, never>.Type: {
readonly quantity: number;
}
Type
// Decoding examples with default applied
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 quantity: number;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
)({}))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: 2 }
// Object construction examples with default applied
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 Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.make(props: void | {
readonly quantity?: number;
}, options?: MakeOptions): {
readonly quantity: number;
}
make
({}))
// Output: { quantity: 1 }
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 Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.make(props: void | {
readonly quantity?: number;
}, options?: MakeOptions): {
readonly quantity: number;
}
make
({
quantity?: number
quantity
: 2 }))
// Output: { quantity: 2 }

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
default: () => number;
}>(self: typeof Schema.NumberFromString, options: {
default: () => number;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
default: () => number
default
: () => 1 // Default value for quantity
})
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
}>
quantity
.
optionalWith<typeof NumberFromString, { default: () => number; }>.from: typeof Schema.NumberFromString
from

When you want the default value to be applied only if the field is completely missing (not when it’s undefined), you can use the exact option.

Syntax

Schema.optionalWith(schema: Schema<A, I, R>, {
default: () => A,
exact: true
})
OperationBehavior
DecodingApplies the default value only if the input is missing
EncodingTransforms the input a: A back to i: I

Example (Applying Default Only When Field Is Missing)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>(self: typeof Schema.NumberFromString, options: {
default: () => number;
exact: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
default: () => number
default
: () => 1, // Default value for quantity
exact: true
exact
: true // Only apply default if quantity is not provided
})
})
// ┌─── { readonly quantity?: string; }
// ▼
type
type Encoded = {
readonly quantity?: string;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string; }, never>.Encoded: {
readonly quantity?: string;
}
Encoded
// ┌─── { readonly quantity: number; }
// ▼
type
type Type = {
readonly quantity: number;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string; }, never>.Type: {
readonly quantity: number;
}
Type
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 quantity: number;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}>
Product
)({}))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: 2 }
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

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

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
/*
throws:
ParseError: (Struct (Encoded side) <-> Struct (Type side))
└─ Encoded side transformation failure
└─ Struct (Encoded side)
└─ ["quantity"]
└─ NumberFromString
└─ Encoded side transformation failure
└─ Expected string, actual undefined
*/

In cases where you want null values to trigger the default behavior, you can use the nullable option. This ensures that if a field is set to null, it will be replaced by the default value.

Syntax

Schema.optionalWith(schema: Schema<A, I, R>, {
default: () => A,
nullable: true
})
OperationBehavior
DecodingApplies the default value if the input is missing or undefined or null
EncodingTransforms the input a: A back to i: I

Example (Applying Default When Field Is Missing or undefined or null)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
default: () => number;
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
default: () => number
default
: () => 1, // Default value for quantity
nullable: true
nullable
: true // Apply default if quantity is null
})
})
// ┌─── { readonly quantity?: string | null | undefined; }
// ▼
type
type Encoded = {
readonly quantity?: string | null | undefined;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>.Encoded: {
readonly quantity?: string | null | undefined;
}
Encoded
// ┌─── { readonly quantity: number; }
// ▼
type
type Type = {
readonly quantity: number;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>.Type: {
readonly quantity: number;
}
Type
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 quantity: number;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>
Product
)({}))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>
Product
)({
quantity: null
quantity
: null }))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
nullable: true;
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: 2 }

For a more strict approach, you can combine both exact and nullable options. This way, the default value is applied only when the field is null or missing, and not when it’s explicitly set to undefined.

Syntax

Schema.optionalWith(schema: Schema<A, I, R>, {
default: () => A,
exact: true,
nullable: true
})
OperationBehavior
DecodingApplies the default value if the input is missing or null
EncodingTransforms the input a: A back to i: I

Example (Applying Default Only When Field Is Missing or null)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
default: () => number;
exact: true;
nullable: true;
}) => Schema.optionalWith<...> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
default: () => number
default
: () => 1, // Default value for quantity
exact: true
exact
: true, // Only apply default if quantity is not provided
nullable: true
nullable
: true // Apply default if quantity is null
})
})
// ┌─── { readonly quantity?: string | null; }
// ▼
type
type Encoded = {
readonly quantity?: string | null;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>.Encoded: {
readonly quantity?: string | null;
}
Encoded
// ┌─── { readonly quantity: number; }
// ▼
type
type Type = {
readonly quantity: number;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>.Type: {
readonly quantity: number;
}
Type
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 quantity: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>
Product
)({}))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: null
quantity
: null }))
// Output: { quantity: 1 }
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 quantity: number;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity: number;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: 2 }
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

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

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
default: () => number;
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
/*
throws:
ParseError: (Struct (Encoded side) <-> Struct (Type side))
└─ Encoded side transformation failure
└─ Struct (Encoded side)
└─ ["quantity"]
└─ NumberFromString
└─ Encoded side transformation failure
└─ Expected string, actual undefined
*/

When working with optional fields, you may want to handle them as Option types. This approach allows you to explicitly manage the presence or absence of a field rather than relying on undefined or null.

You can configure a schema to treat optional fields as Option types, where missing or undefined values are converted to Option.none() and existing values are wrapped in Option.some().

Syntax

optionalWith(schema: Schema<A, I, R>, { as: "Option" })
InputOutput
<missing value>transforms to Option.none()
undefinedtransforms to Option.none()
i: Itransforms to Option.some(a: A)
InputOutput
Option.none()transforms to <missing value>
Option.some(a: A)transforms back to i: I

Example (Handling Optional Field as Option)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option" })
})
// ┌─── { readonly quantity?: string | undefined; }
// ▼
type
type Encoded = {
readonly quantity?: string | undefined;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }, never>.Encoded: {
readonly quantity?: string | undefined;
}
Encoded
// ┌─── { readonly quantity: Option<number>; }
// ▼
type
type Type = {
readonly quantity: Option<number>;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }, never>.Type: {
readonly quantity: Option<number>;
}
Type
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 quantity: Option<number>;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
)({}))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string | undefined;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option" })
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
}>
quantity
.
optionalWith<typeof NumberFromString, { as: "Option"; }>.from: typeof Schema.NumberFromString
from

The exact option ensures that the default behavior of the optional field applies only when the field is entirely missing, not when it is undefined.

Syntax

optionalWith(schema: Schema<A, I, R>, {
as: "Option",
exact: true
})
InputOutput
<missing value>transforms to Option.none()
undefinedParseError
i: Itransforms to Option.some(a: A)
InputOutput
Option.none()transforms to <missing value>
Option.some(a: A)transforms back to i: I

Example (Using Exactness with Optional Field as Option)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
exact: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option",
exact: true
exact
: true
})
})
// ┌─── { readonly quantity?: string; }
// ▼
type
type Encoded = {
readonly quantity?: string;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }, never>.Encoded: {
readonly quantity?: string;
}
Encoded
// ┌─── { readonly quantity: Option<number>; }
// ▼
type
type Type = {
readonly quantity: Option<number>;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }, never>.Type: {
readonly quantity: Option<number>;
}
Type
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 quantity: Option<number>;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
)({}))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

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

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
/*
throws:
ParseError: (Struct (Encoded side) <-> Struct (Type side))
└─ Encoded side transformation failure
└─ Struct (Encoded side)
└─ ["quantity"]
└─ NumberFromString
└─ Encoded side transformation failure
└─ Expected string, actual undefined
*/

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
exact: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option",
exact: true
exact
: true
})
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; exact: true; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
}>
quantity
.
optionalWith<typeof NumberFromString, { as: "Option"; exact: true; }>.from: typeof Schema.NumberFromString
from

The nullable option extends the default behavior to treat null as equivalent to Option.none(), alongside missing or undefined values.

Syntax

optionalWith(schema: Schema<A, I, R>, {
as: "Option",
nullable: true
})
InputOutput
<missing value>transforms to Option.none()
undefinedtransforms to Option.none()
nulltransforms to Option.none()
i: Itransforms to Option.some(a: A)
InputOutput
Option.none()transforms to <missing value>
Option.some(a: A)transforms back to i: I

Example (Handling Null as Missing Value with Optional Field as Option)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option",
nullable: true
nullable
: true
})
})
// ┌─── { readonly quantity?: string | null | undefined; }
// ▼
type
type Encoded = {
readonly quantity?: string | null | undefined;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>.Encoded: {
readonly quantity?: string | null | undefined;
}
Encoded
// ┌─── { readonly quantity: Option<number>; }
// ▼
type
type Type = {
readonly quantity: Option<number>;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>.Type: {
readonly quantity: Option<number>;
}
Type
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 quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
)({}))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
)({
quantity: null
quantity
: null }))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | null | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option",
nullable: true
nullable
: true
})
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; nullable: true; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
nullable: true;
}>
quantity
.
optionalWith<typeof NumberFromString, { as: "Option"; nullable: true; }>.from: typeof Schema.NumberFromString
from

When both exact and nullable options are used together, only null and missing fields are treated as Option.none(), while undefined is considered an invalid value.

Syntax

optionalWith(schema: Schema<A, I, R>, {
as: "Option",
exact: true,
nullable: true
})
InputOutput
<missing value>transforms to Option.none()
undefinedParseError
nulltransforms to Option.none()
i: Itransforms to Option.some(a: A)
InputOutput
Option.none()transforms to <missing value>
Option.some(a: A)transforms back to i: I

Example (Using Exactness and Handling Null as Missing Value with Optional Field as Option)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
exact: true;
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option",
exact: true
exact
: true,
nullable: true
nullable
: true
})
})
// ┌─── { readonly quantity?: string | null; }
// ▼
type
type Encoded = {
readonly quantity?: string | null;
}
Encoded
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>.Encoded: {
readonly quantity?: string | null;
}
Encoded
// ┌─── { readonly quantity: Option<number>; }
// ▼
type
type Type = {
readonly quantity: Option<number>;
}
Type
= typeof
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
.
Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>.Type: {
readonly quantity: Option<number>;
}
Type
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 quantity: Option<number>;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
)({}))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: null
quantity
: null }))
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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 quantity: Option<number>;
}, {
readonly quantity?: string | null;
}>(schema: Schema.Schema<{
readonly quantity: Option<number>;
}, {
readonly quantity?: string | null;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly quantity: Option<number>;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: string
quantity
: "2" }))
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

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

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
)({
quantity: undefined
quantity
:
var undefined
undefined
}))
/*
throws:
ParseError: (Struct (Encoded side) <-> Struct (Type side))
└─ Encoded side transformation failure
└─ Struct (Encoded side)
└─ ["quantity"]
└─ NumberFromString
└─ Encoded side transformation failure
└─ Expected string, actual undefined
*/

You can access the original schema type (before it was marked as optional) using the from property.

Example (Accessing the Original Schema)

import {
import Schema
Schema
} from "effect"
const
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
=
import Schema
Schema
.
function Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>(fields: {
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>(self: typeof Schema.NumberFromString, options: {
as: "Option";
exact: true;
nullable: true;
}) => Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}> (+1 overload)

@since3.10.0

optionalWith
(
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
, {
as: "Option"
as
: "Option",
exact: true
exact
: true,
nullable: true
nullable
: true
})
})
// ┌─── typeof Schema.NumberFromString
// ▼
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}>
Product
.
TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }, []>.fields: {
readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>;
}
fields
.
quantity: Schema.optionalWith<typeof Schema.NumberFromString, {
as: "Option";
exact: true;
nullable: true;
}>
quantity
.
optionalWith<typeof NumberFromString, { as: "Option"; exact: true; nullable: true; }>.from: typeof Schema.NumberFromString
from

The Schema.optionalToOptional API allows you to manage transformations from an optional field in the input to an optional field in the output. This can be useful for controlling both the output type and whether a field is present or absent based on specific criteria.

One common use case for optionalToOptional is handling fields where a specific input value, such as an empty string, should be treated as an absent field in the output.

Syntax

const optionalToOptional = <FA, FI, FR, TA, TI, TR>(
from: Schema<FA, FI, FR>,
to: Schema<TA, TI, TR>,
options: {
readonly decode: (o: Option.Option<FA>) => Option.Option<TI>,
readonly encode: (o: Option.Option<TI>) => Option.Option<FA>
}
): PropertySignature<"?:", TA, never, "?:", FI, false, FR | TR>

In this function:

  • The from parameter specifies the input schema, and to specifies the output schema.
  • The decode and encode functions define how the field should be interpreted on both sides:
    • Option.none() as an input argument indicates a missing field in the input.
    • Returning Option.none() from either function will omit the field in the output.

Example (Omitting Empty Strings from Output)

Consider an optional field of type string where empty strings in the input should be removed from the output.

import {
import Schema
Schema
} from "effect"
import {
const identity: <A>(a: A) => A

The identity function, i.e. A function that returns its input argument.

@parama - The input argument.

@example

import { identity } from "effect/Function"
assert.deepStrictEqual(identity(5), 5)

@since2.0.0

identity
,
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const schema: Schema.Struct<{
nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>;
}>
schema
=
import Schema
Schema
.
function Struct<{
nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>;
}>(fields: {
nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>
nonEmpty
:
import Schema
Schema
.
const optionalToOptional: <string, string, never, string, string, never>(from: Schema.Schema<string, string, never>, to: Schema.Schema<string, string, never>, options: {
readonly decode: (o: Option.Option<string>) => Option.Option<...>;
readonly encode: (o: Option.Option<...>) => Option.Option<...>;
}) => Schema.PropertySignature<...>

Converts an optional property to another optional property through a transformation Option -> Option.

  • decode:
    • none as argument means the value is missing in the input.
    • none as return value means the value will be missing in the output.
  • encode:
    • none as argument means the value is missing in the input.
    • none as return value means the value will be missing in the output.

@since3.10.0

optionalToOptional
(
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
import Schema
Schema
.
class String
export String

@since3.10.0

String
, {
decode: (o: Option.Option<string>) => Option.Option<string>
decode
: (
input: Option.Option<string>
input
) => {
if (
import Option

@since2.0.0

@since2.0.0

Option
.
const isNone: <string>(self: Option.Option<string>) => self is Option.None<string>

Determine if a Option is a None.

@paramself - The Option to check.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.isNone(Option.some(1)), false)
assert.deepStrictEqual(Option.isNone(Option.none()), true)

@since2.0.0

isNone
(
input: Option.Option<string>
input
)) {
// If the field is absent in the input,
// return Option.none() to omit it in the output
return
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <string>() => Option.Option<string>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
}
const
const value: string
value
=
input: Option.Some<string>
input
.
Some<string>.value: string
value
if (
const value: string
value
=== "") {
// Treat empty strings as missing in the output
// by returning Option.none()
return
import Option

@since2.0.0

@since2.0.0

Option
.
const none: <string>() => Option.Option<string>

Creates a new Option that represents the absence of a value.

@since2.0.0

none
()
}
// Non-empty strings are included in the output
return
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(
const value: string
value
)
},
// Here in the encoding part, we can decide to handle things
// in the same way as in the decoding phase or handle them differently.
// For example, we can leave everything unchanged
// and use the identity function
encode: (o: Option.Option<string>) => Option.Option<string>
encode
:
const identity: <A>(a: A) => A

The identity function, i.e. A function that returns its input argument.

@parama - The input argument.

@example

import { identity } from "effect/Function"
assert.deepStrictEqual(identity(5), 5)

@since2.0.0

identity
})
})
// Decoding examples
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
decode
=
import Schema
Schema
.
decodeUnknownSync<{
readonly nonEmpty?: string;
}, {
readonly nonEmpty?: string;
}>(schema: Schema.Schema<{
readonly nonEmpty?: string;
}, {
readonly nonEmpty?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>;
}>
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
decode
({}))
// 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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
decode
({
nonEmpty: string
nonEmpty
: "" }))
// 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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
decode
({
nonEmpty: string
nonEmpty
: "a non-empty string" }))
// Output: { nonEmpty: 'a non-empty string' }
// Encoding examples
const
const encode: (a: {
readonly nonEmpty?: string;
}, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
encode
=
import Schema
Schema
.
encodeSync<{
readonly nonEmpty?: string;
}, {
readonly nonEmpty?: string;
}>(schema: Schema.Schema<{
readonly nonEmpty?: string;
}, {
readonly nonEmpty?: string;
}, never>, options?: ParseOptions): (a: {
readonly nonEmpty?: string;
}, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
export encodeSync

@since3.10.0

encodeSync
(
const schema: Schema.Struct<{
nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>;
}>
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
(
const encode: (a: {
readonly nonEmpty?: string;
}, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
encode
({}))
// 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
(
const encode: (a: {
readonly nonEmpty?: string;
}, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
encode
({
nonEmpty?: string
nonEmpty
: "" }))
// Output: { nonEmpty: '' }
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 encode: (a: {
readonly nonEmpty?: string;
}, overrideOptions?: ParseOptions) => {
readonly nonEmpty?: string;
}
encode
({
nonEmpty?: string
nonEmpty
: "a non-empty string" }))
// Output: { nonEmpty: 'a non-empty string' }

The Schema.optionalToRequired API lets you transform an optional field into a required one, with custom logic to handle cases when the field is missing in the input.

Syntax

const optionalToRequired = <FA, FI, FR, TA, TI, TR>(
from: Schema<FA, FI, FR>,
to: Schema<TA, TI, TR>,
options: {
readonly decode: (o: Option.Option<FA>) => TI,
readonly encode: (ti: TI) => Option.Option<FA>
}
): PropertySignature<":", TA, never, "?:", FI, false, FR | TR>

In this function:

  • from specifies the input schema, while to specifies the output schema.
  • The decode and encode functions define the transformation behavior:
    • Passing Option.none() to decode means the field is absent in the input. The function can then return a default value for the output.
    • Returning Option.none() in encode will omit the field in the output.

Example (Setting a Default Value for a Missing Field)

In this example, if the input object lacks the maybe field, we will provide a default value for it in the output.

import {
import Schema
Schema
} from "effect"
import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const schema: Schema.Struct<{
maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>;
}>
schema
=
import Schema
Schema
.
function Struct<{
maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>;
}>(fields: {
maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>
maybe
:
import Schema
Schema
.
const optionalToRequired: <string, string, never, string, string, never>(from: Schema.Schema<string, string, never>, to: Schema.Schema<string, string, never>, options: {
readonly decode: (o: Option.Option<string>) => string;
readonly encode: (ti: string) => Option.Option<...>;
}) => Schema.PropertySignature<...>

Converts an optional property to a required one through a transformation Option -> Type.

  • decode: none as argument means the value is missing in the input.
  • encode: none as return value means the value will be missing in the output.

@since3.10.0

optionalToRequired
(
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
import Schema
Schema
.
class String
export String

@since3.10.0

String
, {
decode: (o: Option.Option<string>) => string
decode
: (
input: Option.Option<string>
input
) => {
if (
import Option

@since2.0.0

@since2.0.0

Option
.
const isNone: <string>(self: Option.Option<string>) => self is Option.None<string>

Determine if a Option is a None.

@paramself - The Option to check.

@example

import { Option } from "effect"
assert.deepStrictEqual(Option.isNone(Option.some(1)), false)
assert.deepStrictEqual(Option.isNone(Option.none()), true)

@since2.0.0

isNone
(
input: Option.Option<string>
input
)) {
// Assign a default value for the output
// when the field is absent in the input
return "default value"
}
// Use the input's value for the output if the field is present
return
input: Option.Some<string>
input
.
Some<string>.value: string
value
},
// During encoding, we can choose to handle things differently,
// or simply return the same value present in the input for the output
encode: (ti: string) => Option.Option<string>
encode
: (
a: string
a
) =>
import Option

@since2.0.0

@since2.0.0

Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new Option that wraps the given value.

@paramvalue - The value to wrap.

@since2.0.0

some
(
a: string
a
)
})
})
// Decoding examples
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly maybe: string;
}
decode
=
import Schema
Schema
.
decodeUnknownSync<{
readonly maybe: string;
}, {
readonly maybe?: string;
}>(schema: Schema.Schema<{
readonly maybe: string;
}, {
readonly maybe?: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly maybe: string;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>;
}>
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly maybe: string;
}
decode
({}))
// Output: { maybe: 'default value' }
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 decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly maybe: string;
}
decode
({
maybe: string
maybe
: "present value" }))
// Output: { maybe: 'present value' }
// Encoding examples
const
const encode: (a: {
readonly maybe: string;
}, overrideOptions?: ParseOptions) => {
readonly maybe?: string;
}
encode
=
import Schema
Schema
.
encodeSync<{
readonly maybe: string;
}, {
readonly maybe?: string;
}>(schema: Schema.Schema<{
readonly maybe: string;
}, {
readonly maybe?: string;
}, never>, options?: ParseOptions): (a: {
readonly maybe: string;
}, overrideOptions?: ParseOptions) => {
readonly maybe?: string;
}
export encodeSync

@since3.10.0

encodeSync
(
const schema: Schema.Struct<{
maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>;
}>
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
(
const encode: (a: {
readonly maybe: string;
}, overrideOptions?: ParseOptions) => {
readonly maybe?: string;
}
encode
({
maybe: string
maybe
: "present value" }))
// Output: { maybe: 'present value' }

The requiredToOptional API allows you to transform a required field into an optional one, applying custom logic to determine when the field can be omitted.

Syntax

const requiredToOptional = <FA, FI, FR, TA, TI, TR>(
from: Schema<FA, FI, FR>,
to: Schema<TA, TI, TR>,
options: {
readonly decode: (fa: FA) => Option.Option<TI>
readonly encode: (o: Option.Option<TI>) => FA
}
): PropertySignature<"?:", TA, never, ":", FI, false, FR | TR>

With decode and encode functions, you control the presence or absence of the field:

  • Option.none() as an argument in decode means the field is missing in the input.
  • Option.none() as a return value from encode means the field will be omitted in the output.

Example (Handling Empty String as Missing Value)

In this example, the name field is required but treated as optional if it is an empty string. During decoding, an empty string in name is considered absent, while encoding ensures a value (using an empty string as a default if name is absent).

import {
import Schema
Schema
} from "effect"
import {
import Option

@since2.0.0

@since2.0.0

Option
} from "effect"
const
const schema: Schema.Struct<{
name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>;
}>
schema
=
import Schema
Schema
.
function Struct<{
name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>;
}>(fields: {
name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>
name
:
import Schema
Schema
.
const requiredToOptional: <string, string, never, string, string, never>(from: Schema.Schema<string, string, never>, to: Schema.Schema<string, string, never>, options: {
readonly decode: (fa: string) => Option.Option<string>;
readonly encode: (o: Option.Option<...>) => string;
}) => Schema.PropertySignature<...>

Converts an optional property to a required one through a transformation Type -> Option.

  • decode: none as return value means the value will be missing in the output.
  • encode: none as argument means the value is missing in the input.

@since3.10.0

requiredToOptional
(
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
import Schema
Schema
.
class String
export String

@since3.10.0

String
, {
// Treat empty string as a missing value
decode: (fa: string) => Option.Option<string>
decode
:
import Option

@since2.0.0

@since2.0.0

Option
.
const liftPredicate: <string, string>(predicate: Predicate<string>) => (b: string) => Option.Option<string> (+3 overloads)

Transforms a Predicate function into a Some of the input value if the predicate returns true or None if the predicate returns false.

@parampredicate - A Predicate function that takes in a value of type A and returns a boolean.

@example

import { Option } from "effect"
const getOption = Option.liftPredicate((n: number) => n >= 0)
assert.deepStrictEqual(getOption(-1), Option.none())
assert.deepStrictEqual(getOption(1), Option.some(1))

@since2.0.0

liftPredicate
((
s: string
s
) =>
s: string
s
!== ""),
// Provide an empty string as default if the field is missing
encode: (o: Option.Option<string>) => string
encode
:
import Option

@since2.0.0

@since2.0.0

Option
.
const getOrElse: <string>(onNone: LazyArg<string>) => <A>(self: Option.Option<A>) => string | A (+1 overload)

Returns the value of the Option if it is Some, otherwise returns onNone

@paramself - The Option to get the value of.

@paramonNone - Function that returns the default value to return if the Option is None.

@example

import { pipe, Option } from "effect"
assert.deepStrictEqual(pipe(Option.some(1), Option.getOrElse(() => 0)), 1)
assert.deepStrictEqual(pipe(Option.none(), Option.getOrElse(() => 0)), 0)

@since2.0.0

getOrElse
(() => "")
})
})
// Decoding examples
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly name?: string;
}
decode
=
import Schema
Schema
.
decodeUnknownSync<{
readonly name?: string;
}, {
readonly name: string;
}>(schema: Schema.Schema<{
readonly name?: string;
}, {
readonly name: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly name?: string;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>;
}>
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly name?: string;
}
decode
({
name: string
name
: "John" }))
// Output: { name: 'John' }
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly name?: string;
}
decode
({
name: string
name
: "" }))
// Output: {}
// Encoding examples
const
const encode: (a: {
readonly name?: string;
}, overrideOptions?: ParseOptions) => {
readonly name: string;
}
encode
=
import Schema
Schema
.
encodeSync<{
readonly name?: string;
}, {
readonly name: string;
}>(schema: Schema.Schema<{
readonly name?: string;
}, {
readonly name: string;
}, never>, options?: ParseOptions): (a: {
readonly name?: string;
}, overrideOptions?: ParseOptions) => {
readonly name: string;
}
export encodeSync

@since3.10.0

encodeSync
(
const schema: Schema.Struct<{
name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>;
}>
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
(
const encode: (a: {
readonly name?: string;
}, overrideOptions?: ParseOptions) => {
readonly name: string;
}
encode
({
name?: string
name
: "John" }))
// Output: { name: 'John' }
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const encode: (a: {
readonly name?: string;
}, overrideOptions?: ParseOptions) => {
readonly name: string;
}
encode
({}))
// Output: { name: '' }

Schemas in effect can be extended in multiple ways, allowing you to combine or enhance existing types with additional fields or functionality. One common method is to use the fields property available in Struct schemas. This property provides a convenient way to add fields or merge fields from different structs while retaining the original Struct type. This approach also makes it easier to access and modify fields.

For more complex cases, such as extending a struct with a union, you may want to use the Schema.extend function, which offers flexibility in scenarios where direct field spreading may not be sufficient.

Structs provide access to their fields through the fields property, which allows you to extend an existing struct by adding additional fields or combining fields from multiple structs.

Example (Adding New Fields)

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

Struct
({
...
const Orginal: Schema.Struct<{
a: typeof Schema.String;
b: typeof Schema.String;
}>
Orginal
.
TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: {
readonly a: typeof Schema.String;
readonly b: typeof Schema.String;
}
fields
,
// Adding new fields
c: typeof Schema.String
c
:
import Schema
Schema
.
class String
export String

@since3.10.0

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

@since3.10.0

String
})
// ┌─── {
// | readonly a: string;
// | readonly b: string;
// | readonly c: string;
// | readonly d: string;
// | }
// ▼
type
type Type = {
readonly a: string;
readonly b: string;
readonly c: string;
readonly d: string;
}
Type
= typeof
const Extended: Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.String;
a: typeof Schema.String;
b: typeof Schema.String;
}>
Extended
.
Schema<{ readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, never>.Type: {
readonly a: string;
readonly b: string;
readonly c: string;
readonly d: string;
}
Type

Example (Adding Additional Index Signatures)

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

String
})
const
const Extended: Schema.TypeLiteral<{
readonly a: typeof Schema.String;
readonly b: typeof Schema.String;
}, readonly [Schema.Record$<typeof Schema.String, typeof Schema.String>]>
Extended
=
import Schema
Schema
.
function Struct<{
readonly a: typeof Schema.String;
readonly b: typeof Schema.String;
}, readonly [Schema.Record$<typeof Schema.String, typeof Schema.String>]>(fields: {
readonly a: typeof Schema.String;
readonly b: typeof Schema.String;
}, records_0: Schema.Record$<...>): Schema.TypeLiteral<...> (+1 overload)

@since3.10.0

Struct
(
const Original: Schema.Struct<{
a: typeof Schema.String;
b: typeof Schema.String;
}>
Original
.
TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: {
readonly a: typeof Schema.String;
readonly b: typeof Schema.String;
}
fields
,
// Adding an index signature
import Schema
Schema
.
const Record: <typeof Schema.String, typeof Schema.String>(options: {
readonly key: typeof Schema.String;
readonly value: typeof Schema.String;
}) => Schema.Record$<typeof Schema.String, typeof Schema.String>

@since3.10.0

Record
({
key: typeof Schema.String
key
:
import Schema
Schema
.
class String
export String

@since3.10.0

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

@since3.10.0

String
})
)
// ┌─── {
// │ readonly [x: string]: string;
// | readonly a: string;
// | readonly b: string;
// | }
// ▼
type
type Type = {
readonly [x: string]: string;
readonly a: string;
readonly b: string;
}
Type
= typeof
const Extended: Schema.TypeLiteral<{
readonly a: typeof Schema.String;
readonly b: typeof Schema.String;
}, readonly [Schema.Record$<typeof Schema.String, typeof Schema.String>]>
Extended
.
Schema<{ readonly [x: string]: string; readonly a: string; readonly b: string; }, { readonly [x: string]: string; readonly a: string; readonly b: string; }, never>.Type: {
readonly [x: string]: string;
readonly a: string;
readonly b: string;
}
Type

Example (Combining Fields from Multiple Structs)

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

String
})
const
const Struct2: Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.String;
}>
Struct2
=
import Schema
Schema
.
function Struct<{
c: typeof Schema.String;
d: typeof Schema.String;
}>(fields: {
c: typeof Schema.String;
d: typeof Schema.String;
}): Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.String;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

Struct
({
...
const Struct1: Schema.Struct<{
a: typeof Schema.String;
b: typeof Schema.String;
}>
Struct1
.
TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: {
readonly a: typeof Schema.String;
readonly b: typeof Schema.String;
}
fields
,
...
const Struct2: Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.String;
}>
Struct2
.
TypeLiteral<{ c: typeof String$; d: typeof String$; }, []>.fields: {
readonly c: typeof Schema.String;
readonly d: typeof Schema.String;
}
fields
})
// ┌─── {
// | readonly a: string;
// | readonly b: string;
// | readonly c: string;
// | readonly d: string;
// | }
// ▼
type
type Type = {
readonly a: string;
readonly b: string;
readonly c: string;
readonly d: string;
}
Type
= typeof
const Extended: Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.String;
a: typeof Schema.String;
b: typeof Schema.String;
}>
Extended
.
Schema<{ readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, never>.Type: {
readonly a: string;
readonly b: string;
readonly c: string;
readonly d: string;
}
Type

The Schema.extend function provides a structured method to expand schemas, especially useful when direct field spreading isn’t sufficient—such as when you need to extend a struct with a union of other structs.

Supported extensions include:

  • Schema.String with another Schema.String refinement or a string literal
  • Schema.Number with another Schema.Number refinement or a number literal
  • Schema.Boolean with another Schema.Boolean refinement or a boolean literal
  • A struct with another struct where overlapping fields support extension
  • A struct with in index signature
  • A struct with a union of supported schemas
  • A refinement of a struct with a supported schema
  • A suspend of a struct with a supported schema

Example (Extending a Struct with a Union of Structs)

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

@since3.10.0

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

@since3.10.0

String
})
const
const UnionOfStructs: Schema.Union<[Schema.Struct<{
b: typeof Schema.String;
}>, Schema.Struct<{
c: typeof Schema.String;
}>]>
UnionOfStructs
=
import Schema
Schema
.
function Union<[Schema.Struct<{
b: typeof Schema.String;
}>, Schema.Struct<{
c: typeof Schema.String;
}>]>(members_0: Schema.Struct<{
b: typeof Schema.String;
}>, members_1: Schema.Struct<...>): Schema.Union<...> (+3 overloads)

@since3.10.0

Union
(
import Schema
Schema
.
function Struct<{
b: typeof Schema.String;
}>(fields: {
b: typeof Schema.String;
}): Schema.Struct<{
b: 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
}),
import Schema
Schema
.
function Struct<{
c: typeof Schema.String;
}>(fields: {
c: typeof Schema.String;
}): Schema.Struct<{
c: typeof Schema.String;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

String
})
)
const
const Extended: Schema.extend<Schema.Struct<{
a: typeof Schema.String;
}>, Schema.Union<[Schema.Struct<{
b: typeof Schema.String;
}>, Schema.Struct<{
c: typeof Schema.String;
}>]>>
Extended
=
import Schema
Schema
.
const extend: <Schema.Struct<{
a: typeof Schema.String;
}>, Schema.Union<[Schema.Struct<{
b: typeof Schema.String;
}>, Schema.Struct<{
c: typeof Schema.String;
}>]>>(self: Schema.Struct<...>, that: Schema.Union<...>) => Schema.extend<...> (+1 overload)

Extends a schema with another schema.

Not all extensions are supported, and their support depends on the nature of the involved schemas.

Possible extensions include:

  • Schema.String with another Schema.String refinement or a string literal
  • Schema.Number with another Schema.Number refinement or a number literal
  • Schema.Boolean with another Schema.Boolean refinement or a boolean literal
  • A struct with another struct where overlapping fields support extension
  • A struct with in index signature
  • A struct with a union of supported schemas
  • A refinement of a struct with a supported schema
  • A suspend of a struct with a supported schema

@example

import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.String
})
// const extended: Schema<
// {
// readonly a: string
// readonly b: string
// } & {
// readonly c: string
// } & {
// readonly [x: string]: string
// }
// >
const extended = Schema.asSchema(schema.pipe(
Schema.extend(Schema.Struct({ c: Schema.String })), // <= you can add more fields
Schema.extend(Schema.Record({ key: Schema.String, value: Schema.String })) // <= you can add index signatures
))

@since3.10.0

extend
(
const Struct: Schema.Struct<{
a: typeof Schema.String;
}>
Struct
,
const UnionOfStructs: Schema.Union<[Schema.Struct<{
b: typeof Schema.String;
}>, Schema.Struct<{
c: typeof Schema.String;
}>]>
UnionOfStructs
)
// ┌─── {
// | readonly a: string;
// | } & ({
// | readonly b: string;
// | } | {
// | readonly c: string;
// | })
// ▼
type
type Type = {
readonly a: string;
} & ({
readonly b: string;
} | {
readonly c: string;
})
Type
= typeof
const Extended: Schema.extend<Schema.Struct<{
a: typeof Schema.String;
}>, Schema.Union<[Schema.Struct<{
b: typeof Schema.String;
}>, Schema.Struct<{
c: typeof Schema.String;
}>]>>
Extended
.
Schema<{ readonly a: string; } & ({ readonly b: string; } | { readonly c: string; }), { readonly a: string; } & ({ readonly b: string; } | { readonly c: string; }), never>.Type: {
readonly a: string;
} & ({
readonly b: string;
} | {
readonly c: string;
})
Type

Example (Attempting to Extend Structs with Conflicting Fields)

This example demonstrates an attempt to extend a struct with another struct that contains overlapping field names, resulting in an error due to conflicting types.

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

@since3.10.0

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

@since3.10.0

String
})
const
const OverlappingUnion: Schema.Union<[Schema.Struct<{
a: typeof Schema.Number;
}>, Schema.Struct<{
d: typeof Schema.String;
}>]>
OverlappingUnion
=
import Schema
Schema
.
function Union<[Schema.Struct<{
a: typeof Schema.Number;
}>, Schema.Struct<{
d: typeof Schema.String;
}>]>(members_0: Schema.Struct<{
a: typeof Schema.Number;
}>, members_1: Schema.Struct<...>): Schema.Union<...> (+3 overloads)

@since3.10.0

Union
(
import Schema
Schema
.
function Struct<{
a: typeof Schema.Number;
}>(fields: {
a: typeof Schema.Number;
}): Schema.Struct<{
a: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

Number
}), // conflicting type for key "a"
import Schema
Schema
.
function Struct<{
d: typeof Schema.String;
}>(fields: {
d: typeof Schema.String;
}): Schema.Struct<{
d: typeof Schema.String;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

String
})
)
const
const Extended: Schema.extend<Schema.Struct<{
a: typeof Schema.String;
}>, Schema.Union<[Schema.Struct<{
a: typeof Schema.Number;
}>, Schema.Struct<{
d: typeof Schema.String;
}>]>>
Extended
=
import Schema
Schema
.
const extend: <Schema.Struct<{
a: typeof Schema.String;
}>, Schema.Union<[Schema.Struct<{
a: typeof Schema.Number;
}>, Schema.Struct<{
d: typeof Schema.String;
}>]>>(self: Schema.Struct<...>, that: Schema.Union<...>) => Schema.extend<...> (+1 overload)

Extends a schema with another schema.

Not all extensions are supported, and their support depends on the nature of the involved schemas.

Possible extensions include:

  • Schema.String with another Schema.String refinement or a string literal
  • Schema.Number with another Schema.Number refinement or a number literal
  • Schema.Boolean with another Schema.Boolean refinement or a boolean literal
  • A struct with another struct where overlapping fields support extension
  • A struct with in index signature
  • A struct with a union of supported schemas
  • A refinement of a struct with a supported schema
  • A suspend of a struct with a supported schema

@example

import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.String
})
// const extended: Schema<
// {
// readonly a: string
// readonly b: string
// } & {
// readonly c: string
// } & {
// readonly [x: string]: string
// }
// >
const extended = Schema.asSchema(schema.pipe(
Schema.extend(Schema.Struct({ c: Schema.String })), // <= you can add more fields
Schema.extend(Schema.Record({ key: Schema.String, value: Schema.String })) // <= you can add index signatures
))

@since3.10.0

extend
(
const Struct: Schema.Struct<{
a: typeof Schema.String;
}>
Struct
,
const OverlappingUnion: Schema.Union<[Schema.Struct<{
a: typeof Schema.Number;
}>, Schema.Struct<{
d: typeof Schema.String;
}>]>
OverlappingUnion
)
/*
throws:
Error: Unsupported schema or overlapping types
at path: ["a"]
details: cannot extend string with number
*/

Example (xtending a Refinement with Another Refinement)

In this example, we extend two refinements, Integer and Positive, creating a schema that enforces both integer and positivity constraints.

import {
import Schema
Schema
} from "effect"
const
const Integer: Schema.brand<typeof Schema.Int, "Int">
Integer
=
import Schema
Schema
.
class Int

@since3.10.0

Int
.
Pipeable.pipe<typeof Schema.Int, Schema.brand<typeof Schema.Int, "Int">>(this: typeof Schema.Int, ab: (_: typeof Schema.Int) => Schema.brand<typeof Schema.Int, "Int">): Schema.brand<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const brand: <typeof Schema.Int, "Int">(brand: "Int", annotations?: Schema.Annotations.Schema<number & Brand<"Int">, readonly []> | undefined) => (self: typeof Schema.Int) => Schema.brand<typeof Schema.Int, "Int">

Returns a nominal branded schema by applying a brand to a given schema.

Schema<A> + B -> Schema<A & Brand<B>>

@paramself - The input schema to be combined with the brand.

@parambrand - The brand to apply.

@example

import * as Schema from "effect/Schema"
const Int = Schema.Number.pipe(Schema.int(), Schema.brand("Int"))
type Int = Schema.Schema.Type<typeof Int> // number & Brand<"Int">

@since3.10.0

brand
("Int"))
const
const Positive: Schema.brand<typeof Schema.Positive, "Positive">
Positive
=
import Schema
Schema
.
class Positive

@since3.10.0

Positive
.
Pipeable.pipe<typeof Schema.Positive, Schema.brand<typeof Schema.Positive, "Positive">>(this: typeof Schema.Positive, ab: (_: typeof Schema.Positive) => Schema.brand<typeof Schema.Positive, "Positive">): Schema.brand<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const brand: <typeof Schema.Positive, "Positive">(brand: "Positive", annotations?: Schema.Annotations.Schema<number & Brand<"Positive">, readonly []> | undefined) => (self: typeof Schema.Positive) => Schema.brand<...>

Returns a nominal branded schema by applying a brand to a given schema.

Schema<A> + B -> Schema<A & Brand<B>>

@paramself - The input schema to be combined with the brand.

@parambrand - The brand to apply.

@example

import * as Schema from "effect/Schema"
const Int = Schema.Number.pipe(Schema.int(), Schema.brand("Int"))
type Int = Schema.Schema.Type<typeof Int> // number & Brand<"Int">

@since3.10.0

brand
("Positive"))
// ┌─── Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>
// ▼
const
const PositiveInteger: Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>
PositiveInteger
=
import Schema
Schema
.
const asSchema: <Schema.extend<Schema.brand<typeof Schema.Positive, "Positive">, Schema.brand<typeof Schema.Int, "Int">>>(schema: Schema.extend<Schema.brand<typeof Schema.Positive, "Positive">, Schema.brand<...>>) => Schema.Schema<...>

@since3.10.0

asSchema
(
import Schema
Schema
.
const extend: <Schema.brand<typeof Schema.Positive, "Positive">, Schema.brand<typeof Schema.Int, "Int">>(self: Schema.brand<typeof Schema.Positive, "Positive">, that: Schema.brand<...>) => Schema.extend<...> (+1 overload)

Extends a schema with another schema.

Not all extensions are supported, and their support depends on the nature of the involved schemas.

Possible extensions include:

  • Schema.String with another Schema.String refinement or a string literal
  • Schema.Number with another Schema.Number refinement or a number literal
  • Schema.Boolean with another Schema.Boolean refinement or a boolean literal
  • A struct with another struct where overlapping fields support extension
  • A struct with in index signature
  • A struct with a union of supported schemas
  • A refinement of a struct with a supported schema
  • A suspend of a struct with a supported schema

@example

import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.String
})
// const extended: Schema<
// {
// readonly a: string
// readonly b: string
// } & {
// readonly c: string
// } & {
// readonly [x: string]: string
// }
// >
const extended = Schema.asSchema(schema.pipe(
Schema.extend(Schema.Struct({ c: Schema.String })), // <= you can add more fields
Schema.extend(Schema.Record({ key: Schema.String, value: Schema.String })) // <= you can add index signatures
))

@since3.10.0

extend
(
const Positive: Schema.brand<typeof Schema.Positive, "Positive">
Positive
,
const Integer: Schema.brand<typeof Schema.Int, "Int">
Integer
))
import Schema
Schema
.
decodeUnknownSync<number & Brand<"Positive"> & Brand<"Int">, number>(schema: Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => number & ... 1 more ... & Brand<...>
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const PositiveInteger: Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>
PositiveInteger
)(-1)
/*
throws
ParseError: Int & Brand<"Int">
└─ From side refinement failure
└─ Positive & Brand<"Positive">
└─ Predicate refinement failure
└─ Expected Positive & Brand<"Positive">, actual -1
*/
import Schema
Schema
.
decodeUnknownSync<number & Brand<"Positive"> & Brand<"Int">, number>(schema: Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => number & ... 1 more ... & Brand<...>
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const PositiveInteger: Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>
PositiveInteger
)(1.1)
/*
throws
ParseError: Int & Brand<"Int">
└─ Predicate refinement failure
└─ Expected Int & Brand<"Int">, actual 1.1
*/

To rename a property directly during schema creation, you can utilize the Schema.fromKey function.

Example (Renaming a Required Property)

import {
import Schema
Schema
} from "effect"
const
const schema: Schema.Struct<{
a: Schema.PropertySignature<":", string, "c", ":", string, false, never>;
b: typeof Schema.Number;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: Schema.PropertySignature<":", string, "c", ":", string, false, never>;
b: typeof Schema.Number;
}>(fields: {
a: Schema.PropertySignature<":", string, "c", ":", string, false, never>;
b: typeof Schema.Number;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
a: Schema.PropertySignature<":", string, "c", ":", string, false, never>
a
:
import Schema
Schema
.
const propertySignature: <typeof Schema.String>(self: typeof Schema.String) => Schema.propertySignature<typeof Schema.String>

Lifts a Schema into a PropertySignature.

@since3.10.0

propertySignature
(
import Schema
Schema
.
class String
export String

@since3.10.0

String
).
Pipeable.pipe<Schema.propertySignature<typeof Schema.String>, Schema.PropertySignature<":", string, "c", ":", string, false, never>>(this: Schema.propertySignature<...>, ab: (_: Schema.propertySignature<typeof Schema.String>) => Schema.PropertySignature<...>): Schema.PropertySignature<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const fromKey: <"c">(key: "c") => <TypeToken, Type, EncodedToken, Encoded, HasDefault, R>(self: Schema.PropertySignature<TypeToken, Type, PropertyKey, EncodedToken, Encoded, HasDefault, R>) => Schema.PropertySignature<...> (+1 overload)

Enhances a property signature by specifying a different key for it in the Encoded type.

@since3.10.0

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

@since3.10.0

Number
})
// ┌─── { readonly c: string; readonly b: number; }
// ▼
type
type Encoded = {
readonly c: string;
readonly b: number;
}
Encoded
= typeof
const schema: Schema.Struct<{
a: Schema.PropertySignature<":", string, "c", ":", string, false, never>;
b: typeof Schema.Number;
}>
schema
.
Schema<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>.Encoded: {
readonly c: string;
readonly b: number;
}
Encoded
// ┌─── { readonly a: string; readonly b: number; }
// ▼
type
type Type = {
readonly a: string;
readonly b: number;
}
Type
= typeof
const schema: Schema.Struct<{
a: Schema.PropertySignature<":", string, "c", ":", string, false, never>;
b: typeof Schema.Number;
}>
schema
.
Schema<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>.Type: {
readonly a: string;
readonly b: number;
}
Type
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: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}>(schema: Schema.Schema<{
readonly a: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: string;
readonly b: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
a: Schema.PropertySignature<":", string, "c", ":", string, false, never>;
b: typeof Schema.Number;
}>
schema
)({
c: string
c
: "c",
b: number
b
: 1 }))
// Output: { a: "c", b: 1 }

Example (Renaming an Optional Property)

import {
import Schema
Schema
} from "effect"
const
const schema: Schema.Struct<{
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>;
b: typeof Schema.Number;
}>
schema
=
import Schema
Schema
.
function Struct<{
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>;
b: typeof Schema.Number;
}>(fields: {
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>;
b: typeof Schema.Number;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>
a
:
import Schema
Schema
.
const optional: <typeof Schema.String>(self: typeof Schema.String) => Schema.optional<typeof Schema.String>

@since3.10.0

optional
(
import Schema
Schema
.
class String
export String

@since3.10.0

String
).
Pipeable.pipe<Schema.optional<typeof Schema.String>, Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>>(this: Schema.optional<...>, ab: (_: Schema.optional<typeof Schema.String>) => Schema.PropertySignature<...>): Schema.PropertySignature<...> (+21 overloads)
pipe
(
import Schema
Schema
.
const fromKey: <"c">(key: "c") => <TypeToken, Type, EncodedToken, Encoded, HasDefault, R>(self: Schema.PropertySignature<TypeToken, Type, PropertyKey, EncodedToken, Encoded, HasDefault, R>) => Schema.PropertySignature<...> (+1 overload)

Enhances a property signature by specifying a different key for it in the Encoded type.

@since3.10.0

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

@since3.10.0

Number
})
// ┌─── { readonly b: number; readonly c?: string | undefined; }
// ▼
type
type Encoded = {
readonly b: number;
readonly c?: string | undefined;
}
Encoded
= typeof
const schema: Schema.Struct<{
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>;
b: typeof Schema.Number;
}>
schema
.
Schema<{ readonly a?: string | undefined; readonly b: number; }, { readonly b: number; readonly c?: string | undefined; }, never>.Encoded: {
readonly b: number;
readonly c?: string | undefined;
}
Encoded
// ┌─── { readonly a?: string | undefined; readonly b: number; }
// ▼
type
type Type = {
readonly a?: string | undefined;
readonly b: number;
}
Type
= typeof
const schema: Schema.Struct<{
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>;
b: typeof Schema.Number;
}>
schema
.
Schema<{ readonly a?: string | undefined; readonly b: number; }, { readonly b: number; readonly c?: string | undefined; }, never>.Type: {
readonly a?: string | undefined;
readonly b: number;
}
Type
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?: string | undefined;
readonly b: number;
}, {
readonly b: number;
readonly c?: string | undefined;
}>(schema: Schema.Schema<{
readonly a?: string | undefined;
readonly b: number;
}, {
readonly b: number;
readonly c?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a?: string | undefined;
readonly b: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>;
b: typeof Schema.Number;
}>
schema
)({
c: string
c
: "c",
b: number
b
: 1 }))
// Output: { a: 'c', b: 1 }
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?: string | undefined;
readonly b: number;
}, {
readonly b: number;
readonly c?: string | undefined;
}>(schema: Schema.Schema<{
readonly a?: string | undefined;
readonly b: number;
}, {
readonly b: number;
readonly c?: string | undefined;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a?: string | undefined;
readonly b: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const schema: Schema.Struct<{
a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>;
b: typeof Schema.Number;
}>
schema
)({
b: number
b
: 1 }))
// Output: { b: 1 }

Using Schema.optional automatically returns a PropertySignature, making it unnecessary to explicitly use Schema.propertySignature as required for renaming required fields in the previous example.

For existing schemas, the Schema.rename API offers a way to systematically change property names across a schema, even within complex structures like unions, though in case of structs you lose the original field types.

Example (Renaming Properties in a Struct Schema)

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

Number
})
// Renaming the "c" property to "a"
//
//
// ┌─── SchemaClass<{
// | readonly a: string;
// | readonly b: number;
// | }>
// ▼
const
const Renamed: Schema.SchemaClass<{
readonly a: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}, never>
Renamed
=
import Schema
Schema
.
const rename: <{
readonly c: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}, never, {
readonly c: "a";
}>(self: Schema.Schema<{
readonly c: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}, never>, mapping: {
readonly c: "a";
}) => Schema.SchemaClass<...> (+1 overload)

@since3.10.0

rename
(
const Original: Schema.Struct<{
c: typeof Schema.String;
b: typeof Schema.Number;
}>
Original
, {
c: "a"
c
: "a" })
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: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}>(schema: Schema.Schema<{
readonly a: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: string;
readonly b: number;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Renamed: Schema.SchemaClass<{
readonly a: string;
readonly b: number;
}, {
readonly c: string;
readonly b: number;
}, never>
Renamed
)({
c: string
c
: "c",
b: number
b
: 1 }))
// Output: { a: "c", b: 1 }

Example (Renaming Properties in Union Schemas)

import {
import Schema
Schema
} from "effect"
const
const Original: Schema.Union<[Schema.Struct<{
c: typeof Schema.String;
b: typeof Schema.Number;
}>, Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.Boolean;
}>]>
Original
=
import Schema
Schema
.
function Union<[Schema.Struct<{
c: typeof Schema.String;
b: typeof Schema.Number;
}>, Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.Boolean;
}>]>(members_0: Schema.Struct<{
c: typeof Schema.String;
b: typeof Schema.Number;
}>, members_1: Schema.Struct<...>): Schema.Union<...> (+3 overloads)

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

Number
}),
import Schema
Schema
.
function Struct<{
c: typeof Schema.String;
d: typeof Schema.Boolean;
}>(fields: {
c: typeof Schema.String;
d: typeof Schema.Boolean;
}): Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.Boolean;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

String
,
d: typeof Schema.Boolean
d
:
import Schema
Schema
.
class Boolean
export Boolean

@since3.10.0

Boolean
})
)
// Renaming the "c" property to "a" for all members
//
// ┌─── SchemaClass<{
// | readonly a: string;
// | readonly b: number;
// | } | {
// | readonly a: string;
// | readonly d: number;
// | }>
// ▼
const
const Renamed: Schema.SchemaClass<{
readonly a: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}, never>
Renamed
=
import Schema
Schema
.
const rename: <{
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}, never, {
...;
}>(self: Schema.Schema<...>, mapping: {
...;
}) => Schema.SchemaClass<...> (+1 overload)

@since3.10.0

rename
(
const Original: Schema.Union<[Schema.Struct<{
c: typeof Schema.String;
b: typeof Schema.Number;
}>, Schema.Struct<{
c: typeof Schema.String;
d: typeof Schema.Boolean;
}>]>
Original
, {
c: "a"
c
: "a" })
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: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}>(schema: Schema.Schema<{
readonly a: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Renamed: Schema.SchemaClass<{
readonly a: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}, never>
Renamed
)({
c: string
c
: "c",
b: number
b
: 1 }))
// Output: { a: "c", b: 1 }
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: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}>(schema: Schema.Schema<{
readonly a: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly a: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Renamed: Schema.SchemaClass<{
readonly a: string;
readonly b: number;
} | {
readonly a: string;
readonly d: boolean;
}, {
readonly c: string;
readonly b: number;
} | {
readonly c: string;
readonly d: boolean;
}, never>
Renamed
)({
c: string
c
: "c",
d: boolean
d
: false }))
// Output: { a: 'c', d: false }

The Schema.suspend function is designed for defining schemas that reference themselves, such as in recursive data structures.

Example (Self-Referencing Schema)

In this example, the Category schema references itself through the subcategories field, which is an array of Category objects.

import {
import Schema
Schema
} from "effect"
interface
interface Category
Category
{
readonly
Category.name: string
name
: string
readonly
Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
}
const
const Category: Schema.Struct<{
name: typeof Schema.String;
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
}>
Category
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
}>(fields: {
name: typeof Schema.String;
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

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

@since3.10.0

String
,
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>
subcategories
:
import Schema
Schema
.
Array<Schema.suspend<Category, Category, never>>(value: Schema.suspend<Category, Category, never>): Schema.Array$<Schema.suspend<Category, Category, never>>
export Array

@since3.10.0

Array
(
import Schema
Schema
.
const suspend: <Category, Category, never>(f: () => Schema.Schema<Category, Category, never>) => Schema.suspend<Category, Category, never>

@since3.10.0

suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface Category
Category
> =>
const Category: Schema.Struct<{
name: typeof Schema.String;
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
}>
Category
)
)
})

Example (Type Inference Error)

import {
import Schema
Schema
} from "effect"
// @ts-expect-error
const
const Category: Schema.Struct<{
name: typeof Schema.String;
subcategories: Schema.Array$<Schema.suspend<unknown, unknown, unknown>>;
}>
Category
=
import Schema
Schema
.
function Struct<{
name: typeof Schema.String;
subcategories: Schema.Array$<Schema.suspend<unknown, unknown, unknown>>;
}>(fields: {
name: typeof Schema.String;
subcategories: Schema.Array$<Schema.suspend<unknown, unknown, unknown>>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

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

@since3.10.0

String
,
// @ts-expect-error
subcategories: Schema.Array$<Schema.suspend<unknown, unknown, unknown>>
subcategories
:
import Schema
Schema
.
Array<Schema.suspend<unknown, unknown, unknown>>(value: Schema.suspend<unknown, unknown, unknown>): Schema.Array$<Schema.suspend<unknown, unknown, unknown>>
export Array

@since3.10.0

Array
(
import Schema
Schema
.
const suspend: <unknown, unknown, unknown>(f: () => Schema.Schema<unknown, unknown, unknown>) => Schema.suspend<unknown, unknown, unknown>

@since3.10.0

suspend
(() =>
const Category: any
Category
))
})
/*
'Category' implicitly has type 'any' because it does not have a type annotation and is
referenced directly or indirectly in its own initializer.ts(7022)
*/

As we’ve observed, it’s necessary to define an interface for the Type of the schema to enable recursive schema definition, which can complicate things and be quite tedious. One pattern to mitigate this is to separate the field responsible for recursion from all other fields.

Example (Separating Recursive Fields)

import {
import Schema
Schema
} from "effect"
const
const fields: {
name: typeof Schema.String;
}
fields
= {
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
// ...other fields as needed
}
// Define an interface for the Category schema,
// extending the Type of the defined fields
interface
interface Category
Category
extends
import Schema
Schema
.
namespace Struct

@since3.10.0

@since3.10.0

@since3.10.0

Struct
.
type Struct<Fields extends Struct.Fields>.Type<F extends Schema.Struct.Fields> = UnionToIntersection<{ [K in keyof F]: F[K] extends Schema.Struct.OptionalPropertySignature ? { readonly [H in K]?: Schema.Schema.Type<F[H]>; } : { readonly [h in K]: Schema.Schema.Type<F[h]>; }; }[keyof F]> extends infer Q ? Q : never

@since3.10.0

Type
<typeof
const fields: {
name: typeof Schema.String;
}
fields
> {
// Define `subcategories` using recursion
readonly
Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
}
const
const Category: Schema.Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
name: typeof Schema.String;
}>
Category
=
import Schema
Schema
.
function Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
name: typeof Schema.String;
}>(fields: {
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
name: typeof Schema.String;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
...
const fields: {
name: typeof Schema.String;
}
fields
, // Spread in the base fields
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>
subcategories
:
import Schema
Schema
.
Array<Schema.suspend<Category, Category, never>>(value: Schema.suspend<Category, Category, never>): Schema.Array$<Schema.suspend<Category, Category, never>>
export Array

@since3.10.0

Array
(
// Define `subcategories` using recursion
import Schema
Schema
.
const suspend: <Category, Category, never>(f: () => Schema.Schema<Category, Category, never>) => Schema.suspend<Category, Category, never>

@since3.10.0

suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface Category
Category
> =>
const Category: Schema.Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
name: typeof Schema.String;
}>
Category
)
)
})

You can also use Schema.suspend to create mutually recursive schemas, where two schemas reference each other. In the following example, Expression and Operation form a simple arithmetic expression tree by referencing each other.

Example (Defining Mutually Recursive Schemas)

import {
import Schema
Schema
} from "effect"
interface
interface Expression
Expression
{
readonly
Expression.type: "expression"
type
: "expression"
readonly
Expression.value: number | Operation
value
: number |
interface Operation
Operation
}
interface
interface Operation
Operation
{
readonly
Operation.type: "operation"
type
: "operation"
readonly
Operation.operator: "+" | "-"
operator
: "+" | "-"
readonly
Operation.left: Expression
left
:
interface Expression
Expression
readonly
Operation.right: Expression
right
:
interface Expression
Expression
}
const
const Expression: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>;
}>
Expression
=
import Schema
Schema
.
function Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>;
}>(fields: {
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
type: Schema.Literal<["expression"]>
type
:
import Schema
Schema
.
function Literal<["expression"]>(literals_0: "expression"): Schema.Literal<["expression"]> (+2 overloads)

@since3.10.0

Literal
("expression"),
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>
value
:
import Schema
Schema
.
function Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>(members_0: typeof Schema.Number, members_1: Schema.suspend<Operation, Operation, never>): Schema.Union<...> (+3 overloads)

@since3.10.0

Union
(
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
,
import Schema
Schema
.
const suspend: <Operation, Operation, never>(f: () => Schema.Schema<Operation, Operation, never>) => Schema.suspend<Operation, Operation, never>

@since3.10.0

suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface Operation
Operation
> =>
const Operation: Schema.Struct<{
type: Schema.Literal<["operation"]>;
operator: Schema.Literal<["+", "-"]>;
left: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<...>]>;
}>;
right: Schema.Struct<...>;
}>
Operation
)
)
})
const
const Operation: Schema.Struct<{
type: Schema.Literal<["operation"]>;
operator: Schema.Literal<["+", "-"]>;
left: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<...>]>;
}>;
right: Schema.Struct<...>;
}>
Operation
=
import Schema
Schema
.
function Struct<{
type: Schema.Literal<["operation"]>;
operator: Schema.Literal<["+", "-"]>;
left: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<...>]>;
}>;
right: Schema.Struct<...>;
}>(fields: {
type: Schema.Literal<["operation"]>;
operator: Schema.Literal<["+", "-"]>;
left: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<...>]>;
}>;
right: Schema.Struct<...>;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
type: Schema.Literal<["operation"]>
type
:
import Schema
Schema
.
function Literal<["operation"]>(literals_0: "operation"): Schema.Literal<["operation"]> (+2 overloads)

@since3.10.0

Literal
("operation"),
operator: Schema.Literal<["+", "-"]>
operator
:
import Schema
Schema
.
function Literal<["+", "-"]>(literals_0: "+", literals_1: "-"): Schema.Literal<["+", "-"]> (+2 overloads)

@since3.10.0

Literal
("+", "-"),
left: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>;
}>
left
:
const Expression: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>;
}>
Expression
,
right: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>;
}>
right
:
const Expression: Schema.Struct<{
type: Schema.Literal<["expression"]>;
value: Schema.Union<[typeof Schema.Number, Schema.suspend<Operation, Operation, never>]>;
}>
Expression
})

Defining a recursive schema where the Encoded type differs from the Type type adds another layer of complexity. In such cases, we need to define two interfaces: one for the Type type, as seen previously, and another for the Encoded type.

Example (Recursive Schema with Different Encoded and Type Definitions)

Let’s consider an example: suppose we want to add an id field to the Category schema, where the schema for id is NumberFromString. It’s important to note that NumberFromString is a schema that transforms a string into a number, so the Type and Encoded types of NumberFromString differ, being number and string respectively. When we add this field to the Category schema, TypeScript raises an error:

import {
import Schema
Schema
} from "effect"
const
const fields: {
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}
fields
= {
id: typeof Schema.NumberFromString
id
:
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
,
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
}
interface
interface Category
Category
extends
import Schema
Schema
.
namespace Struct

@since3.10.0

@since3.10.0

@since3.10.0

Struct
.
type Struct<Fields extends Struct.Fields>.Type<F extends Schema.Struct.Fields> = UnionToIntersection<{ [K in keyof F]: F[K] extends Schema.Struct.OptionalPropertySignature ? { readonly [H in K]?: Schema.Schema.Type<F[H]>; } : { readonly [h in K]: Schema.Schema.Type<F[h]>; }; }[keyof F]> extends infer Q ? Q : never

@since3.10.0

Type
<typeof
const fields: {
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}
fields
> {
readonly
Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
}
const
const Category: Schema.Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}>
Category
=
import Schema
Schema
.
function Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}>(fields: {
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
...
const fields: {
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}
fields
,
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>
subcategories
:
import Schema
Schema
.
Array<Schema.suspend<Category, Category, never>>(value: Schema.suspend<Category, Category, never>): Schema.Array$<Schema.suspend<Category, Category, never>>
export Array

@since3.10.0

Array
(
// @ts-expect-error
import Schema
Schema
.
const suspend: <Category, Category, never>(f: () => Schema.Schema<Category, Category, never>) => Schema.suspend<Category, Category, never>

@since3.10.0

suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface Category
Category
> =>
const Category: Schema.Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}>
Category
)
)
})
/*
Type 'Struct<{ subcategories: Array$<suspend<Category, Category, never>>; id: typeof NumberFromString; name: typeof String$; }>' is not assignable to type 'Schema<Category, Category, never>'.
The types of 'Encoded.id' are incompatible between these types.
Type 'string' is not assignable to type 'number'.ts(2322)
*/

This error occurs because the explicit annotation Schema.Schema<Category> is no longer sufficient and needs to be adjusted by explicitly adding the Encoded type:

import {
import Schema
Schema
} from "effect"
const
const fields: {
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}
fields
= {
id: typeof Schema.NumberFromString
id
:
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
,
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
}
interface
interface Category
Category
extends
import Schema
Schema
.
namespace Struct

@since3.10.0

@since3.10.0

@since3.10.0

Struct
.
type Struct<Fields extends Struct.Fields>.Type<F extends Schema.Struct.Fields> = UnionToIntersection<{ [K in keyof F]: F[K] extends Schema.Struct.OptionalPropertySignature ? { readonly [H in K]?: Schema.Schema.Type<F[H]>; } : { readonly [h in K]: Schema.Schema.Type<F[h]>; }; }[keyof F]> extends infer Q ? Q : never

@since3.10.0

Type
<typeof
const fields: {
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}
fields
> {
readonly
Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
}
interface
interface CategoryEncoded
CategoryEncoded
extends
import Schema
Schema
.
namespace Struct

@since3.10.0

@since3.10.0

@since3.10.0

Struct
.
type Struct<Fields extends Struct.Fields>.Encoded<F extends Schema.Struct.Fields> = { readonly [K in Exclude<keyof F, Schema.Struct.EncodedTokenKeys<F>> as Schema.Struct.Key<F, K>]: Schema.Schema.Encoded<F[K]>; } & { readonly [K in Schema.Struct.EncodedTokenKeys<...> as Schema.Struct.Key<...>]?: Schema.Schema.Encoded<...>; }

@since3.10.0

Encoded
<typeof
const fields: {
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}
fields
> {
readonly
CategoryEncoded.subcategories: readonly CategoryEncoded[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface CategoryEncoded
CategoryEncoded
>
}
const
const Category: Schema.Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, CategoryEncoded, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}>
Category
=
import Schema
Schema
.
function Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, CategoryEncoded, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}>(fields: {
subcategories: Schema.Array$<Schema.suspend<Category, CategoryEncoded, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}): Schema.Struct<...> (+1 overload)

@since3.10.0

Struct
({
...
const fields: {
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}
fields
,
subcategories: Schema.Array$<Schema.suspend<Category, CategoryEncoded, never>>
subcategories
:
import Schema
Schema
.
Array<Schema.suspend<Category, CategoryEncoded, never>>(value: Schema.suspend<Category, CategoryEncoded, never>): Schema.Array$<...>
export Array

@since3.10.0

Array
(
import Schema
Schema
.
const suspend: <Category, CategoryEncoded, never>(f: () => Schema.Schema<Category, CategoryEncoded, never>) => Schema.suspend<Category, CategoryEncoded, never>

@since3.10.0

suspend
(
():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never>

@since3.10.0

@since3.10.0

Schema
<
interface Category
Category
,
interface CategoryEncoded
CategoryEncoded
> =>
const Category: Schema.Struct<{
subcategories: Schema.Array$<Schema.suspend<Category, CategoryEncoded, never>>;
id: typeof Schema.NumberFromString;
name: typeof Schema.String;
}>
Category
)
)
})