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

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

Example (Declaring a Schema with Annotations)

1
import {
import Schema
Schema
} from "effect"
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"
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: <Schema.Schema<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 (Attempting to Generate Arbitrary Values Without Required Annotations)

1
import {
import Arbitrary
Arbitrary
,
import Schema
Schema
} from "effect"
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 Annotation for Custom File Schema)

1
import {
import Arbitrary
Arbitrary
,
import FastCheck
FastCheck
,
import Pretty
Pretty
,
import Schema
Schema
} from "effect"
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?: Arbitrary.ArbitraryAnnotation<File, readonly []>
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
*/

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

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

Example (Structural Typing Issue)

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

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

Example (Adding Annotations to a Property Signature)

1
import {
import Schema
Schema
} from "effect"
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" // Annotation to label the age field
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"
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>

The syntax:

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

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

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

Example (Defining an Optional Number Field)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>(fields: { quantity: Schema.optional<typeof Schema.NumberFromString>; }): Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optional<typeof Schema.NumberFromString>
quantity
:
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 quantity?: string | undefined; }
8
// ▼
9
type
type Encoded = { readonly quantity?: string | undefined; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
.
(property) Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>.Encoded: { readonly quantity?: string | undefined; }
Encoded
10
11
// ┌─── { readonly quantity?: number | undefined; }
12
// ▼
13
type
type Type = { readonly quantity?: number | undefined; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
.
(property) Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>.Type: { readonly quantity?: number | undefined; }
Type
14
15
// Decoding examples
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 quantity?: number | undefined; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
)({
(property) quantity: string
quantity
: "1" }))
18
// Output: { quantity: 1 }
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 quantity?: number | undefined; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
)({}))
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) decodeUnknownSync<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
22
// Output: { quantity: undefined }
23
24
// Encoding examples
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 quantity?: number | undefined; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (a: { readonly quantity?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | undefined; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
)({
(property) quantity?: number | undefined
quantity
: 1 }))
27
// Output: { quantity: "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 quantity?: number | undefined; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (a: { readonly quantity?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | undefined; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
)({}))
29
// Output: {}
30
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 quantity?: number | undefined; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (a: { readonly quantity?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | undefined; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
)({
(property) quantity?: number | undefined
quantity
:
var undefined
undefined
}))
31
// Output: { quantity: undefined }

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>(fields: { quantity: Schema.optional<typeof Schema.NumberFromString>; }): Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optional<typeof Schema.NumberFromString>
quantity
:
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
// ┌─── typeof Schema.NumberFromString
8
// ▼
9
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.NumberFromString>; }>
Product
.
(property) TypeLiteral<{ quantity: optional<typeof NumberFromString>; }, []>.fields: { readonly quantity: Schema.optional<typeof Schema.NumberFromString>; }
fields
.
(property) quantity: Schema.optional<typeof Schema.NumberFromString>
quantity
.
(property) optional<typeof NumberFromString>.from: typeof Schema.NumberFromString
from

The syntax:

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

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

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

Example (Handling Null as Missing Value)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { nullable: true; }>(self: typeof Schema.NumberFromString, options: { nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }> (+1 overload)
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) nullable: true
nullable
: true
6
})
7
})
8
9
// ┌─── { readonly quantity?: string | null | undefined; }
10
// ▼
11
type
type Encoded = { readonly quantity?: string | null | undefined; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>.Encoded: { readonly quantity?: string | null | undefined; }
Encoded
12
13
// ┌─── { readonly quantity?: number | undefined; }
14
// ▼
15
type
type Type = { readonly quantity?: number | undefined; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>.Type: { readonly quantity?: number | undefined; }
Type
16
17
// Decoding examples
18
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 quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "1" }))
20
// Output: { quantity: 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) decodeUnknownSync<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
)({}))
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) decodeUnknownSync<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
24
// Output: { quantity: undefined }
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) decodeUnknownSync<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number | undefined; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
)({
(property) quantity: null
quantity
: null }))
26
// Output: {}
27
28
// Encoding examples
29
30
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 quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (a: { readonly quantity?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | null | undefined; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
)({
(property) quantity?: number | undefined
quantity
: 1 }))
31
// Output: { quantity: "1" }
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
(
import Schema
Schema
.
(alias) encodeSync<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (a: { readonly quantity?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | null | undefined; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
)({}))
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
(
import Schema
Schema
.
(alias) encodeSync<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity?: number | undefined; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (a: { readonly quantity?: number | undefined; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | null | undefined; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
)({
(property) quantity?: number | undefined
quantity
:
var undefined
undefined
}))
35
// Output: { quantity: undefined }

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { nullable: true; }>(self: typeof Schema.NumberFromString, options: { nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }> (+1 overload)
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) nullable: true
nullable
: true
6
})
7
})
8
9
// ┌─── typeof Schema.NumberFromString
10
// ▼
11
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { nullable: true; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { nullable: true; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { nullable: true; }>.from: typeof Schema.NumberFromString
from

The syntax:

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

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

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

Example (Using Exactness with Optional Field)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { exact: true; }>(self: typeof Schema.NumberFromString, options: { exact: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }> (+1 overload)
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 quantity?: string; }
8
// ▼
9
type
type Encoded = { readonly quantity?: string; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>.Encoded: { readonly quantity?: string; }
Encoded
10
11
// ┌─── { readonly quantity?: number; }
12
// ▼
13
type
type Type = { readonly quantity?: number; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>.Type: { readonly quantity?: number; }
Type
14
15
// Decoding examples
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 quantity?: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "1" }))
18
// Output: { quantity: 1 }
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 quantity?: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
)({}))
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) decodeUnknownSync<{ readonly quantity?: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
22
/*
23
throws:
24
ParseError: { readonly quantity?: NumberFromString }
25
└─ ["quantity"]
26
└─ NumberFromString
27
└─ Encoded side transformation failure
28
└─ Expected string, actual undefined
29
*/
30
31
// Encoding examples
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) encodeSync<{ readonly quantity?: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (a: { readonly quantity?: number; }, overrideOptions?: ParseOptions) => { readonly quantity?: string; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
)({
(property) quantity?: number
quantity
: 1 }))
34
// Output: { quantity: "1" }
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 quantity?: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (a: { readonly quantity?: number; }, overrideOptions?: ParseOptions) => { readonly quantity?: string; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
)({}))
36
// Output: {}

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { exact: true; }>(self: typeof Schema.NumberFromString, options: { exact: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }> (+1 overload)
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
// ┌─── typeof Schema.NumberFromString
8
// ▼
9
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { exact: true; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { exact: true; }>.from: typeof Schema.NumberFromString
from

The syntax:

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

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

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

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

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { exact: true; nullable: true; }>(self: typeof Schema.NumberFromString, options: { exact: true; nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }> (+1 overload)
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 quantity?: string | null; }
11
// ▼
12
type
type Encoded = { readonly quantity?: string | null; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>.Encoded: { readonly quantity?: string | null; }
Encoded
13
14
// ┌─── { readonly quantity?: number; }
15
// ▼
16
type
type Type = { readonly quantity?: number; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>.Type: { readonly quantity?: number; }
Type
17
18
// Decoding examples
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 quantity?: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "1" }))
21
// Output: { quantity: 1 }
22
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 quantity?: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
)({}))
23
// Output: {}
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity?: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
25
/*
26
throws:
27
ParseError: (Struct (Encoded side) <-> Struct (Type side))
28
└─ Encoded side transformation failure
29
└─ Struct (Encoded side)
30
└─ ["quantity"]
31
└─ NumberFromString | null
32
├─ NumberFromString
33
│ └─ Encoded side transformation failure
34
│ └─ Expected string, actual undefined
35
└─ Expected null, actual undefined
36
*/
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) decodeUnknownSync<{ readonly quantity?: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity?: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: null
quantity
: null }))
38
// Output: {}
39
40
// Encoding examples
41
42
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 quantity?: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (a: { readonly quantity?: number; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | null; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
)({
(property) quantity?: number
quantity
: 1 }))
43
// Output: { quantity: "1" }
44
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 quantity?: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity?: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (a: { readonly quantity?: number; }, overrideOptions?: ParseOptions) => { readonly quantity?: string | null; } export encodeSync
encodeSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
)({}))
45
// Output: {}

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { exact: true; nullable: true; }>(self: typeof Schema.NumberFromString, options: { exact: true; nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }> (+1 overload)
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
// ┌─── typeof Schema.NumberFromString
11
// ▼
12
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { exact: true; nullable: true; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { exact: true; nullable: true; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { exact: true; nullable: true; }>.from: typeof Schema.NumberFromString
from

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

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

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

Example (exactOptionalPropertyTypes: false)

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

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.Never>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optional<typeof Schema.Never>; }>(fields: { quantity: Schema.optional<typeof Schema.Never>; }): Schema.Struct<{ quantity: Schema.optional<typeof Schema.Never>; }> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optional<typeof Schema.Never>
quantity
:
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 quantity?: undefined; }
8
// ▼
9
type
type Encoded = { readonly quantity?: undefined; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.Never>; }>
Product
.
(property) Schema<{ readonly quantity?: undefined; }, { readonly quantity?: undefined; }, never>.Encoded: { readonly quantity?: undefined; }
Encoded
10
11
// ┌─── { readonly quantity?: undefined; }
12
// ▼
13
type
type Type = { readonly quantity?: undefined; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optional<typeof Schema.Never>; }>
Product
.
(property) Schema<{ readonly quantity?: undefined; }, { readonly quantity?: undefined; }, never>.Type: { readonly quantity?: undefined; }
Type

Example (exactOptionalPropertyTypes: true)

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

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }): Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.Never, { exact: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.Never, { exact: true; }>(self: typeof Schema.Never, options: { exact: true; }) => Schema.optionalWith<typeof Schema.Never, { exact: true; }> (+1 overload)
optionalWith
(
import Schema
Schema
.
class Never
Never
, {
(property) exact: true
exact
: true })
5
})
6
7
// ┌─── { readonly quantity?: never; }
8
// ▼
9
type
type Encoded = { readonly quantity?: never; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: never; }, { readonly quantity?: never; }, never>.Encoded: { readonly quantity?: never; }
Encoded
10
11
// ┌─── { readonly quantity?: never; }
12
// ▼
13
type
type Type = { readonly quantity?: never; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.Never, { exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity?: never; }, { readonly quantity?: never; }, never>.Type: { readonly quantity?: never; }
Type

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

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

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

Syntax

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

Example (Applying Default When Field Is Missing or undefined)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(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
, {
5
(property) default: () => number
default
: () => 1 // Default value for quantity
6
})
7
})
8
9
// ┌─── { readonly quantity?: string | undefined; }
10
// ▼
11
type
type Encoded = { readonly quantity?: string | undefined; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }, never>.Encoded: { readonly quantity?: string | undefined; }
Encoded
12
13
// ┌─── { readonly quantity: number; }
14
// ▼
15
type
type Type = { readonly quantity: number; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }, never>.Type: { readonly quantity: number; }
Type
16
17
// Decoding examples with default applied
18
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 quantity: number; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
)({}))
20
// Output: { quantity: 1 }
21
22
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 quantity: number; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
23
// Output: { quantity: 1 }
24
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) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
26
// Output: { quantity: 2 }
27
28
// Object construction examples with default applied
29
30
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<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
.
(method) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.make(props: { readonly quantity?: number; }, options?: MakeOptions): { readonly quantity: number; }
make
({}))
31
// Output: { quantity: 1 }
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
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
.
(method) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.make(props: { readonly quantity?: number; }, options?: MakeOptions): { readonly quantity: number; }
make
({
(property) quantity?: number
quantity
: 2 }))
34
// Output: { quantity: 2 }

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(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
, {
5
(property) default: () => number
default
: () => 1 // Default value for quantity
6
})
7
})
8
9
// ┌─── typeof Schema.NumberFromString
10
// ▼
11
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { default: () => number; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { default: () => number; }>.from: typeof Schema.NumberFromString
from

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

Syntax

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

Example (Applying Default Only When Field Is Missing)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { default: () => number; exact: true; }>(self: typeof Schema.NumberFromString, options: { default: () => number; exact: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }> (+1 overload)
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) default: () => number
default
: () => 1, // Default value for quantity
6
(property) exact: true
exact
: true // Only apply default if quantity is not provided
7
})
8
})
9
10
// ┌─── { readonly quantity?: string; }
11
// ▼
12
type
type Encoded = { readonly quantity?: string; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string; }, never>.Encoded: { readonly quantity?: string; }
Encoded
13
14
// ┌─── { readonly quantity: number; }
15
// ▼
16
type
type Type = { readonly quantity: number; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string; }, never>.Type: { readonly quantity: number; }
Type
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }>
Product
)({}))
19
// Output: { quantity: 1 }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
22
// Output: { quantity: 2 }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
25
/*
26
throws:
27
ParseError: (Struct (Encoded side) <-> Struct (Type side))
28
└─ Encoded side transformation failure
29
└─ Struct (Encoded side)
30
└─ ["quantity"]
31
└─ NumberFromString
32
└─ Encoded side transformation failure
33
└─ Expected string, actual undefined
34
*/

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

Syntax

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

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

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { default: () => number; nullable: true; }>(self: typeof Schema.NumberFromString, options: { default: () => number; nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }> (+1 overload)
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) default: () => number
default
: () => 1, // Default value for quantity
6
(property) nullable: true
nullable
: true // Apply default if quantity is null
7
})
8
})
9
10
// ┌─── { readonly quantity?: string | null | undefined; }
11
// ▼
12
type
type Encoded = { readonly quantity?: string | null | undefined; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>.Encoded: { readonly quantity?: string | null | undefined; }
Encoded
13
14
// ┌─── { readonly quantity: number; }
15
// ▼
16
type
type Type = { readonly quantity: number; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>.Type: { readonly quantity: number; }
Type
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>
Product
)({}))
19
// Output: { quantity: 1 }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
22
// Output: { quantity: 1 }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>
Product
)({
(property) quantity: null
quantity
: null }))
25
// Output: { quantity: 1 }
26
27
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 quantity: number; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; nullable: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
28
// Output: { quantity: 2 }

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

Syntax

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

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

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>(self: typeof Schema.NumberFromString, options: { default: () => number; exact: true; nullable: true; }) => Schema.optionalWith<...> (+1 overload)
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) default: () => number
default
: () => 1, // Default value for quantity
6
(property) exact: true
exact
: true, // Only apply default if quantity is not provided
7
(property) nullable: true
nullable
: true // Apply default if quantity is null
8
})
9
})
10
11
// ┌─── { readonly quantity?: string | null; }
12
// ▼
13
type
type Encoded = { readonly quantity?: string | null; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>.Encoded: { readonly quantity?: string | null; }
Encoded
14
15
// ┌─── { readonly quantity: number; }
16
// ▼
17
type
type Type = { readonly quantity: number; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>.Type: { readonly quantity: number; }
Type
18
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 quantity: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>
Product
)({}))
20
// Output: { quantity: 1 }
21
22
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 quantity: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: null
quantity
: null }))
23
// Output: { quantity: 1 }
24
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) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
26
// Output: { quantity: 2 }
27
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) decodeUnknownSync<{ readonly quantity: number; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: number; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: number; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { default: () => number; exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
29
/*
30
throws:
31
ParseError: (Struct (Encoded side) <-> Struct (Type side))
32
└─ Encoded side transformation failure
33
└─ Struct (Encoded side)
34
└─ ["quantity"]
35
└─ NumberFromString
36
└─ Encoded side transformation failure
37
└─ Expected string, actual undefined
38
*/

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

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

Syntax

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

Example (Handling Optional Field as Option)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(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" })
5
})
6
7
// ┌─── { readonly quantity?: string | undefined; }
8
// ▼
9
type
type Encoded = { readonly quantity?: string | undefined; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }, never>.Encoded: { readonly quantity?: string | undefined; }
Encoded
10
11
// ┌─── { readonly quantity: Option<number>; }
12
// ▼
13
type
type Type = { readonly quantity: Option<number>; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }, never>.Type: { readonly quantity: Option<number>; }
Type
14
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 quantity: Option<number>; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
)({}))
16
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
19
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
22
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(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" })
5
})
6
7
// ┌─── typeof Schema.NumberFromString
8
// ▼
9
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { as: "Option"; }>.from: typeof Schema.NumberFromString
from

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

Syntax

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

Example (Using Exactness with Optional Field as Option)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { as: "Option"; exact: true; }>(self: typeof Schema.NumberFromString, options: { as: "Option"; exact: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }> (+1 overload)
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) as: "Option"
as
: "Option",
6
(property) exact: true
exact
: true
7
})
8
})
9
10
// ┌─── { readonly quantity?: string; }
11
// ▼
12
type
type Encoded = { readonly quantity?: string; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }, never>.Encoded: { readonly quantity?: string; }
Encoded
13
14
// ┌─── { readonly quantity: Option<number>; }
15
// ▼
16
type
type Type = { readonly quantity: Option<number>; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }, never>.Type: { readonly quantity: Option<number>; }
Type
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
)({}))
19
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
22
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
25
/*
26
throws:
27
ParseError: (Struct (Encoded side) <-> Struct (Type side))
28
└─ Encoded side transformation failure
29
└─ Struct (Encoded side)
30
└─ ["quantity"]
31
└─ NumberFromString
32
└─ Encoded side transformation failure
33
└─ Expected string, actual undefined
34
*/

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { as: "Option"; exact: true; }>(self: typeof Schema.NumberFromString, options: { as: "Option"; exact: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }> (+1 overload)
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) as: "Option"
as
: "Option",
6
(property) exact: true
exact
: true
7
})
8
})
9
10
// ┌─── typeof Schema.NumberFromString
11
// ▼
12
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; exact: true; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { as: "Option"; exact: true; }>.from: typeof Schema.NumberFromString
from

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

Syntax

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

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

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>(self: typeof Schema.NumberFromString, options: { as: "Option"; nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }> (+1 overload)
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) as: "Option"
as
: "Option",
6
(property) nullable: true
nullable
: true
7
})
8
})
9
10
// ┌─── { readonly quantity?: string | null | undefined; }
11
// ▼
12
type
type Encoded = { readonly quantity?: string | null | undefined; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>.Encoded: { readonly quantity?: string | null | undefined; }
Encoded
13
14
// ┌─── { readonly quantity: Option<number>; }
15
// ▼
16
type
type Type = { readonly quantity: Option<number>; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>.Type: { readonly quantity: Option<number>; }
Type
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
)({}))
19
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
22
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
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
(
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
)({
(property) quantity: null
quantity
: null }))
25
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
26
27
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 quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null | undefined; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
28
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>(self: typeof Schema.NumberFromString, options: { as: "Option"; nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }> (+1 overload)
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) as: "Option"
as
: "Option",
6
(property) nullable: true
nullable
: true
7
})
8
})
9
10
// ┌─── typeof Schema.NumberFromString
11
// ▼
12
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; nullable: true; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; nullable: true; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { as: "Option"; nullable: true; }>.from: typeof Schema.NumberFromString
from

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

Syntax

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

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

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>(self: typeof Schema.NumberFromString, options: { as: "Option"; exact: true; nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }> (+1 overload)
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) as: "Option"
as
: "Option",
6
(property) exact: true
exact
: true,
7
(property) nullable: true
nullable
: true
8
})
9
})
10
11
// ┌─── { readonly quantity?: string | null; }
12
// ▼
13
type
type Encoded = { readonly quantity?: string | null; }
Encoded
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>.Encoded: { readonly quantity?: string | null; }
Encoded
14
15
// ┌─── { readonly quantity: Option<number>; }
16
// ▼
17
type
type Type = { readonly quantity: Option<number>; }
Type
= typeof
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
.
(property) Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>.Type: { readonly quantity: Option<number>; }
Type
18
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 quantity: Option<number>; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
)({}))
20
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
21
22
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 quantity: Option<number>; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: null
quantity
: null }))
23
// Output: { quantity: { _id: 'Option', _tag: 'None' } }
24
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) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: string
quantity
: "2" }))
26
// Output: { quantity: { _id: 'Option', _tag: 'Some', value: 2 } }
27
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) decodeUnknownSync<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }>(schema: Schema.Schema<{ readonly quantity: Option<number>; }, { readonly quantity?: string | null; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly quantity: Option<number>; } export decodeUnknownSync
decodeUnknownSync
(
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
)({
(property) quantity: undefined
quantity
:
var undefined
undefined
}))
29
/*
30
throws:
31
ParseError: (Struct (Encoded side) <-> Struct (Type side))
32
└─ Encoded side transformation failure
33
└─ Struct (Encoded side)
34
└─ ["quantity"]
35
└─ NumberFromString
36
└─ Encoded side transformation failure
37
└─ Expected string, actual undefined
38
*/

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

Example (Accessing the Original Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
=
import Schema
Schema
.
function Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>(fields: { quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
4
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>
quantity
:
import Schema
Schema
.
const optionalWith: <typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>(self: typeof Schema.NumberFromString, options: { as: "Option"; exact: true; nullable: true; }) => Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }> (+1 overload)
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) as: "Option"
as
: "Option",
6
(property) exact: true
exact
: true,
7
(property) nullable: true
nullable
: true
8
})
9
})
10
11
// ┌─── typeof Schema.NumberFromString
12
// ▼
13
const
const from: typeof Schema.NumberFromString
from
=
const Product: Schema.Struct<{ quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }>
Product
.
(property) TypeLiteral<{ quantity: optionalWith<typeof NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }, []>.fields: { readonly quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>; }
fields
.
(property) quantity: Schema.optionalWith<typeof Schema.NumberFromString, { as: "Option"; exact: true; nullable: true; }>
quantity
.
(property) optionalWith<typeof NumberFromString, { as: "Option"; exact: true; nullable: true; }>.from: typeof Schema.NumberFromString
from

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

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

Syntax

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

In this function:

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

Example (Omitting Empty Strings from Output)

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

1
import {
import Schema
Schema
} from "effect"
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<{ nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>
schema
=
import Schema
Schema
.
function Struct<{ nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>(fields: { nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
5
(property) nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>
nonEmpty
:
import Schema
Schema
.
const optionalToOptional: <string, string, never, string, string, never>(from: Schema.Schema<string, string, never>, to: Schema.Schema<string, string, never>, options: { readonly decode: (o: Option.Option<string>) => Option.Option<...>; readonly encode: (o: Option.Option<...>) => Option.Option<...>; }) => Schema.PropertySignature<...>

Converts an optional property to another optional property through a transformation `Option -> Option`. - `decode`: - `none` as argument means the value is missing in the input. - `none` as return value means the value will be missing in the output. - `encode`: - `none` as argument means the value is missing in the input. - `none` as return value means the value will be missing in the output.

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,
9
// return Option.none() to omit it in the output
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
// Treat empty strings as missing in the output
15
// by returning Option.none()
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
// Non-empty strings are included in the output
19
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
)
20
},
21
// Here in the encoding part, we can decide to handle things
22
// in the same way as in the decoding phase or handle them differently.
23
// For example, we can leave everything unchanged
24
// and use the identity function
25
(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
26
})
27
})
28
29
// Decoding examples
30
31
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
decode
=
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly nonEmpty?: string; }, { readonly nonEmpty?: string; }>(schema: Schema.Schema<{ readonly nonEmpty?: string; }, { readonly nonEmpty?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>
schema
)
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
decode
({}))
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
decode
({
(property) nonEmpty: string
nonEmpty
: "" }))
36
// Output: {}
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
decode
({
(property) nonEmpty: string
nonEmpty
: "a non-empty string" }))
38
// Output: { nonEmpty: 'a non-empty string' }
39
40
// Encoding examples
41
42
const
const encode: (a: { readonly nonEmpty?: string; }, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
encode
=
import Schema
Schema
.
(alias) encodeSync<{ readonly nonEmpty?: string; }, { readonly nonEmpty?: string; }>(schema: Schema.Schema<{ readonly nonEmpty?: string; }, { readonly nonEmpty?: string; }, never>, options?: ParseOptions): (a: { readonly nonEmpty?: string; }, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ nonEmpty: Schema.PropertySignature<"?:", string, never, "?:", string, false, never>; }>
schema
)
43
44
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 nonEmpty?: string; }, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
encode
({}))
45
// Output: {}
46
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 nonEmpty?: string; }, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
encode
({
(property) nonEmpty?: string
nonEmpty
: "" }))
47
// Output: { nonEmpty: '' }
48
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 nonEmpty?: string; }, overrideOptions?: ParseOptions) => { readonly nonEmpty?: string; }
encode
({
(property) nonEmpty?: string
nonEmpty
: "a non-empty string" }))
49
// Output: { nonEmpty: 'a non-empty string' }

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

Syntax

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

In this function:

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

Example (Setting a Default Value for a Missing Field)

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

1
import {
import Schema
Schema
} from "effect"
2
import {
import Option
Option
} from "effect"
3
4
const
const schema: Schema.Struct<{ maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>
schema
=
import Schema
Schema
.
function Struct<{ maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>(fields: { maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }): Schema.Struct<...> (+1 overload) namespace Struct
Struct
({
5
(property) maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>
maybe
:
import Schema
Schema
.
const optionalToRequired: <string, string, never, string, string, never>(from: Schema.Schema<string, string, never>, to: Schema.Schema<string, string, never>, options: { readonly decode: (o: Option.Option<string>) => string; readonly encode: (ti: string) => Option.Option<...>; }) => Schema.PropertySignature<...>

Converts an optional property to a required one through a transformation `Option -> Type`. - `decode`: `none` as argument means the value is missing in the input. - `encode`: `none` as return value means the value will be missing in the output.

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
// Assign a default value for the output
9
// when the field is absent in the input
10
return "default value"
11
}
12
// Use the input's value for the output if the field is present
13
return
(parameter) input: Option.Some<string>
input
.
(property) Some<string>.value: string
value
14
},
15
// During encoding, we can choose to handle things differently,
16
// or simply return the same value present in the input for the output
17
(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
)
18
})
19
})
20
21
// Decoding examples
22
23
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly maybe: string; }
decode
=
import Schema
Schema
.
(alias) decodeUnknownSync<{ readonly maybe: string; }, { readonly maybe?: string; }>(schema: Schema.Schema<{ readonly maybe: string; }, { readonly maybe?: string; }, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => { readonly maybe: string; } export decodeUnknownSync
decodeUnknownSync
(
const schema: Schema.Struct<{ maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>
schema
)
24
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly maybe: string; }
decode
({}))
26
// Output: { maybe: 'default value' }
27
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 maybe: string; }
decode
({
(property) maybe: string
maybe
: "present value" }))
28
// Output: { maybe: 'present value' }
29
30
// Encoding examples
31
32
const
const encode: (a: { readonly maybe: string; }, overrideOptions?: ParseOptions) => { readonly maybe?: string; }
encode
=
import Schema
Schema
.
(alias) encodeSync<{ readonly maybe: string; }, { readonly maybe?: string; }>(schema: Schema.Schema<{ readonly maybe: string; }, { readonly maybe?: string; }, never>, options?: ParseOptions): (a: { readonly maybe: string; }, overrideOptions?: ParseOptions) => { readonly maybe?: string; } export encodeSync
encodeSync
(
const schema: Schema.Struct<{ maybe: Schema.PropertySignature<":", string, never, "?:", string, false, never>; }>
schema
)
33
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 encode: (a: { readonly maybe: string; }, overrideOptions?: ParseOptions) => { readonly maybe?: string; }
encode
({
(property) maybe: string
maybe
: "present value" }))
35
// Output: { maybe: 'present value' }

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

Syntax

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

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

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

Example (Handling Empty String as Missing Value)

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

1
import {
import Schema
Schema
} from "effect"
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
// Treat empty string as a missing value
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
// Provide an empty string as default if the field is missing
9
(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
(() => "")
10
})
11
})
12
13
// Decoding examples
14
15
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
)
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly name?: string; }
decode
({
(property) name: string
name
: "John" }))
18
// Output: { name: 'John' }
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
(
const decode: (u: unknown, overrideOptions?: ParseOptions) => { readonly name?: string; }
decode
({
(property) name: string
name
: "" }))
20
// Output: {}
21
22
// Encoding examples
23
24
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
)
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
(
const encode: (a: { readonly name?: string; }, overrideOptions?: ParseOptions) => { readonly name: string; }
encode
({
(property) name?: string
name
: "John" }))
27
// Output: { name: 'John' }
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
(
const encode: (a: { readonly name?: string; }, overrideOptions?: ParseOptions) => { readonly name: string; }
encode
({}))
29
// Output: { name: '' }

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

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

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

Example (Adding New Fields)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Orginal: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Orginal
=
import Schema
Schema
.
function Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>(fields: { a: typeof Schema.String; b: typeof Schema.String; }): Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }> (+1 overload) 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 Orginal: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Orginal
.
(property) TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: { readonly a: typeof Schema.String; readonly b: typeof Schema.String; }
fields
,
10
// Adding new 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
})
14
15
// ┌─── {
16
// | readonly a: string;
17
// | readonly b: string;
18
// | readonly c: string;
19
// | readonly d: string;
20
// | }
21
// ▼
22
type
type Type = { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }
Type
= typeof
const Extended: Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.String; a: typeof Schema.String; b: typeof Schema.String; }>
Extended
.
(property) Schema<{ readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, never>.Type: { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }
Type

Example (Adding Additional Index Signatures)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Original: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Original
=
import Schema
Schema
.
function Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>(fields: { a: typeof Schema.String; b: typeof Schema.String; }): Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }> (+1 overload) 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 Original: Schema.Struct<{ a: typeof Schema.String; b: typeof Schema.String; }>
Original
.
(property) TypeLiteral<{ a: typeof String$; b: typeof String$; }, []>.fields: { readonly a: typeof Schema.String; readonly b: typeof Schema.String; }
fields
,
10
// Adding an index signature
11
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
})
12
)
13
14
// ┌─── {
15
// │ readonly [x: string]: string;
16
// | readonly a: string;
17
// | readonly b: string;
18
// | }
19
// ▼
20
type
type Type = { readonly [x: string]: string; readonly a: string; readonly b: string; }
Type
= typeof
const Extended: Schema.TypeLiteral<{ readonly a: typeof Schema.String; readonly b: typeof Schema.String; }, readonly [Schema.Record$<typeof Schema.String, typeof Schema.String>]>
Extended
.
(property) Schema<{ readonly [x: string]: string; readonly a: string; readonly b: string; }, { readonly [x: string]: string; readonly a: string; readonly b: string; }, never>.Type: { readonly [x: string]: string; readonly a: string; readonly b: string; }
Type

Example (Combining Fields from Multiple Structs)

1
import {
import Schema
Schema
} from "effect"
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
})
17
18
// ┌─── {
19
// | readonly a: string;
20
// | readonly b: string;
21
// | readonly c: string;
22
// | readonly d: string;
23
// | }
24
// ▼
25
type
type Type = { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }
Type
= typeof
const Extended: Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.String; a: typeof Schema.String; b: typeof Schema.String; }>
Extended
.
(property) Schema<{ readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }, never>.Type: { readonly a: string; readonly b: string; readonly c: string; readonly d: string; }
Type

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

Supported extensions include:

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

Example (Extending a Struct with a Union of Structs)

1
import {
import Schema
Schema
} from "effect"
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
)
13
14
// ┌─── {
15
// | readonly a: string;
16
// | } & ({
17
// | readonly b: string;
18
// | } | {
19
// | readonly c: string;
20
// | })
21
// ▼
22
type
type Type = { readonly a: string; } & ({ readonly b: string; } | { readonly c: string; })
Type
= typeof
const Extended: Schema.extend<Schema.Struct<{ a: typeof Schema.String; }>, Schema.Union<[Schema.Struct<{ b: typeof Schema.String; }>, Schema.Struct<{ c: typeof Schema.String; }>]>>
Extended
.
(property) Schema<{ readonly a: string; } & ({ readonly b: string; } | { readonly c: string; }), { readonly a: string; } & ({ readonly b: string; } | { readonly c: string; }), never>.Type: { readonly a: string; } & ({ readonly b: string; } | { readonly c: string; })
Type

Example (Attempting to Extend Structs with Conflicting Fields)

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

1
import {
import Schema
Schema
} from "effect"
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
}), // conflicting type for key "a"
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 (xtending a Refinement with Another Refinement)

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

1
import {
import Schema
Schema
} from "effect"
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<number & Brand<"Positive"> & Brand<"Int">, number, never>
7
// ▼
8
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
))
9
10
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)
11
/*
12
throws
13
ParseError: Int & Brand<"Int">
14
└─ From side refinement failure
15
└─ Positive & Brand<"Positive">
16
└─ Predicate refinement failure
17
└─ Expected Positive & Brand<"Positive">, actual -1
18
*/
19
20
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)
21
/*
22
throws
23
ParseError: Int & Brand<"Int">
24
└─ Predicate refinement failure
25
└─ Expected Int & Brand<"Int">, actual 1.1
26
*/

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

Example (Renaming a Required Property)

1
import {
import Schema
Schema
} from "effect"
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
// ┌─── { readonly c: string; readonly b: number; }
9
// ▼
10
type
type Encoded = { readonly c: string; readonly b: number; }
Encoded
= typeof
const schema: Schema.Struct<{ a: Schema.PropertySignature<":", string, "c", ":", string, false, never>; b: typeof Schema.Number; }>
schema
.
(property) Schema<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>.Encoded: { readonly c: string; readonly b: number; }
Encoded
11
12
// ┌─── { readonly a: string; readonly b: number; }
13
// ▼
14
type
type Type = { readonly a: string; readonly b: number; }
Type
= typeof
const schema: Schema.Struct<{ a: Schema.PropertySignature<":", string, "c", ":", string, false, never>; b: typeof Schema.Number; }>
schema
.
(property) Schema<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>.Type: { readonly a: string; readonly b: number; }
Type
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: 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 }))
17
// Output: { a: "c", b: 1 }

Example (Renaming an Optional Property)

1
import {
import Schema
Schema
} from "effect"
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
// ┌─── { readonly b: number; readonly c?: string | undefined; }
9
// ▼
10
type
type Encoded = { readonly b: number; readonly c?: string | undefined; }
Encoded
= typeof
const schema: Schema.Struct<{ a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>; b: typeof Schema.Number; }>
schema
.
(property) Schema<{ readonly a?: string | undefined; readonly b: number; }, { readonly b: number; readonly c?: string | undefined; }, never>.Encoded: { readonly b: number; readonly c?: string | undefined; }
Encoded
11
12
// ┌─── { readonly a?: string | undefined; readonly b: number; }
13
// ▼
14
type
type Type = { readonly a?: string | undefined; readonly b: number; }
Type
= typeof
const schema: Schema.Struct<{ a: Schema.PropertySignature<"?:", string | undefined, "c", "?:", string | undefined, false, never>; b: typeof Schema.Number; }>
schema
.
(property) Schema<{ readonly a?: string | undefined; readonly b: number; }, { readonly b: number; readonly c?: string | undefined; }, never>.Type: { readonly a?: string | undefined; readonly b: number; }
Type
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?: 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 }))
17
// Output: { a: 'c', b: 1 }
18
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?: 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 }))
20
// Output: { b: 1 }

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

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

Example (Renaming Properties in a Struct Schema)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Original: Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>
Original
=
import Schema
Schema
.
function Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>(fields: { c: typeof Schema.String; b: typeof Schema.Number; }): Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }> (+1 overload) namespace Struct
Struct
({
4
(property) c: typeof Schema.String
c
:
import Schema
Schema
.
(alias) class String export String
String
,
5
(property) b: typeof Schema.Number
b
:
import Schema
Schema
.
(alias) class Number export Number
Number
6
})
7
8
// Renaming the "c" property to "a"
9
//
10
//
11
// ┌─── SchemaClass<{
12
// | readonly a: string;
13
// | readonly b: number;
14
// | }>
15
// ▼
16
const
const Renamed: Schema.SchemaClass<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>
Renamed
=
import Schema
Schema
.
const rename: <{ readonly c: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never, { readonly c: "a"; }>(self: Schema.Schema<{ readonly c: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>, mapping: { ...; }) => Schema.SchemaClass<...> (+1 overload)
rename
(
const Original: Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>
Original
, {
(property) c: "a"
c
: "a" })
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
(
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 Renamed: Schema.SchemaClass<{ readonly a: string; readonly b: number; }, { readonly c: string; readonly b: number; }, never>
Renamed
)({
(property) c: string
c
: "c",
(property) b: number
b
: 1 }))
19
// Output: { a: "c", b: 1 }

Example (Renaming Properties in Union Schemas)

1
import {
import Schema
Schema
} from "effect"
2
3
const
const Original: Schema.Union<[Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>, Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.Boolean; }>]>
Original
=
import Schema
Schema
.
function Union<[Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>, Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.Boolean; }>]>(members_0: Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>, members_1: Schema.Struct<...>): Schema.Union<...> (+3 overloads)
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
//
16
// ┌─── SchemaClass<{
17
// | readonly a: string;
18
// | readonly b: number;
19
// | } | {
20
// | readonly a: string;
21
// | readonly d: number;
22
// | }>
23
// ▼
24
const
const Renamed: Schema.SchemaClass<{ readonly a: string; readonly b: number; } | { readonly a: string; readonly d: boolean; }, { readonly c: string; readonly b: number; } | { readonly c: string; readonly d: boolean; }, never>
Renamed
=
import Schema
Schema
.
const rename: <{ readonly c: string; readonly b: number; } | { readonly c: string; readonly d: boolean; }, { readonly c: string; readonly b: number; } | { readonly c: string; readonly d: boolean; }, never, { ...; }>(self: Schema.Schema<...>, mapping: { ...; }) => Schema.SchemaClass<...> (+1 overload)
rename
(
const Original: Schema.Union<[Schema.Struct<{ c: typeof Schema.String; b: typeof Schema.Number; }>, Schema.Struct<{ c: typeof Schema.String; d: typeof Schema.Boolean; }>]>
Original
, {
(property) c: "a"
c
: "a" })
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) 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 Renamed: Schema.SchemaClass<{ readonly a: string; readonly b: number; } | { readonly a: string; readonly d: boolean; }, { readonly c: string; readonly b: number; } | { readonly c: string; readonly d: boolean; }, never>
Renamed
)({
(property) c: string
c
: "c",
(property) b: number
b
: 1 }))
27
// Output: { a: "c", b: 1 }
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
(
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 Renamed: Schema.SchemaClass<{ readonly a: string; readonly b: number; } | { readonly a: string; readonly d: boolean; }, { readonly c: string; readonly b: number; } | { readonly c: string; readonly d: boolean; }, never>
Renamed
)({
(property) c: string
c
: "c",
(property) d: boolean
d
: false }))
30
// Output: { a: 'c', d: false }

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

Example (Self-Referencing Schema)

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

1
import {
import Schema
Schema
} from "effect"
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
})

Example (Type Inference Error)

1
import {
import Schema
Schema
} from "effect"
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.

Example (Separating Recursive Fields)

1
import {
import Schema
Schema
} from "effect"
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
// ...other fields as needed
6
}
7
8
// Define an interface for the Category schema,
9
// extending the Type of the defined fields
10
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
> {
11
// Define `subcategories` using recursion
12
readonly
(property) Category.subcategories: readonly Category[]
subcategories
:
interface ReadonlyArray<T>
ReadonlyArray
<
interface Category
Category
>
13
}
14
15
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
({
16
...
const fields: { name: typeof Schema.String; }
fields
, // Spread in the base fields
17
(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
(
18
// Define `subcategories` using recursion
19
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
)
20
)
21
})

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

Example (Defining Mutually Recursive Schemas)

1
import {
import Schema
Schema
} from "effect"
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 (Recursive Schema with Different Encoded and Type Definitions)

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

1
import {
import Schema
Schema
} from "effect"
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"
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
})