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)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
// Declare a schema for the File type using a type guard
4
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.

declare
(
5
(
(parameter) input: unknown
input
: unknown):
(parameter) 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

File
=>
(parameter) 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

File
6
)
7
8
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
=
import Schema
Schema
.
(alias) decodeUnknownSync<File, File>(schema: Schema.Schema<File, File, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => File export decodeUnknownSync
decodeUnknownSync
(
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
)
9
10
// Decoding a valid File object
11
namespace console var console: Console

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

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

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

log
(
const 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

File
([], "")))
12
/*
13
Output:
14
File { size: 0, type: '', name: '', lastModified: 1724774163056 }
15
*/
16
17
// Decoding an invalid input
18
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
(null)
19
/*
20
throws
21
ParseError: Expected <declaration schema>, actual null
22
*/

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.

A “title” should be concise, while a “description” provides a more detailed explanation of the purpose of the data described by the schema.

Example (Adding Annotations)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
// Declare a schema for the File type with additional annotations
4
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.

declare
(
5
(
(parameter) input: unknown
input
: unknown):
(parameter) 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

File
=>
(parameter) 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

File
,
6
{
7
// A unique identifier for the schema
8
(property) Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "FileFromSelf",
9
// Detailed description of the schema
10
(property) Annotations.Doc<A>.description?: string
description
: "The `File` type in JavaScript"
11
}
12
)
13
14
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
=
import Schema
Schema
.
(alias) decodeUnknownSync<File, File>(schema: Schema.Schema<File, File, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => File export decodeUnknownSync
decodeUnknownSync
(
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
)
15
16
// Decoding a valid File object
17
namespace console var console: Console

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

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

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

log
(
const 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

File
([], "")))
18
/*
19
Output:
20
File { size: 0, type: '', name: '', lastModified: 1724774163056 }
21
*/
22
23
// Decoding an invalid input
24
const decode: (u: unknown, overrideOptions?: ParseOptions) => File
decode
(null)
25
/*
26
throws
27
ParseError: Expected FileFromSelf, actual null
28
*/

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>)

1
import {
import ParseResult
ParseResult
,
import Schema
Schema
} from "@effect/schema"
2
3
export const
const MyReadonlySet: <A, I, R>(item: Schema.Schema<A, I, R>) => Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
MyReadonlySet
= <
(type parameter) A in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
A
,
(type parameter) I in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
I
,
(type parameter) R in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
R
>(
4
// Schema for the elements of the Set
5
(parameter) item: Schema.Schema<A, I, R>
item
:
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never> namespace Schema
Schema
<
(type parameter) A in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
A
,
(type parameter) I in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
I
,
(type parameter) R in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
R
>
6
):
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never> namespace Schema
Schema
<
interface ReadonlySet<T>
ReadonlySet
<
(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
<
(type parameter) I in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
I
>,
(type parameter) R in <A, I, R>(item: Schema.Schema<A, I, R>): Schema.Schema<ReadonlySet<A>, ReadonlySet<I>, R>
R
> =>
7
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.

declare
(
8
// Store the schema for the Set's elements
9
[
(parameter) item: Schema.Schema<A, I, R>
item
],
10
{
11
// Decoding function
12
(property) decode: (typeParameters_0: Schema.Schema<A, I, never>) => (input: unknown, options: ParseOptions, ast: Declaration) => Effect<...>
decode
: (
(parameter) item: Schema.Schema<A, I, never>
item
) => (
(parameter) input: unknown
input
,
(parameter) parseOptions: ParseOptions
parseOptions
,
(parameter) ast: Declaration
ast
) => {
13
if (
(parameter) input: unknown
input
instanceof
var Set: SetConstructor
Set
) {
14
// Decode each element in the Set
15
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<...>
decodeUnknown
(
import Schema
Schema
.
(alias) Array<Schema.Schema<A, I, never>>(value: Schema.Schema<A, I, never>): Schema.Array$<Schema.Schema<A, I, never>> export Array
Array
(
(parameter) item: Schema.Schema<A, I, never>
item
))(
16
var Array: ArrayConstructor
Array
.
(method) ArrayConstructor.from<any>(iterable: Iterable<any> | ArrayLike<any>): any[] (+3 overloads)

Creates an array from an iterable object.

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

Returns an iterable of values in the set.

values
()),
17
(parameter) parseOptions: ParseOptions
parseOptions
18
)
19
// Return a ReadonlySet containing the decoded elements
20
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)
map
(
21
const elements: Effect<readonly A[], ParseResult.ParseIssue, never>
elements
,
22
(
(parameter) as: readonly A[]
as
):
interface ReadonlySet<T>
ReadonlySet
<
(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
(
(parameter) as: readonly A[]
as
)
23
)
24
}
25
// Handle invalid input
26
return
import ParseResult
ParseResult
.
const fail: (issue: ParseResult.ParseIssue) => Either<never, ParseResult.ParseIssue>
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.

Type
(
(parameter) ast: Declaration
ast
,
(parameter) input: unknown
input
))
27
},
28
// Encoding function
29
(property) encode: (typeParameters_0: Schema.Schema<A, I, never>) => (input: unknown, options: ParseOptions, ast: Declaration) => Effect<...>
encode
: (
(parameter) item: Schema.Schema<A, I, never>
item
) => (
(parameter) input: unknown
input
,
(parameter) parseOptions: ParseOptions
parseOptions
,
(parameter) ast: Declaration
ast
) => {
30
if (
(parameter) input: unknown
input
instanceof
var Set: SetConstructor
Set
) {
31
// Encode each element in the Set
32
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<...>
encodeUnknown
(
import Schema
Schema
.
(alias) Array<Schema.Schema<A, I, never>>(value: Schema.Schema<A, I, never>): Schema.Array$<Schema.Schema<A, I, never>> export Array
Array
(
(parameter) item: Schema.Schema<A, I, never>
item
))(
33
var Array: ArrayConstructor
Array
.
(method) ArrayConstructor.from<any>(iterable: Iterable<any> | ArrayLike<any>): any[] (+3 overloads)

Creates an array from an iterable object.

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

Returns an iterable of values in the set.

values
()),
34
(parameter) parseOptions: ParseOptions
parseOptions
35
)
36
// Return a ReadonlySet containing the encoded elements
37
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)
map
(
38
const elements: Effect<readonly I[], ParseResult.ParseIssue, never>
elements
,
39
(
(parameter) is: readonly I[]
is
):
interface ReadonlySet<T>
ReadonlySet
<
(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
(
(parameter) is: readonly I[]
is
)
40
)
41
}
42
// Handle invalid input
43
return
import ParseResult
ParseResult
.
const fail: (issue: ParseResult.ParseIssue) => Either<never, ParseResult.ParseIssue>
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.

Type
(
(parameter) ast: Declaration
ast
,
(parameter) input: unknown
input
))
44
}
45
},
46
{
47
(property) Annotations.Doc<A>.description?: string
description
: `ReadonlySet<${
import Schema
Schema
.
const format: <A, I, R>(schema: Schema.Schema<A, I, R>) => string
format
(
(parameter) item: Schema.Schema<A, I, R>
item
)}>`
48
}
49
)
50
51
// Define a schema for a ReadonlySet of numbers
52
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".

NumberFromString
)
53
54
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<number>
decode
=
import Schema
Schema
.
(alias) decodeUnknownSync<ReadonlySet<number>, ReadonlySet<string>>(schema: Schema.Schema<ReadonlySet<number>, ReadonlySet<string>, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<...> export decodeUnknownSync
decodeUnknownSync
(
const setOfNumbers: Schema.Schema<ReadonlySet<number>, ReadonlySet<string>, never>
setOfNumbers
)
55
56
namespace console var console: Console

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

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

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

log
(
const 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 }
57
58
// Decode an invalid input
59
const decode: (u: unknown, overrideOptions?: ParseOptions) => ReadonlySet<number>
decode
(null)
60
/*
61
throws
62
ParseError: Expected ReadonlySet<NumberFromString>, actual null
63
*/
64
65
// Decode a Set with an invalid element
66
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"]))
67
/*
68
throws
69
ParseError: ReadonlyArray<NumberFromString>
70
└─ [1]
71
└─ NumberFromString
72
└─ Encoded side transformation failure
73
└─ Expected string, actual null
74
*/

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 (Missing Annotations)

1
import {
import Arbitrary
Arbitrary
,
import Schema
Schema
} from "@effect/schema"
2
3
// Define a schema for the File type
4
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.

declare
(
5
(
(parameter) input: unknown
input
: unknown):
(parameter) 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

File
=>
(parameter) 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

File
,
6
{
7
(property) Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "FileFromSelf"
8
}
9
)
10
11
// Try creating an Arbitrary instance for the schema
12
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.

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

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 Annotations)

1
import {
import Arbitrary
Arbitrary
,
import FastCheck
FastCheck
,
import Pretty
Pretty
,
import Schema
Schema
} from "@effect/schema"
2
3
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.

declare
(
4
(
(parameter) input: unknown
input
: unknown):
(parameter) 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

File
=>
(parameter) 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

File
,
5
{
6
(property) Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "FileFromSelf",
7
// Provide a function to generate random File instances
8
(property) Annotations.Schema<File, readonly []>.arbitrary?: (ctx: Arbitrary.GenerationContext) => Arbitrary.LazyArbitrary<File>
arbitrary
: () => (
(parameter) fc: typeof FastCheck
fc
) =>
9
(parameter) fc: typeof FastCheck
fc
10
.
(alias) 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`

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

For strings of {@link char }

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

For strings of {@link char }

string
())
11
.
(method) 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

map
(([
(parameter) content: string
content
,
(parameter) 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

File
([
(parameter) content: string
content
],
(parameter) path: string
path
))
12
}
13
)
14
15
// Create an Arbitrary instance for the schema
16
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.

make
(
const FileFromSelf: Schema.SchemaClass<File, File, never>
FileFromSelf
)
17
18
// Generate sample files using the Arbitrary instance
19
const
const files: File[]
files
=
import FastCheck
FastCheck
.
(alias) 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 {@link assert } or {@link check }

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

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

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

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

log
(
const files: File[]
files
)
21
/*
22
Example Output:
23
[
24
File { size: 5, type: '', name: 'C', lastModified: 1706435571176 },
25
File { size: 1, type: '', name: '98Ggmc', lastModified: 1706435571176 }
26
]
27
*/

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)

1
type
type UserId = string
UserId
= string
2
type
type Username = string
Username
= string
3
4
declare const
const getUser: (id: UserId) => object
getUser
: (
(parameter) id: string
id
:
type UserId = string
UserId
) => object
5
6
const
const myUsername: string
myUsername
:
type Username = string
Username
= "gcanti"
7
8
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)

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

A generic interface that defines a branded type.

Brand
<"UserId">
4
type
type Username = string
Username
= string
5
6
declare const
const getUser: (id: UserId) => object
getUser
: (
(parameter) id: UserId
id
:
type UserId = string & Brand.Brand<"UserId">
UserId
) => object
7
8
const
const myUsername: string
myUsername
:
type Username = string
Username
= "gcanti"
9
10
// @ts-expect-error
11
const getUser: (id: UserId) => object
getUser
(
const myUsername: string
myUsername
)
12
/*
13
Argument of type 'string' is not assignable to parameter of type 'UserId'.
14
Type 'string' is not assignable to type 'Brand<"UserId">'.ts(2345)
15
*/

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)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
=
import Schema
Schema
.
(alias) class String export String
String
.
(method) 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>> ```

brand
("UserId"))
4
5
// string & Brand<"UserId">
6
type
type UserId = string & Brand<"UserId">
UserId
= typeof
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
.
(property) 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)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const UserIdBrand: typeof UserIdBrand
UserIdBrand
: unique symbol =
var Symbol: SymbolConstructor
Symbol
.
(method) 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.

for
("UserId")
4
5
const
const UserId: Schema.brand<typeof Schema.String, typeof UserIdBrand>
UserId
=
import Schema
Schema
.
(alias) class String export String
String
.
(method) 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>> ```

brand
(
const UserIdBrand: typeof UserIdBrand
UserIdBrand
))
6
7
// string & Brand<typeof UserIdBrand>
8
type
type UserId = string & Brand<typeof UserIdBrand>
UserId
= typeof
const UserId: Schema.brand<typeof Schema.String, typeof UserIdBrand>
UserId
.
(property) 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)

1
import {
import Schema
Schema
} from "@effect/schema"
2
import {
import Brand
Brand
} from "effect"
3
4
// the existing branded type
5
type
type UserId = string & Brand.Brand<"UserId">
UserId
= string &
import Brand
Brand
.
interface Brand<in out K extends string | symbol> namespace Brand

A generic interface that defines a branded type.

Brand
<"UserId">
6
7
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 {@link refined } .

nominal
<
type UserId = string & Brand.Brand<"UserId">
UserId
>()
8
9
// Define a schema for the branded type
10
const
const UserIdSchema: Schema.BrandSchema<string & Brand.Brand<"UserId">, string, never>
UserIdSchema
=
import Schema
Schema
.
(alias) class String export String
String
.
(method) 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<...>
fromBrand
(
const UserId: Brand.Brand.Constructor<UserId>
UserId
))

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

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
=
import Schema
Schema
.
(alias) class String export String
String
.
(method) 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>> ```

brand
("UserId"))
4
5
const
const userId: string & Brand<"UserId">
userId
=
const UserId: Schema.brand<typeof Schema.String, "UserId">
UserId
.
(method) 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.

Let’s start with the simple definition of a property signature that can be used to add annotations:

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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) namespace Struct
Struct
({
4
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) 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`.

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

NumberFromString
).
(method) 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
({
6
(property) Annotations.Doc<number>.title?: string
title
: "Age"
7
})
8
})

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)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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) namespace Struct
Struct
({
4
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) 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`.

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

NumberFromString
).
(method) 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
(
6
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.

fromKey
("AGE") // Maps from "AGE" to "age"
7
)
8
})
9
10
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const Person: Schema.Struct<{ name: typeof Schema.String; age: Schema.PropertySignature<":", number, "AGE", ":", string, false, never>; }>
Person
)({
(property) name: string
name
: "name",
(property) AGE: string
AGE
: "18" }))
11
// 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>

Schema.optional(schema: Schema<A, I, R>) defines a basic optional property.

Decoding

  • <missing value> remains <missing value>
  • undefined remains undefined
  • Input i: I transforms to a: A

Encoding

  • <missing value> remains <missing value>
  • undefined remains undefined
  • Input a: A transforms back to i: I

Example

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
=
import Schema
Schema
.
function Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>(fields: { a: Schema.optional<typeof Schema.NumberFromString>; }): Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }> (+1 overload) namespace Struct
Struct
({
4
(property) a: Schema.optional<typeof Schema.NumberFromString>
a
:
import Schema
Schema
.
const optional: <typeof Schema.NumberFromString>(self: typeof Schema.NumberFromString) => Schema.optional<typeof Schema.NumberFromString>
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".

NumberFromString
)
5
})
6
7
// { readonly a?: number | undefined; }
8
type
type Type = { readonly a?: number | undefined; }
Type
= typeof
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
.
(property) Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>.Type: { readonly a?: number | undefined; }
Type
9
10
// { readonly a?: string | undefined; }
11
type
type Encoded = { readonly a?: string | undefined; }
Encoded
= typeof
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
.
(property) Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>.Encoded: { readonly a?: string | undefined; }
Encoded
12
13
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
)({
(property) a: string
a
: "1" }))
14
// Output: { a: 1 }
15
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
)({}))
16
// Output: {}
17
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
)({
(property) a: undefined
a
:
var undefined
undefined
}))
18
// Output: { a: undefined }
19
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>, options?: ParseOptions): (a: { readonly a?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly a?: string | undefined; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
)({
(property) a?: number | undefined
a
: 1 }))
20
// Output: { a: "1" }
21
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>, options?: ParseOptions): (a: { readonly a?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly a?: string | undefined; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
)({}))
22
// Output: {}
23
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | undefined; }, never>, options?: ParseOptions): (a: { readonly a?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly a?: string | undefined; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.NumberFromString>; }>
schema
)({
(property) a?: number | undefined
a
:
var undefined
undefined
}))
24
// Output: { a: undefined }

Schema.optionalWith(schema: Schema<A, I, R>, { nullable: true }) allows handling of null values as equivalent to missing values.

Decoding

  • <missing value> remains <missing value>
  • undefined remains undefined
  • null transforms to <missing value>
  • Input i: I transforms to a: A

Encoding

  • <missing value> remains <missing value>
  • undefined remains undefined
  • Input a: A transforms back to i: I

Example

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
=
import Schema
Schema
.
function Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>(fields: { a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>
a
:
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)
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".

NumberFromString
, {
(property) nullable: true
nullable
: true })
5
})
6
7
// { readonly a?: number | undefined; }
8
type
type Type = { readonly a?: number | undefined; }
Type
= typeof
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
.
(property) Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>.Type: { readonly a?: number | undefined; }
Type
9
10
// { readonly a?: string | null | undefined; }
11
type
type Encoded = { readonly a?: string | null | undefined; }
Encoded
= typeof
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
.
(property) Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>.Encoded: { readonly a?: string | null | undefined; }
Encoded
12
13
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
)({
(property) a: string
a
: "1" }))
14
// Output: { a: 1 }
15
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
)({}))
16
// Output: {}
17
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
)({
(property) a: undefined
a
:
var undefined
undefined
}))
18
// Output: { a: undefined }
19
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
)({
(property) a: null
a
: null }))
20
// Output: {}
21
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>, options?: ParseOptions): (a: { readonly a?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly a?: string | null | undefined; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
)({
(property) a?: number | undefined
a
: 1 }))
22
// Output: { a: "1" }
23
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>, options?: ParseOptions): (a: { readonly a?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly a?: string | null | undefined; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
)({}))
24
// Output: {}
25
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }>(schema: Schema.Schema<{ readonly a?: number | undefined; }, { readonly a?: string | null | undefined; }, never>, options?: ParseOptions): (a: { readonly a?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly a?: string | null | undefined; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
schema
)({
(property) a?: number | undefined
a
:
var undefined
undefined
}))
26
// Output: { a: undefined }

Schema.optionalWith(schema: Schema<A, I, R>, { exact: true }) ensures that only the exact types specified are handled, excluding undefined.

Decoding

  • <missing value> remains <missing value>
  • Input i: I transforms to a: A

Encoding

  • <missing value> remains <missing value>
  • Input a: A transforms back to i: I

Example

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
=
import Schema
Schema
.
function Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>(fields: { a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>
a
:
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)
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".

NumberFromString
, {
(property) exact: true
exact
: true })
5
})
6
7
// { readonly a?: number; }
8
type
type Type = { readonly a?: number; }
Type
= typeof
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
.
(property) Schema<{ readonly a?: number; }, { readonly a?: string; }, never>.Type: { readonly a?: number; }
Type
9
10
// { readonly a?: string; }
11
type
type Encoded = { readonly a?: string; }
Encoded
= typeof
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
.
(property) Schema<{ readonly a?: number; }, { readonly a?: string; }, never>.Encoded: { readonly a?: string; }
Encoded
12
13
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
)({
(property) a: string
a
: "1" }))
14
// Output: { a: 1 }
15
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
)({}))
16
// Output: {}
17
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
)({
(property) a: undefined
a
:
var undefined
undefined
}))
18
/*
19
throws:
20
ParseError: { readonly a?: NumberFromString }
21
└─ ["a"]
22
└─ NumberFromString
23
└─ Encoded side transformation failure
24
└─ Expected string, actual undefined
25
*/
26
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string; }, never>, options?: ParseOptions): (a: { readonly a?: number; }, overrideOptions?: ParseOptions) => { readonly a?: string; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
)({
(property) a?: number
a
: 1 }))
27
// Output: { a: "1" }
28
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string; }, never>, options?: ParseOptions): (a: { readonly a?: number; }, overrideOptions?: ParseOptions) => { readonly a?: string; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
schema
)({}))
29
// Output: {}

Schema.optionalWith(schema: Schema<A, I, R>, { exact: true, nullable: true }) combines handling for exact types and null values.

Decoding

  • <missing value> remains <missing value>
  • null transforms to <missing value>
  • Input i: I transforms to a: A

Encoding

  • <missing value> remains <missing value>
  • Input a: A transforms back to i: I

Example

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
=
import Schema
Schema
.
function Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>(fields: { a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>
a
:
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)
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".

NumberFromString
, {
5
(property) exact: true
exact
: true,
6
(property) nullable: true
nullable
: true
7
})
8
})
9
10
// { readonly a?: number; }
11
type
type Type = { readonly a?: number; }
Type
= typeof
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
.
(property) Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>.Type: { readonly a?: number; }
Type
12
13
// { readonly a?: string | null; }
14
type
type Encoded = { readonly a?: string | null; }
Encoded
= typeof
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
.
(property) Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>.Encoded: { readonly a?: string | null; }
Encoded
15
16
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number; }, { readonly a?: string | null; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
)({
(property) a: string
a
: "1" }))
17
// Output: { a: 1 }
18
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number; }, { readonly a?: string | null; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
)({}))
19
// Output: {}
20
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number; }, { readonly a?: string | null; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
)({
(property) a: undefined
a
:
var undefined
undefined
}))
21
/*
22
throws:
23
ParseError: (Struct (Encoded side) <-> Struct (Type side))
24
└─ Encoded side transformation failure
25
└─ Struct (Encoded side)
26
└─ ["a"]
27
└─ NumberFromString | null
28
├─ NumberFromString
29
│ └─ Encoded side transformation failure
30
│ └─ Expected string, actual undefined
31
└─ Expected null, actual undefined
32
*/
33
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: number; }, { readonly a?: string | null; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: number; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
)({
(property) a: null
a
: null }))
34
// Output: {}
35
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number; }, { readonly a?: string | null; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>, options?: ParseOptions): (a: { readonly a?: number; }, overrideOptions?: ParseOptions) => { readonly a?: string | null; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
)({
(property) a?: number
a
: 1 }))
36
// Output: { a: "1" }
37
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: number; }, { readonly a?: string | null; }>(schema: Schema.Schema<{ readonly a?: number; }, { readonly a?: string | null; }, never>, options?: ParseOptions): (a: { readonly a?: number; }, overrideOptions?: ParseOptions) => { readonly a?: string | null; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
schema
)({}))
38
// Output: {}

When defining types in TypeScript that include optional fields with the type never, such as:

1
type MyType = {
2
readonly foo?: never
3
}

the approach varies based on the exactOptionalPropertyTypes configuration in your tsconfig.json

TypeScript Configuration: 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.

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.Never>; }>
schema
=
import Schema
Schema
.
function Struct<{ a: Schema.optional<typeof Schema.Never>; }>(fields: { a: Schema.optional<typeof Schema.Never>; }): Schema.Struct<{ a: Schema.optional<typeof Schema.Never>; }> (+1 overload) namespace Struct
Struct
({
4
(property) a: Schema.optional<typeof Schema.Never>
a
:
import Schema
Schema
.
const optional: <typeof Schema.Never>(self: typeof Schema.Never) => Schema.optional<typeof Schema.Never>
optional
(
import Schema
Schema
.
class Never
Never
)
5
})
6
7
// { readonly a?: undefined; }
8
type
type Type = { readonly a?: undefined; }
Type
= typeof
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.Never>; }>
schema
.
(property) Schema<{ readonly a?: undefined; }, { readonly a?: undefined; }, never>.Type: { readonly a?: undefined; }
Type
9
10
// { readonly a?: undefined; }
11
type
type Encoded = { readonly a?: undefined; }
Encoded
= typeof
const schema: Schema.Struct<{ a: Schema.optional<typeof Schema.Never>; }>
schema
.
(property) Schema<{ readonly a?: undefined; }, { readonly a?: undefined; }, never>.Encoded: { readonly a?: undefined; }
Encoded

TypeScript Configuration: exactOptionalPropertyTypes = true

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

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const schema: Schema.Struct<{ foo: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>
schema
=
import Schema
Schema
.
function Struct<{ foo: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>(fields: { foo: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }): Schema.Struct<{ foo: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }> (+1 overload) namespace Struct
Struct
({
4
(property) foo: Schema.optionalWith<typeof Schema.Never, { exact: true; }>
foo
:
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)
optionalWith
(
import Schema
Schema
.
class Never
Never
, {
(property) exact: true
exact
: true })
5
})
6
7
// { readonly a?: never; }
8
type
type Type = { readonly foo?: never; }
Type
= typeof
const schema: Schema.Struct<{ foo: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>
schema
.
(property) Schema<{ readonly foo?: never; }, { readonly foo?: never; }, never>.Type: { readonly foo?: never; }
Type
9
10
// { readonly a?: never; }
11
type
type Encoded = { readonly foo?: never; }
Encoded
= typeof
const schema: Schema.Struct<{ foo: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>
schema
.
(property) Schema<{ readonly foo?: never; }, { readonly foo?: never; }, never>.Encoded: { readonly foo?: never; }
Encoded

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 most straightforward use case. If the input is missing or undefined, the default value will be used.

Syntax

Schema.optionalWith(schema: Schema<A, I, R>, { default: () => A })

Decoding: Translates missing or undefined inputs to a default value

Encoding: Input a: A transforms back to i: I

Example

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>(fields: { name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<...>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) price: typeof Schema.NumberFromString
price
:
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".

NumberFromString
,
6
(property) 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)
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".

NumberFromString
, {
7
(property) default: () => number
default
: () => 1 // Default value for quantity
8
})
9
})
10
11
// Applying defaults in the decoding phase
12
13
namespace console var console: Console

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

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

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

log
(
14
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly name: string; readonly price: number; readonly quantity: number; }, { readonly name: string; readonly price: string; readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly name: string; readonly price: number; readonly quantity: number; }, { ...; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly name: string; readonly price: number; readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
)({
(property) name: string
name
: "Laptop",
(property) price: string
price
: "999" })
15
)
16
// Output: { name: 'Laptop', price: 999, quantity: 1 }
17
18
namespace console var console: Console

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

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

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

log
(
19
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly name: string; readonly price: number; readonly quantity: number; }, { readonly name: string; readonly price: string; readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly name: string; readonly price: number; readonly quantity: number; }, { ...; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly name: string; readonly price: number; readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
)({
20
(property) name: string
name
: "Laptop",
21
(property) price: string
price
: "999",
22
(property) quantity: string
quantity
: "2"
23
})
24
)
25
// Output: { name: 'Laptop', price: 999, quantity: 2 }
26
27
// Applying defaults in the constructor
28
29
namespace console var console: Console

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

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

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

log
(
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
.
(method) TypeLiteral<{ name: typeof String$; price: typeof NumberFromString; quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.make(props: { readonly name: string; readonly price: number; readonly quantity?: number; }, options?: MakeOptions): { readonly name: string; readonly price: number; readonly quantity: number; }
make
({
(property) name: string
name
: "Laptop",
(property) price: number
price
: 999 }))
30
// Output: { name: 'Laptop', price: 999, quantity: 1 }
31
32
namespace console var console: Console

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

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

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

log
(
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
.
(method) TypeLiteral<{ name: typeof String$; price: typeof NumberFromString; quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.make(props: { readonly name: string; readonly price: number; readonly quantity?: number; }, options?: MakeOptions): { readonly name: string; readonly price: number; readonly quantity: number; }
make
({
(property) name: string
name
: "Laptop",
(property) price: number
price
: 999,
(property) quantity?: number
quantity
: 2 }))
33
// Output: { name: 'Laptop', price: 999, quantity: 2 }

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
})

Decoding: Applies the default value only if the input is missing

Encoding: Input a: A transforms back to i: I

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
})

Decoding: Treats null, undefined, or missing inputs as defaults

Encoding: Input a: A transforms back to i: I

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
})

Decoding: Defaults are applied when values are null or missing

Encoding: Input a: A transforms back to i: I

In many cases you may want to transform an optional field into an Option type. This approach allows you to explicitly manage whether a field is present or not, rather than relying on undefined or null.

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

Syntax

optionalWith(schema: Schema<A, I, R>, { as: "Option" })

Decoding

  • Missing values or undefined are converted to Option.none()
  • Provided values (i: I) are converted to Option.some(a: A)

Encoding

  • Option.none() results in the value being omitted
  • Option.some(a: A) is converted back to the original input (i: I)

Example

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>(fields: { name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<...>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) price: typeof Schema.NumberFromString
price
:
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".

NumberFromString
,
6
(property) 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)
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".

NumberFromString
, {
(property) as: "Option"
as
: "Option" })
7
})
8
9
namespace console var console: Console

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

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

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

log
(
10
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly name: string; readonly price: number; readonly quantity: Option<number>; }, { readonly name: string; readonly price: string; readonly quantity?: string | undefined; }>(schema: Schema.Schema<...>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { ...; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
)({
(property) name: string
name
: "Laptop",
(property) price: string
price
: "999" })
11
)
12
// Output: { name: 'Laptop', price: 999, quantity: none() }
13
14
namespace console var console: Console

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

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

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

log
(
15
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly name: string; readonly price: number; readonly quantity: Option<number>; }, { readonly name: string; readonly price: string; readonly quantity?: string | undefined; }>(schema: Schema.Schema<...>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { ...; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ name: typeof Schema.String; price: typeof Schema.NumberFromString; quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
)({
16
(property) name: string
name
: "Laptop",
17
(property) price: string
price
: "999",
18
(property) quantity: string
quantity
: "2"
19
})
20
)
21
// Output: { name: 'Laptop', price: 999, quantity: some(2) }

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
})

Decoding

  • Only truly missing values are converted to Option.none()
  • Provided values (i: I) are converted to Option.some(a)

Encoding

  • Option.none() results in the value being omitted
  • Option.some(a: A) is converted back to the original input (i: I)

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
})

Decoding

  • Treats missing, undefined, and null values all as Option.none()
  • Provided values (i: I) are converted to Option.some(a: A)

Encoding

  • Option.none() results in the value being omitted
  • Option.some(a: A) is converted back to the original input (i: I)

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

Syntax

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

Decoding

  • Missing or null values lead to Option.none()
  • Provided values (i: I) are converted to Option.some(a: A)

Encoding

  • Option.none() results in the value being omitted
  • Option.some(a: A) is converted back to the original input (i: I)

The Schema.optionalToOptional API is used to manage the transformation from an optional field to another optional field. With this, we can control both the output type and the presence or absence of the field.

For example a common use case is to equate a specific value in the source field with the absence of value in the destination field.

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>

You can transform the type by specifying a schema for to, which can be different from the schema of from. Additionally, you can control the presence or absence of the field using decode and encode, with the following meanings:

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

Example

Suppose we have an optional field of type string, and we want to exclude empty strings from the output. In other words, if the input contains an empty string, we want the field to be absent in the output.

1
import {
import Schema
Schema
} from "@effect/schema"
2
import {
(alias) const identity: <A>(a: A) => A import identity

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

identity
,
import Option
Option
} from "effect"
3
4
const
const schema: Schema.Struct<{ a: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>
schema
=
import Schema
Schema
.
function Struct<{ a: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>(fields: { a: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
5
(property) a: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>
a
:
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.

optionalToOptional
(
import Schema
Schema
.
(alias) class String export String
String
,
import Schema
Schema
.
(alias) class String export String
String
, {
6
(property) decode: (o: Option.Option<string>) => Option.Option<string>
decode
: (
(parameter) input: Option.Option<string>
input
) => {
7
if (
import Option
Option
.
const isNone: <string>(self: Option.Option<string>) => self is Option.None<string>

Determine if a `Option` is a `None`.

isNone
(
(parameter) input: Option.Option<string>
input
)) {
8
// If the field is absent in the input, returning `Option.none()`
9
// will make it absent in the output too
10
return
import Option
Option
.
const none: <string>() => Option.Option<string>

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

none
()
11
}
12
const
const value: string
value
=
(parameter) input: Option.Some<string>
input
.
(property) Some<string>.value: string
value
13
if (
const value: string
value
=== "") {
14
// If the field is present in the input but is an empty string,
15
// returning `Option.none()` will make it absent in the output
16
return
import Option
Option
.
const none: <string>() => Option.Option<string>

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

none
()
17
}
18
// If the field is present in the input and is not an empty string,
19
// returning `Option.some` will make it present in the output
20
return
import Option
Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new `Option` that wraps the given value.

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

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

identity
27
})
28
})
29
30
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: string; }
decode
=
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a?: string; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a?: string; }, { readonly a?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: string; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>
schema
)
31
32
namespace console var console: Console

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

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

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

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: string; }
decode
({}))
33
// Output: {}
34
namespace console var console: Console

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

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

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

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: string; }
decode
({
(property) a: string
a
: "" }))
35
// Output: {}
36
namespace console var console: Console

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

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

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

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly a?: string; }
decode
({
(property) a: string
a
: "a non-empty string" }))
37
// Output: { a: 'a non-empty string' }
38
39
const
const encode: (a: { readonly a?: string; }, overrideOptions?: ParseOptions) => { readonly a?: string; }
encode
=
import Schema
Schema
.
(alias) encodeSync<{ readonly a?: string; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a?: string; }, { readonly a?: string; }, never>, options?: ParseOptions): (a: { readonly a?: string; }, overrideOptions?: ParseOptions) => { readonly a?: string; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>
schema
)
40
41
namespace console var console: Console

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

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

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

log
(
const encode: (a: { readonly a?: string; }, overrideOptions?: ParseOptions) => { readonly a?: string; }
encode
({}))
42
// Output: {}
43
namespace console var console: Console

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

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

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

log
(
const encode: (a: { readonly a?: string; }, overrideOptions?: ParseOptions) => { readonly a?: string; }
encode
({
(property) a?: string
a
: "" }))
44
// Output: { a: '' }
45
namespace console var console: Console

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

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

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

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

The Schema.optionalToRequired API allows us to transform an optional field into a required one, applying custom logic if the field is absent 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>

You can control the presence or absence of the field using decode and encode, with the following meanings:

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

Example

For instance, a common use case is to assign a default value to the field in the output if it’s missing in the input.

1
import {
import Schema
Schema
} from "@effect/schema"
2
import {
import Option
Option
} from "effect"
3
4
const
const schema: Schema.Struct<{ a: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>
schema
=
import Schema
Schema
.
function Struct<{ a: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>(fields: { a: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
5
(property) a: Schema.PropertySignature<":", string, never, "?:", string, false, never>
a
:
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.

optionalToRequired
(
import Schema
Schema
.
(alias) class String export String
String
,
import Schema
Schema
.
(alias) class String export String
String
, {
6
(property) decode: (o: Option.Option<string>) => string
decode
: (
(parameter) input: Option.Option<string>
input
) => {
7
if (
import Option
Option
.
const isNone: <string>(self: Option.Option<string>) => self is Option.None<string>

Determine if a `Option` is a `None`.

isNone
(
(parameter) input: Option.Option<string>
input
)) {
8
// If the field is absent in the input,
9
// we can return the default value for the field in the output
10
return "default value"
11
}
12
// If the field is present in the input,
13
// return its value as it is in the output
14
return
(parameter) input: Option.Some<string>
input
.
(property) Some<string>.value: string
value
15
},
16
// During encoding, we can choose to handle things differently,
17
// or simply return the same value present in the input for the output
18
(property) encode: (ti: string) => Option.Option<string>
encode
: (
(parameter) a: string
a
) =>
import Option
Option
.
const some: <string>(value: string) => Option.Option<string>

Creates a new `Option` that wraps the given value.

some
(
(parameter) a: string
a
)
19
})
20
})
21
22
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly a: string; }
decode
=
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly a: string; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a: string; }, { readonly a?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly a: string; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>
schema
)
23
24
namespace console var console: Console

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

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

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

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly a: string; }
decode
({}))
25
// Output: { a: 'default value' }
26
namespace console var console: Console

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

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

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

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly a: string; }
decode
({
(property) a: string
a
: "foo" }))
27
// Output: { a: 'foo' }
28
29
const
const encode: (a: { readonly a: string; }, overrideOptions?: ParseOptions) => { readonly a?: string; }
encode
=
import Schema
Schema
.
(alias) encodeSync<{ readonly a: string; }, { readonly a?: string; }>(schema: Schema.Schema<{ readonly a: string; }, { readonly a?: string; }, never>, options?: ParseOptions): (a: { readonly a: string; }, overrideOptions?: ParseOptions) => { readonly a?: string; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ a: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>
schema
)
30
31
namespace console var console: Console

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

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

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

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

This API allows developers to specify how a field that is normally required can be treated as optional based on custom logic.

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>

You can control the presence or absence of the field using decode and encode, with the following meanings:

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

Example

Let’s look at a practical example where a field name that is typically required can be considered optional if it’s an empty string during decoding, and ensure there is always a value during encoding by providing a default.

1
import {
import Schema
Schema
} from "@effect/schema"
2
import {
import Option
Option
} from "effect"
3
4
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) namespace Struct
Struct
({
5
(property) 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.

requiredToOptional
(
import Schema
Schema
.
(alias) class String export String
String
,
import Schema
Schema
.
(alias) class String export String
String
, {
6
// empty string is considered as absent
7
(property) decode: (fa: string) => Option.Option<string>
decode
:
import Option
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`.

liftPredicate
((
(parameter) s: string
s
) =>
(parameter) s: string
s
!== ""),
8
(property) encode: (o: Option.Option<string>) => string
encode
:
import Option
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`

getOrElse
(() => "")
9
})
10
})
11
12
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly name?: string; }
decode
=
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const schema: Schema.Struct<{ name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>; }>
schema
)
13
14
namespace console var console: Console

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

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

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

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly name?: string; }
decode
({
(property) name: string
name
: "John" }))
15
// Output: { name: 'John' }
16
namespace console var console: Console

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

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

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

log
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly name?: string; }
decode
({
(property) name: string
name
: "" }))
17
// Output: {}
18
19
const
const encode: (a: { readonly name?: string; }, overrideOptions?: ParseOptions) => { readonly name: string; }
encode
=
import Schema
Schema
.
(alias) 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
encodeSync
(
const schema: Schema.Struct<{ name: Schema.PropertySignature<"?:", string, never, ":", string, false, never>; }>
schema
)
20
21
namespace console var console: Console

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

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

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

log
(
const encode: (a: { readonly name?: string; }, overrideOptions?: ParseOptions) => { readonly name: string; }
encode
({
(property) name?: string
name
: "John" }))
22
// Output: { name: 'John' }
23
namespace console var console: Console

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

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

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

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

Structs expose their fields through a fields property. This feature can be utilized to extend an existing struct with additional fields or to merge fields from another struct.

Example (Adding Fields)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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) namespace Struct
Struct
({
4
(property) a: typeof Schema.String
a
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) b: typeof Schema.String
b
:
import Schema
Schema
.
(alias) class String export String
String
6
})
7
8
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) namespace Struct
Struct
({
9
...
const Struct1: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Struct1
.
(property) TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: { readonly a: typeof Schema.String; readonly b: typeof Schema.String; }
fields
,
10
// other fields
11
(property) c: typeof Schema.String
c
:
import Schema
Schema
.
(alias) class String export String
String
,
12
(property) d: typeof Schema.String
d
:
import Schema
Schema
.
(alias) class String export String
String
13
})

Example (Integrating Additional Index Signatures)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const Struct: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Struct
=
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) namespace Struct
Struct
({
4
(property) a: typeof Schema.String
a
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) b: typeof Schema.String
b
:
import Schema
Schema
.
(alias) class String export String
String
6
})
7
8
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) namespace Struct
Struct
(
9
const Struct: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Struct
.
(property) TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: { readonly a: typeof Schema.String; readonly b: typeof Schema.String; }
fields
,
10
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>
Record
({
(property) key: typeof Schema.String
key
:
import Schema
Schema
.
(alias) class String export String
String
,
(property) value: typeof Schema.String
value
:
import Schema
Schema
.
(alias) class String export String
String
})
11
)

Example (Merging Fields from Two Structs)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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) namespace Struct
Struct
({
4
(property) a: typeof Schema.String
a
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) b: typeof Schema.String
b
:
import Schema
Schema
.
(alias) class String export String
String
6
})
7
8
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) namespace Struct
Struct
({
9
(property) c: typeof Schema.String
c
:
import Schema
Schema
.
(alias) class String export String
String
,
10
(property) d: typeof Schema.String
d
:
import Schema
Schema
.
(alias) class String export String
String
11
})
12
13
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) namespace Struct
Struct
({
14
...
const Struct1: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Struct1
.
(property) TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: { readonly a: typeof Schema.String; readonly b: typeof Schema.String; }
fields
,
15
...
const Struct2: Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.String; }>
Struct2
.
(property) TypeLiteral<{ c: typeof String$; d: typeof String$; }, []>.fields: { readonly c: typeof Schema.String; readonly d: typeof Schema.String; }
fields
16
})

The Schema.extend function offers a structured way to extend schemas, particularly useful when direct field spreading is insufficient—for instance, when you need to extend a struct with a union of structs.

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 (Extending a Struct with a Union of Structs)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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) namespace Struct
Struct
({
4
(property) a: typeof Schema.String
a
:
import Schema
Schema
.
(alias) class String export String
String
5
})
6
7
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)
Union
(
8
import Schema
Schema
.
function Struct<{ b: typeof Schema.String; }>(fields: { b: typeof Schema.String; }): Schema.Struct<{ b: typeof Schema.String; }> (+1 overload) namespace Struct
Struct
({
(property) b: typeof Schema.String
b
:
import Schema
Schema
.
(alias) class String export String
String
}),
9
import Schema
Schema
.
function Struct<{ c: typeof Schema.String; }>(fields: { c: typeof Schema.String; }): Schema.Struct<{ c: typeof Schema.String; }> (+1 overload) namespace Struct
Struct
({
(property) c: typeof Schema.String
c
:
import Schema
Schema
.
(alias) class String export String
String
})
10
)
11
12
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

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
)

This example shows an attempt to extend a struct with another struct where field names overlap, leading to an error:

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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) namespace Struct
Struct
({
4
(property) a: typeof Schema.String
a
:
import Schema
Schema
.
(alias) class String export String
String
5
})
6
7
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)
Union
(
8
import Schema
Schema
.
function Struct<{ a: typeof Schema.Number; }>(fields: { a: typeof Schema.Number; }): Schema.Struct<{ a: typeof Schema.Number; }> (+1 overload) namespace Struct
Struct
({
(property) a: typeof Schema.Number
a
:
import Schema
Schema
.
(alias) class Number export Number
Number
}), // duplicate key
9
import Schema
Schema
.
function Struct<{ d: typeof Schema.String; }>(fields: { d: typeof Schema.String; }): Schema.Struct<{ d: typeof Schema.String; }> (+1 overload) namespace Struct
Struct
({
(property) d: typeof Schema.String
d
:
import Schema
Schema
.
(alias) class String export String
String
})
10
)
11
12
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

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
)
13
/*
14
throws:
15
Error: Unsupported schema or overlapping types
16
at path: ["a"]
17
details: cannot extend string with number
18
*/

Example (Extending a refinement of Schema.String with another refinement)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const Integer: Schema.brand<typeof Schema.Int, "Int">
Integer
=
import Schema
Schema
.
class Int
Int
.
(method) 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>> ```

brand
("Int"))
4
const
const Positive: Schema.brand<typeof Schema.Positive, "Positive">
Positive
=
import Schema
Schema
.
class Positive
Positive
.
(method) 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>> ```

brand
("Positive"))
5
6
// Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>
7
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<...>
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

extend
(
const Positive: Schema.brand<typeof Schema.Positive, "Positive">
Positive
,
const Integer: Schema.brand<typeof Schema.Int, "Int">
Integer
))
8
9
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const PositiveInteger: Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>
PositiveInteger
)(-1)
10
/*
11
throws
12
ParseError: Int & Brand<"Int">
13
└─ From side refinement failure
14
└─ Positive & Brand<"Positive">
15
└─ Predicate refinement failure
16
└─ Expected Positive & Brand<"Positive">, actual -1
17
*/
18
19
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const PositiveInteger: Schema.Schema<number & Brand<"Positive"> & Brand<"Int">, number, never>
PositiveInteger
)(1.1)
20
/*
21
throws
22
ParseError: Int & Brand<"Int">
23
└─ Predicate refinement failure
24
└─ Expected Int & Brand<"Int">, actual 1.1
25
*/

To rename a property directly during schema creation, you can utilize the Schema.fromKey function. This function is particularly useful when you want to map properties from the input object to different names in the resulting schema object.

Example (Renaming a Required Property)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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) namespace Struct
Struct
({
4
(property) 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`.

propertySignature
(
import Schema
Schema
.
(alias) class String export String
String
).
(method) 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.

fromKey
("c")),
5
(property) b: typeof Schema.Number
b
:
import Schema
Schema
.
(alias) class Number export Number
Number
6
})
7
8
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.PropertySignature<":", string, "c", ":", string, false, never>; b: typeof Schema.Number; }>
schema
)({
(property) c: string
c
: "c",
(property) b: number
b
: 1 }))
9
// Output: { a: "c", b: 1 }

Example (Renaming an Optional Property)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
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, ... 4 more ..., never>; b: typeof Schema.Number; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) 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>
optional
(
import Schema
Schema
.
(alias) class String export String
String
).
(method) 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.

fromKey
("c")),
5
(property) b: typeof Schema.Number
b
:
import Schema
Schema
.
(alias) class Number export Number
Number
6
})
7
8
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>; b: typeof Schema.Number; }>
schema
)({
(property) c: string
c
: "c",
(property) b: number
b
: 1 }))
9
// Output: { b: 1, a: "c" }
10
11
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const schema: Schema.Struct<{ a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>; b: typeof Schema.Number; }>
schema
)({
(property) b: number
b
: 1 }))
12
// Output: { b: 1 }

Note that Schema.optional returns a PropertySignature, which simplifies the process by eliminating the need for explicit Schema.propertySignature usage as required in the previous example.

For existing schemas, the rename API offers a way to systematically change property names across a schema, even within complex structures like unions.

Example (Renaming Properties in a Struct Schema)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
// Original Schema
4
const
const originalSchema: Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>
originalSchema
=
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) namespace Struct
Struct
({
5
(property) c: typeof Schema.String
c
:
import Schema
Schema
.
(alias) class String export String
String
,
6
(property) b: typeof Schema.Number
b
:
import Schema
Schema
.
(alias) class Number export Number
Number
7
})
8
9
// Renaming the "c" property to "a"
10
const
const renamedSchema: Schema.SchemaClass<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>
renamedSchema
=
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: { ...; }) => Schema.SchemaClass<...> (+1 overload)
rename
(
const originalSchema: Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>
originalSchema
, {
(property) c: "a"
c
: "a" })
11
12
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) 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
decodeUnknownSync
(
const renamedSchema: Schema.SchemaClass<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>
renamedSchema
)({
(property) c: string
c
: "c",
(property) b: number
b
: 1 }))
13
// Output: { a: "c", b: 1 }

Example (Renaming Properties in Union Schemas)

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const originalSchema: Schema.Union<[Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>, Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.Boolean; }>]>
originalSchema
=
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)
Union
(
4
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) namespace Struct
Struct
({
5
(property) c: typeof Schema.String
c
:
import Schema
Schema
.
(alias) class String export String
String
,
6
(property) b: typeof Schema.Number
b
:
import Schema
Schema
.
(alias) class Number export Number
Number
7
}),
8
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) namespace Struct
Struct
({
9
(property) c: typeof Schema.String
c
:
import Schema
Schema
.
(alias) class String export String
String
,
10
(property) d: typeof Schema.Boolean
d
:
import Schema
Schema
.
(alias) class Boolean export Boolean
Boolean
11
})
12
)
13
14
// Renaming the "c" property to "a" for all members
15
const
const renamedSchema: 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>
renamedSchema
=
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)
rename
(
const originalSchema: Schema.Union<[Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>, Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.Boolean; }>]>
originalSchema
, {
(property) c: "a"
c
: "a" })
16
17
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) 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<{ ...; } | { ...; }, { ...; } | { ...; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { ...; } | { ...; } export decodeUnknownSync
decodeUnknownSync
(
const renamedSchema: 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>
renamedSchema
)({
(property) c: string
c
: "c",
(property) b: number
b
: 1 }))
18
// Output: { a: "c", b: 1 }
19
20
namespace console var console: Console

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

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

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

log
(
import Schema
Schema
.
(alias) 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<{ ...; } | { ...; }, { ...; } | { ...; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { ...; } | { ...; } export decodeUnknownSync
decodeUnknownSync
(
const renamedSchema: 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>
renamedSchema
)({
(property) c: string
c
: "c",
(property) d: boolean
d
: false }))
21
// Output: { d: false, a: 'c' }

The Schema.suspend function is useful when you need to define a schema that depends on itself, like in the case of recursive data structures.

Example

In this example, the Category schema depends on itself because it has a field subcategories that is an array of Category objects.

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
interface
interface Category
Category
{
4
readonly
(property) Category.name: string
name
: string
5
readonly
(property) Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
6
}
7
8
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.Struct<...> (+1 overload) namespace Struct
Struct
({
9
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
,
10
(property) subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>
subcategories
:
import Schema
Schema
.
(alias) Array<Schema.suspend<Category, Category, never>>(value: Schema.suspend<Category, Category, never>): Schema.Array$<Schema.suspend<Category, Category, never>> export Array
Array
(
11
import Schema
Schema
.
const suspend: <Category, Category, never>(f: () => Schema.Schema<Category, Category, never>) => Schema.suspend<Category, Category, never>
suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never> namespace Schema
Schema
<
interface Category
Category
> =>
const Category: Schema.Struct<{ name: typeof Schema.String; subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>; }>
Category
)
12
)
13
})
1
import {
import Schema
Schema
} from "@effect/schema"
2
3
// @ts-expect-error
4
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) namespace Struct
Struct
({
5
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
,
6
// @ts-expect-error
7
(property) subcategories: Schema.Array$<Schema.suspend<unknown, unknown, unknown>>
subcategories
:
import Schema
Schema
.
(alias) Array<Schema.suspend<unknown, unknown, unknown>>(value: Schema.suspend<unknown, unknown, unknown>): Schema.Array$<Schema.suspend<unknown, unknown, unknown>> export Array
Array
(
import Schema
Schema
.
const suspend: <unknown, unknown, unknown>(f: () => Schema.Schema<unknown, unknown, unknown>) => Schema.suspend<unknown, unknown, unknown>
suspend
(() =>
const Category: any
Category
))
8
})
9
/*
10
'Category' implicitly has type 'any' because it does not have a type annotation and is
11
referenced directly or indirectly in its own initializer.ts(7022)
12
*/

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.

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const fields: { name: typeof Schema.String; }
fields
= {
4
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
5
// ...possibly other fields
6
}
7
8
// Define an interface for the Category schema, extending the Type of the defined fields
9
interface
interface Category
Category
extends
import Schema
Schema
.
namespace Struct
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
Type
<typeof
const fields: { name: typeof Schema.String; }
fields
> {
10
// Define `subcategories` using recursion
11
readonly
(property) Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
12
}
13
14
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) namespace Struct
Struct
({
15
...
const fields: { name: typeof Schema.String; }
fields
, // Include the fields
16
(property) subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>
subcategories
:
import Schema
Schema
.
(alias) Array<Schema.suspend<Category, Category, never>>(value: Schema.suspend<Category, Category, never>): Schema.Array$<Schema.suspend<Category, Category, never>> export Array
Array
(
17
// Define `subcategories` using recursion
18
import Schema
Schema
.
const suspend: <Category, Category, never>(f: () => Schema.Schema<Category, Category, never>) => Schema.suspend<Category, Category, never>
suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never> namespace Schema
Schema
<
interface Category
Category
> =>
const Category: Schema.Struct<{ subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>; name: typeof Schema.String; }>
Category
)
19
)
20
})

Here’s an example of two mutually recursive schemas, Expression and Operation, that represent a simple arithmetic expression tree.

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
interface
interface Expression
Expression
{
4
readonly
(property) Expression.type: "expression"
type
: "expression"
5
readonly
(property) Expression.value: number | Operation
value
: number |
interface Operation
Operation
6
}
7
8
interface
interface Operation
Operation
{
9
readonly
(property) Operation.type: "operation"
type
: "operation"
10
readonly
(property) Operation.operator: "+" | "-"
operator
: "+" | "-"
11
readonly
(property) Operation.left: Expression
left
:
interface Expression
Expression
12
readonly
(property) Operation.right: Expression
right
:
interface Expression
Expression
13
}
14
15
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: { ...; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
16
(property) type: Schema.Literal<["expression"]>
type
:
import Schema
Schema
.
function Literal<["expression"]>(literals_0: "expression"): Schema.Literal<["expression"]> (+2 overloads)
Literal
("expression"),
17
(property) 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)
Union
(
18
import Schema
Schema
.
(alias) class Number export Number
Number
,
19
import Schema
Schema
.
const suspend: <Operation, Operation, never>(f: () => Schema.Schema<Operation, Operation, never>) => Schema.suspend<Operation, Operation, never>
suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never> namespace Schema
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
)
20
)
21
})
22
23
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: { ...; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
24
(property) type: Schema.Literal<["operation"]>
type
:
import Schema
Schema
.
function Literal<["operation"]>(literals_0: "operation"): Schema.Literal<["operation"]> (+2 overloads)
Literal
("operation"),
25
(property) operator: Schema.Literal<["+", "-"]>
operator
:
import Schema
Schema
.
function Literal<["+", "-"]>(literals_0: "+", literals_1: "-"): Schema.Literal<["+", "-"]> (+2 overloads)
Literal
("+", "-"),
26
(property) 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
,
27
(property) 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
28
})

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

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:

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const fields: { id: typeof Schema.NumberFromString; name: typeof Schema.String; }
fields
= {
4
(property) 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".

NumberFromString
,
5
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
6
}
7
8
interface
interface Category
Category
extends
import Schema
Schema
.
namespace Struct
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
Type
<typeof
const fields: { id: typeof Schema.NumberFromString; name: typeof Schema.String; }
fields
> {
9
readonly
(property) Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
10
}
11
12
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: { ...; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
13
...
const fields: { id: typeof Schema.NumberFromString; name: typeof Schema.String; }
fields
,
14
(property) subcategories: Schema.Array$<Schema.suspend<Category, Category, never>>
subcategories
:
import Schema
Schema
.
(alias) Array<Schema.suspend<Category, Category, never>>(value: Schema.suspend<Category, Category, never>): Schema.Array$<Schema.suspend<Category, Category, never>> export Array
Array
(
15
// @ts-expect-error
16
import Schema
Schema
.
const suspend: <Category, Category, never>(f: () => Schema.Schema<Category, Category, never>) => Schema.suspend<Category, Category, never>
suspend
(():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never> namespace Schema
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
)
17
)
18
})
19
/*
20
Type 'Struct<{ subcategories: Array$<suspend<Category, Category, never>>; id: typeof NumberFromString; name: typeof String$; }>' is not assignable to type 'Schema<Category, Category, never>'.
21
The types of 'Encoded.id' are incompatible between these types.
22
Type 'string' is not assignable to type 'number'.ts(2322)
23
*/

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:

1
import {
import Schema
Schema
} from "@effect/schema"
2
3
const
const fields: { id: typeof Schema.NumberFromString; name: typeof Schema.String; }
fields
= {
4
(property) 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".

NumberFromString
,
5
(property) name: typeof Schema.String
name
:
import Schema
Schema
.
(alias) class String export String
String
6
}
7
8
interface
interface Category
Category
extends
import Schema
Schema
.
namespace Struct
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
Type
<typeof
const fields: { id: typeof Schema.NumberFromString; name: typeof Schema.String; }
fields
> {
9
readonly
(property) Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
10
}
11
12
interface
interface CategoryEncoded
CategoryEncoded
extends
import Schema
Schema
.
namespace Struct
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<...>; }
Encoded
<typeof
const fields: { id: typeof Schema.NumberFromString; name: typeof Schema.String; }
fields
> {
13
readonly
(property) CategoryEncoded.subcategories: readonly CategoryEncoded[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface CategoryEncoded
CategoryEncoded
>
14
}
15
16
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: { ...; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
17
...
const fields: { id: typeof Schema.NumberFromString; name: typeof Schema.String; }
fields
,
18
(property) subcategories: Schema.Array$<Schema.suspend<Category, CategoryEncoded, never>>
subcategories
:
import Schema
Schema
.
(alias) Array<Schema.suspend<Category, CategoryEncoded, never>>(value: Schema.suspend<Category, CategoryEncoded, never>): Schema.Array$<...> export Array
Array
(
19
import Schema
Schema
.
const suspend: <Category, CategoryEncoded, never>(f: () => Schema.Schema<Category, CategoryEncoded, never>) => Schema.suspend<Category, CategoryEncoded, never>
suspend
(
20
():
import Schema
Schema
.
interface Schema<in out A, in out I = A, out R = never> namespace Schema
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
21
)
22
)
23
})