Skip to content

错误格式化器

在使用Effect Schema时,解码或编码操作期间遇到的错误可以使用两种内置方法进行格式化:TreeFormatterArrayFormatter。这些格式化器有助于以可读和可操作的方式构建和呈现错误。

TreeFormatter是格式化错误的默认方法。它以树状结构组织错误,提供清晰的问题层次结构。

示例(解码缺失属性)

import {
import Either

@since2.0.0

@since2.0.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

decodeUnknownEither
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)
const
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
=
const decode: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
decode
({})
if (
import Either

@since2.0.0

@since2.0.0

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

Determine if a Either is a Left.

@example

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

@since2.0.0

isLeft
(
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
)) {
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
("解码失败:")
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
(
import ParseResult
ParseResult
.
const TreeFormatter: ParseResult.ParseResultFormatter<string>

@since3.10.0

TreeFormatter
.
ParseResultFormatter<string>.formatErrorSync: (error: ParseResult.ParseError) => string
formatErrorSync
(
const result: Either.Left<ParseResult.ParseError, {
readonly name: string;
readonly age: number;
}>
result
.
Left<ParseError, { readonly name: string; readonly age: number; }>.left: ParseResult.ParseError
left
))
}
/*
解码失败:
{ readonly name: string; readonly age: number }
└─ ["name"]
└─ is missing
*/

在此示例中:

  • { readonly name: string; readonly age: number }描述了模式的预期结构。
  • ["name"]标识导致错误的特定字段。
  • is missing解释了"name"字段的问题。

您可以通过使用identifiertitledescription等注解来注释模式,使错误输出更加简洁和有意义。这些注解会替换错误消息中默认的TypeScript样式表示。

示例(使用title注解提高清晰度)

添加title注解会将错误消息中的模式结构替换为更易于理解的”Person”,使其更容易理解。

import {
import Either

@since2.0.0

@since2.0.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

Number
}).
Struct<{ name: typeof String$; age: typeof Number$; }>.annotations(annotations: Schema.Annotations.Schema<{
readonly name: string;
readonly age: number;
}, readonly []>): Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>

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

annotations
({
Annotations.Doc<{ readonly name: string; readonly age: number; }>.title?: string
title
: "Person" }) // 添加title注解
const
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
=
import Schema
Schema
.
const decodeUnknownEither: <{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly name: string;
readonly age: number;
}, {
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>

@since3.10.0

decodeUnknownEither
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)({})
if (
import Either

@since2.0.0

@since2.0.0

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

Determine if a Either is a Left.

@example

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

@since2.0.0

isLeft
(
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
)) {
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
(
import ParseResult
ParseResult
.
const TreeFormatter: ParseResult.ParseResultFormatter<string>

@since3.10.0

TreeFormatter
.
ParseResultFormatter<string>.formatErrorSync: (error: ParseResult.ParseError) => string
formatErrorSync
(
const result: Either.Left<ParseResult.ParseError, {
readonly name: string;
readonly age: number;
}>
result
.
Left<ParseError, { readonly name: string; readonly age: number; }>.left: ParseResult.ParseError
left
))
}
/*
Person
└─ ["name"]
└─ is missing
*/

默认情况下,像Schema.decodeUnknownEither这样的解码函数只报告第一个错误。要列出所有错误,请使用{ errors: "all" }选项。

示例(列出所有错误)

import {
import Either

@since2.0.0

@since2.0.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

decodeUnknownEither
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
, {
ParseOptions.errors?: "first" | "all" | undefined

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

default: "first"

@since3.10.0

errors
: "all" })
const
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
=
const decode: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
decode
({})
if (
import Either

@since2.0.0

@since2.0.0

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

Determine if a Either is a Left.

@example

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

@since2.0.0

isLeft
(
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
)) {
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
("解码失败:")
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
(
import ParseResult
ParseResult
.
const TreeFormatter: ParseResult.ParseResultFormatter<string>

@since3.10.0

TreeFormatter
.
ParseResultFormatter<string>.formatErrorSync: (error: ParseResult.ParseError) => string
formatErrorSync
(
const result: Either.Left<ParseResult.ParseError, {
readonly name: string;
readonly age: number;
}>
result
.
Left<ParseError, { readonly name: string; readonly age: number; }>.left: ParseResult.ParseError
left
))
}
/*
解码失败:
{ readonly name: string; readonly age: number }
├─ ["name"]
│ └─ is missing
└─ ["age"]
└─ is missing
*/

parseIssueTitle注解允许您通过基于正在验证的值生成标题来为错误消息添加动态上下文。例如,它可以包含来自验证对象的ID,使在复杂或嵌套数据结构中更容易识别特定问题。

注解类型

export type ParseIssueTitleAnnotation = (
issue: ParseIssue
) => string | undefined

返回值

  • 如果函数返回stringTreeFormatter将其用作标题,除非存在message注解(优先级更高)。
  • 如果函数返回undefinedTreeFormatter根据以下优先级确定标题:
    1. identifier注解
    2. title注解
    3. description注解
    4. 默认的TypeScript样式模式表示

示例(使用parseIssueTitle的动态标题)

import type {
import ParseResult
ParseResult
} from "effect"
import {
import Schema
Schema
} from "effect"
// 为OrderItem问题生成标题的函数
const
const getOrderItemId: ({ actual }: ParseResult.ParseIssue) => string | undefined
getOrderItemId
= ({
actual: unknown
actual
}:
import ParseResult
ParseResult
.
type ParseIssue = ParseResult.Type | ParseResult.Missing | ParseResult.Unexpected | ParseResult.Forbidden | ParseResult.Pointer | ParseResult.Refinement | ParseResult.Transformation | ParseResult.Composite

ParseIssue is a type that represents the different types of errors that can occur when decoding/encoding a value.

@since3.10.0

ParseIssue
) => {
if (
import Schema
Schema
.
is<{
readonly id: string;
}, {
readonly id: string;
}, never>(schema: Schema.Schema<{
readonly id: string;
}, {
readonly id: string;
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions | number) => u is {
readonly id: string;
}
export is

By default the option exact is set to true.

@since3.10.0

is
(
import Schema
Schema
.
function Struct<{
id: typeof Schema.String;
}>(fields: {
id: typeof Schema.String;
}): Schema.Struct<{
id: typeof Schema.String;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

String
}))(
actual: unknown
actual
)) {
return `OrderItem with id: ${
actual: {
readonly id: string;
}
actual
.
id: string
id
}`
}
}
const
const OrderItem: Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>
OrderItem
=
import Schema
Schema
.
function Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>(fields: {
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}): Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

Number
}).
Struct<{ id: typeof String$; name: typeof String$; price: typeof Number$; }>.annotations(annotations: Schema.Annotations.Schema<{
readonly id: string;
readonly name: string;
readonly price: number;
}, readonly []>): Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>

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

annotations
({
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "OrderItem",
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.parseIssueTitle?: ParseIssueTitleAnnotation
parseIssueTitle
:
const getOrderItemId: ({ actual }: ParseResult.ParseIssue) => string | undefined
getOrderItemId
})
// 为Order问题生成标题的函数
const
const getOrderId: ({ actual }: ParseResult.ParseIssue) => string | undefined
getOrderId
= ({
actual: unknown
actual
}:
import ParseResult
ParseResult
.
type ParseIssue = ParseResult.Type | ParseResult.Missing | ParseResult.Unexpected | ParseResult.Forbidden | ParseResult.Pointer | ParseResult.Refinement | ParseResult.Transformation | ParseResult.Composite

ParseIssue is a type that represents the different types of errors that can occur when decoding/encoding a value.

@since3.10.0

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

By default the option exact is set to true.

@since3.10.0

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

@since3.10.0

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

@since3.10.0

Number
}))(
actual: unknown
actual
)) {
return `Order with id: ${
actual: {
readonly id: number;
}
actual
.
id: number
id
}`
}
}
const
const Order: Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
items: Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>;
}>
Order
=
import Schema
Schema
.
function Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
items: Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>;
}>(fields: {
id: typeof Schema.Number;
name: typeof Schema.String;
items: Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>;
}): Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
items: Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

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

@since3.10.0

String
,
items: Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>
items
:
import Schema
Schema
.
Array<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>(value: Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>): Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>
export Array

@since3.10.0

Array
(
const OrderItem: Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>
OrderItem
)
}).
Struct<{ id: typeof Number$; name: typeof String$; items: Array$<Struct<{ id: typeof String$; name: typeof String$; price: typeof Number$; }>>; }>.annotations(annotations: Schema.Annotations.Schema<{
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}, readonly []>): Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
items: Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>;
}>

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

annotations
({
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.identifier?: string
identifier
: "Order",
Annotations.Schema<A, TypeParameters extends ReadonlyArray<any> = readonly []>.parseIssueTitle?: ParseIssueTitleAnnotation
parseIssueTitle
:
const getOrderId: ({ actual }: ParseResult.ParseIssue) => string | undefined
getOrderId
})
const
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}
decode
=
import Schema
Schema
.
decodeUnknownSync<{
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}, {
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}>(schema: Schema.Schema<{
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}, {
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}, never>, options?: ParseOptions): (u: unknown, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}
export decodeUnknownSync

@throwsParseError

@since3.10.0

decodeUnknownSync
(
const Order: Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
items: Schema.Array$<Schema.Struct<{
id: typeof Schema.String;
name: typeof Schema.String;
price: typeof Schema.Number;
}>>;
}>
Order
, {
ParseOptions.errors?: "first" | "all" | undefined

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

default: "first"

@since3.10.0

errors
: "all" })
// 情况1:没有可用的id,使用`identifier`注解
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}
decode
({})
/*
throws
ParseError: Order
├─ ["id"]
│ └─ is missing
├─ ["name"]
│ └─ is missing
└─ ["items"]
└─ is missing
*/
// 情况2:存在ID,使用动态`parseIssueTitle`注解
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}
decode
({
id: number
id
: 1 })
/*
throws
ParseError: Order with id: 1
├─ ["name"]
│ └─ is missing
└─ ["items"]
└─ is missing
*/
// 情况3:Order和OrderItem都有ID的嵌套问题
const decode: (u: unknown, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly items: readonly {
readonly id: string;
readonly name: string;
readonly price: number;
}[];
}
decode
({
id: number
id
: 1,
items: {
id: string;
price: string;
}[]
items
: [{
id: string
id
: "22b",
price: string
price
: "100" }] })
/*
throws
ParseError: Order with id: 1
├─ ["name"]
│ └─ is missing
└─ ["items"]
└─ ReadonlyArray<OrderItem>
└─ [0]
└─ OrderItem with id: 22b
├─ ["name"]
│ └─ is missing
└─ ["price"]
└─ Expected a number, actual "100"
*/

ArrayFormatter提供了一种结构化的、基于数组的错误格式化方法。它将每个错误表示为一个对象,使在数据解码或编码期间更容易分析和解决多个问题。每个错误对象包括_tagpathmessage等属性以提高清晰度。

示例(数组格式的单个错误)

import {
import Either

@since2.0.0

@since2.0.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

decodeUnknownEither
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
)
const
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
=
const decode: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
decode
({})
if (
import Either

@since2.0.0

@since2.0.0

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

Determine if a Either is a Left.

@example

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

@since2.0.0

isLeft
(
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
)) {
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
("解码失败:")
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
(
import ParseResult
ParseResult
.
const ArrayFormatter: ParseResult.ParseResultFormatter<ParseResult.ArrayFormatterIssue[]>

@since3.10.0

ArrayFormatter
.
ParseResultFormatter<ArrayFormatterIssue[]>.formatErrorSync: (error: ParseResult.ParseError) => ParseResult.ArrayFormatterIssue[]
formatErrorSync
(
const result: Either.Left<ParseResult.ParseError, {
readonly name: string;
readonly age: number;
}>
result
.
Left<ParseError, { readonly name: string; readonly age: number; }>.left: ParseResult.ParseError
left
))
}
/*
解码失败:
[ { _tag: 'Missing', path: [ 'name' ], message: 'is missing' } ]
*/

在此示例中:

  • _tag:指示错误类型(Missing)。
  • path:指定数据中错误的位置(['name'])。
  • message:描述问题('is missing')。

默认情况下,像Schema.decodeUnknownEither这样的解码函数只报告第一个错误。要列出所有错误,请使用{ errors: "all" }选项。

示例(列出所有错误)

import {
import Either

@since2.0.0

@since2.0.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

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

@since3.10.0

decodeUnknownEither
(
const Person: Schema.Struct<{
name: typeof Schema.String;
age: typeof Schema.Number;
}>
Person
, {
ParseOptions.errors?: "first" | "all" | undefined

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

default: "first"

@since3.10.0

errors
: "all" })
const
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
=
const decode: (u: unknown, overrideOptions?: ParseOptions) => Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
decode
({})
if (
import Either

@since2.0.0

@since2.0.0

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

Determine if a Either is a Left.

@example

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

@since2.0.0

isLeft
(
const result: Either.Either<{
readonly name: string;
readonly age: number;
}, ParseResult.ParseError>
result
)) {
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
("解码失败:")
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

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

@sincev0.1.100

error
(
import ParseResult
ParseResult
.
const ArrayFormatter: ParseResult.ParseResultFormatter<ParseResult.ArrayFormatterIssue[]>

@since3.10.0

ArrayFormatter
.
ParseResultFormatter<ArrayFormatterIssue[]>.formatErrorSync: (error: ParseResult.ParseError) => ParseResult.ArrayFormatterIssue[]
formatErrorSync
(
const result: Either.Left<ParseResult.ParseError, {
readonly name: string;
readonly age: number;
}>
result
.
Left<ParseError, { readonly name: string; readonly age: number; }>.left: ParseResult.ParseError
left
))
}
/*
解码失败:
[
{ _tag: 'Missing', path: [ 'name' ], message: 'is missing' },
{ _tag: 'Missing', path: [ 'age' ], message: 'is missing' }
]
*/

如果您正在使用React并需要表单验证,@hookform/resolverseffect/Schema提供了适配器,可以与React Hook Form集成以增强表单验证过程。这种集成允许您在React应用程序中利用effect/Schema的强大功能。

有关如何使用@hookform/resolverseffect/Schema与React Hook Form集成的更详细说明和示例,您可以访问官方npm包页面: React Hook Form Resolvers