Skip to content

默认构造器

在处理数据结构时,以最少的努力创建符合模式的值会很有帮助。 为此,Schema模块为各种模式类型提供了默认构造器,包括StructsRecordsfiltersbrands

默认构造器是不安全的,这意味着如果输入不符合模式,它们会抛出错误。 如果您需要更安全的替代方案,请考虑使用Schema.validateEither,它返回指示成功或失败的结果,而不是抛出错误。

示例(使用细化默认构造器)

import {
import Schema
Schema
} from "effect"
const
const schema: Schema.filter<typeof Schema.NumberFromString>
schema
=
import Schema
Schema
.
class NumberFromString

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

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

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

@since3.10.0

NumberFromString
.
Pipeable.pipe<typeof Schema.NumberFromString, Schema.filter<typeof Schema.NumberFromString>>(this: typeof Schema.NumberFromString, ab: (_: typeof Schema.NumberFromString) => Schema.filter<typeof Schema.NumberFromString>): Schema.filter<typeof Schema.NumberFromString> (+21 overloads)
pipe
(
import Schema
Schema
.
const between: <typeof Schema.NumberFromString>(minimum: number, maximum: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <A>(self: typeof Schema.NumberFromString & Schema.Schema<A, string, never>) => Schema.filter<typeof Schema.NumberFromString>

This filter checks whether the provided number falls within the specified minimum and maximum values.

@since3.10.0

between
(1, 10))
// 构造器只接受数字
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const schema: Schema.filter<typeof Schema.NumberFromString>
schema
.
refine<number, typeof NumberFromString>.make(a: number, options?: Schema.MakeOptions): number
make
(5))
// Output: 5
// 这将抛出错误,因为数字超出了有效范围
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const schema: Schema.filter<typeof Schema.NumberFromString>
schema
.
refine<number, typeof NumberFromString>.make(a: number, options?: Schema.MakeOptions): number
make
(20))
/*
throws:
ParseError: between(1, 10)
└─ Predicate refinement failure
└─ Expected a number between 1 and 10, actual 20
*/

结构模式允许您定义具有特定字段和约束的对象。make函数可用于创建结构模式的实例。

示例(创建结构实例)

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

@since3.10.0

Struct
({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
})
// 成功创建
const Struct: Schema.Struct<{
name: typeof Schema.NonEmptyString;
}>
Struct
.
Struct<{ name: typeof NonEmptyString; }>.make(props: {
readonly name: string;
}, options?: Schema.MakeOptions): {
readonly name: string;
}
make
({
name: string
name
: "a" })
// 这将抛出错误,因为名称为空
const Struct: Schema.Struct<{
name: typeof Schema.NonEmptyString;
}>
Struct
.
Struct<{ name: typeof NonEmptyString; }>.make(props: {
readonly name: string;
}, options?: Schema.MakeOptions): {
readonly name: string;
}
make
({
name: string
name
: "" })
/*
throws
ParseError: { readonly name: NonEmptyString }
└─ ["name"]
└─ NonEmptyString
└─ Predicate refinement failure
└─ Expected NonEmptyString, actual ""
*/

在某些情况下,您可能需要绕过验证。虽然在大多数情况下不建议这样做,但make提供了禁用验证的选项。

示例(绕过验证)

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

@since3.10.0

Struct
({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
})
// 在实例化期间绕过验证
const Struct: Schema.Struct<{
name: typeof Schema.NonEmptyString;
}>
Struct
.
Struct<{ name: typeof NonEmptyString; }>.make(props: {
readonly name: string;
}, options?: Schema.MakeOptions): {
readonly name: string;
}
make
({
name: string
name
: "" }, true)
// 或显式使用`disableValidation`选项
const Struct: Schema.Struct<{
name: typeof Schema.NonEmptyString;
}>
Struct
.
Struct<{ name: typeof NonEmptyString; }>.make(props: {
readonly name: string;
}, options?: Schema.MakeOptions): {
readonly name: string;
}
make
({
name: string
name
: "" }, {
disableValidation?: boolean | undefined
disableValidation
: true })

记录模式允许您定义键值映射,其中键和值必须满足特定条件。

示例(创建记录实例)

import {
import Schema
Schema
} from "effect"
const
const Record: Schema.Record$<typeof Schema.String, typeof Schema.NonEmptyString>
Record
=
import Schema
Schema
.
const Record: <typeof Schema.String, typeof Schema.NonEmptyString>(options: {
readonly key: typeof Schema.String;
readonly value: typeof Schema.NonEmptyString;
}) => Schema.Record$<typeof Schema.String, typeof Schema.NonEmptyString>

@since3.10.0

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

@since3.10.0

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

@since3.10.0

NonEmptyString
})
// 成功创建
const Record: Schema.Record$<typeof Schema.String, typeof Schema.NonEmptyString>
Record
.
Record$<typeof String$, typeof NonEmptyString>.make(props: void | {
readonly [x: string]: string;
}, options?: Schema.MakeOptions): {
readonly [x: string]: string;
}
make
({
a: string
a
: "a",
b: string
b
: "b" })
// 这将抛出错误,因为'b'为空
const Record: Schema.Record$<typeof Schema.String, typeof Schema.NonEmptyString>
Record
.
Record$<typeof String$, typeof NonEmptyString>.make(props: void | {
readonly [x: string]: string;
}, options?: Schema.MakeOptions): {
readonly [x: string]: string;
}
make
({
a: string
a
: "a",
b: string
b
: "" })
/*
throws
ParseError: { readonly [x: string]: NonEmptyString }
└─ ["b"]
└─ NonEmptyString
└─ Predicate refinement failure
└─ Expected NonEmptyString, actual ""
*/
// 绕过验证
const Record: Schema.Record$<typeof Schema.String, typeof Schema.NonEmptyString>
Record
.
Record$<typeof String$, typeof NonEmptyString>.make(props: void | {
readonly [x: string]: string;
}, options?: Schema.MakeOptions): {
readonly [x: string]: string;
}
make
({
a: string
a
: "a",
b: string
b
: "" }, {
disableValidation?: boolean | undefined
disableValidation
: true })

过滤器允许您定义对单个值的约束。

示例(使用过滤器强制范围)

import {
import Schema
Schema
} from "effect"
const
const MyNumber: Schema.filter<typeof Schema.Number>
MyNumber
=
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.filter<typeof Schema.Number>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.filter<typeof Schema.Number>): Schema.filter<typeof Schema.Number> (+21 overloads)
pipe
(
import Schema
Schema
.
const between: <typeof Schema.Number>(minimum: number, maximum: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <A>(self: typeof Schema.Number & Schema.Schema<A, number, never>) => Schema.filter<typeof Schema.Number>

This filter checks whether the provided number falls within the specified minimum and maximum values.

@since3.10.0

between
(1, 10))
// 成功创建
const
const n: number
n
=
const MyNumber: Schema.filter<typeof Schema.Number>
MyNumber
.
refine<number, typeof Number$>.make(a: number, options?: Schema.MakeOptions): number
make
(5)
// 这将抛出错误,因为数字超出了有效范围
const MyNumber: Schema.filter<typeof Schema.Number>
MyNumber
.
refine<number, typeof Number$>.make(a: number, options?: Schema.MakeOptions): number
make
(20)
/*
throws
ParseError: a number between 1 and 10
└─ Predicate refinement failure
└─ Expected a number between 1 and 10, actual 20
*/
// 绕过验证
const MyNumber: Schema.filter<typeof Schema.Number>
MyNumber
.
refine<number, typeof Number$>.make(a: number, options?: Schema.MakeOptions): number
make
(20, {
disableValidation?: boolean | undefined
disableValidation
: true })

品牌模式为值添加元数据以赋予其更具体的类型,同时仍保留其原始类型。

示例(创建品牌值)

import {
import Schema
Schema
} from "effect"
const
const BrandedNumberSchema: Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber">
BrandedNumberSchema
=
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.filter<typeof Schema.Number>, Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber">>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.filter<typeof Schema.Number>, bc: (_: Schema.filter<typeof Schema.Number>) => Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber">): Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber"> (+21 overloads)
pipe
(
import Schema
Schema
.
const between: <typeof Schema.Number>(minimum: number, maximum: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <A>(self: typeof Schema.Number & Schema.Schema<A, number, never>) => Schema.filter<typeof Schema.Number>

This filter checks whether the provided number falls within the specified minimum and maximum values.

@since3.10.0

between
(1, 10),
import Schema
Schema
.
const brand: <Schema.filter<typeof Schema.Number>, "MyNumber">(brand: "MyNumber", annotations?: Schema.Annotations.Schema<number & Brand<"MyNumber">, readonly []> | undefined) => (self: Schema.filter<typeof Schema.Number>) => Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber">

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

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

@example

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

@since3.10.0

brand
("MyNumber")
)
// 成功创建
const
const n: number & Brand<"MyNumber">
n
=
const BrandedNumberSchema: Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber">
BrandedNumberSchema
.
BrandSchema<number & Brand<"MyNumber">, number, never>.make(a: number, options?: Schema.MakeOptions): number & Brand<"MyNumber">
make
(5)
// 这将抛出错误,因为数字超出了有效范围
const BrandedNumberSchema: Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber">
BrandedNumberSchema
.
BrandSchema<number & Brand<"MyNumber">, number, never>.make(a: number, options?: Schema.MakeOptions): number & Brand<"MyNumber">
make
(20)
/*
throws
ParseError: a number between 1 and 10 & Brand<"MyNumber">
└─ Predicate refinement failure
└─ Expected a number between 1 and 10 & Brand<"MyNumber">, actual 20
*/
// 绕过验证
const BrandedNumberSchema: Schema.brand<Schema.filter<typeof Schema.Number>, "MyNumber">
BrandedNumberSchema
.
BrandSchema<number & Brand<"MyNumber">, number, never>.make(a: number, options?: Schema.MakeOptions): number & Brand<"MyNumber">
make
(20, {
disableValidation?: boolean | undefined
disableValidation
: true })

使用默认构造器时,了解它们产生的值类型很有帮助。

例如,在BrandedNumberSchema示例中,构造器的返回类型是number & Brand<"MyNumber">。这表明结果值是带有额外品牌信息"MyNumber"number

这种行为与过滤器示例形成对比,在过滤器示例中,返回类型只是number。品牌添加了额外的类型信息层,这可以帮助更有效地识别和处理您的数据。

默认构造器被认为是”不安全的”,因为如果输入不符合模式,它们会抛出错误。此错误包括对出错原因的详细描述。默认构造器的目的是提供一种直接的方式来创建有效值,例如用于测试或配置,其中无效输入预期是异常情况。

如果您需要一个”安全”的构造器,它不抛出错误,而是返回指示成功或失败的结果,您可以使用Schema.validateEither

示例(使用Schema.validateEither进行安全验证)

import {
import Schema
Schema
} from "effect"
const
const schema: Schema.filter<typeof Schema.NumberFromString>
schema
=
import Schema
Schema
.
class NumberFromString

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

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

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

@since3.10.0

NumberFromString
.
Pipeable.pipe<typeof Schema.NumberFromString, Schema.filter<typeof Schema.NumberFromString>>(this: typeof Schema.NumberFromString, ab: (_: typeof Schema.NumberFromString) => Schema.filter<typeof Schema.NumberFromString>): Schema.filter<typeof Schema.NumberFromString> (+21 overloads)
pipe
(
import Schema
Schema
.
const between: <typeof Schema.NumberFromString>(minimum: number, maximum: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <A>(self: typeof Schema.NumberFromString & Schema.Schema<A, string, never>) => Schema.filter<typeof Schema.NumberFromString>

This filter checks whether the provided number falls within the specified minimum and maximum values.

@since3.10.0

between
(1, 10))
// 创建验证未知输入的安全构造器
const
const safeMake: (u: unknown, overrideOptions?: ParseOptions) => Either<number, ParseError>
safeMake
=
import Schema
Schema
.
const validateEither: <number, string, never>(schema: Schema.Schema<number, string, never>, options?: ParseOptions) => (u: unknown, overrideOptions?: ParseOptions) => Either<number, ParseError>

@since3.10.0

validateEither
(
const schema: Schema.filter<typeof Schema.NumberFromString>
schema
)
// 有效输入返回Right值
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const safeMake: (u: unknown, overrideOptions?: ParseOptions) => Either<number, ParseError>
safeMake
(5))
/*
Output:
{ _id: 'Either', _tag: 'Right', right: 5 }
*/
// 无效输入返回带有详细错误信息的Left值
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const safeMake: (u: unknown, overrideOptions?: ParseOptions) => Either<number, ParseError>
safeMake
(20))
/*
Output:
{
_id: 'Either',
_tag: 'Left',
left: {
_id: 'ParseError',
message: 'between(1, 10)\n' +
'└─ Predicate refinement failure\n' +
' └─ Expected a number between 1 and 10, actual 20'
}
}
*/
// 这将抛出错误,因为它是不安全的
const schema: Schema.filter<typeof Schema.NumberFromString>
schema
.
refine<number, typeof NumberFromString>.make(a: number, options?: Schema.MakeOptions): number
make
(20)
/*
throws:
ParseError: between(1, 10)
└─ Predicate refinement failure
└─ Expected a number between 1 and 10, actual 20
*/

创建对象时,您可能希望为某些字段分配默认值以简化对象构造。Schema.withConstructorDefault函数让您处理默认值,使字段在默认构造器中变为可选。

示例(具有必需字段的结构)

在此示例中,创建新实例时所有字段都是必需的。

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

@since3.10.0

Struct
({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

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

@since3.10.0

Number
})
// 必须提供name和age
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: typeof Schema.Number;
}>
Person
.
Struct<{ name: typeof NonEmptyString; age: typeof Number$; }>.make(props: {
readonly name: string;
readonly age: number;
}, options?: Schema.MakeOptions): {
readonly name: string;
readonly age: number;
}
make
({
name: string
name
: "John",
age: number
age
: 30 }))
/*
Output: { name: 'John', age: 30 }
*/

示例(具有默认值的结构)

在这里,age字段是可选的,因为它有默认值0

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

@since3.10.0

Struct
({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
,
age: Schema.PropertySignature<":", number, never, ":", number, true, never>
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.propertySignature<typeof Schema.Number>, Schema.PropertySignature<":", number, never, ":", number, true, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.propertySignature<typeof Schema.Number>, bc: (_: Schema.propertySignature<typeof Schema.Number>) => Schema.PropertySignature<":", number, never, ":", number, true, never>): Schema.PropertySignature<":", number, never, ":", number, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <number>(defaultValue: () => number) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => 0)
)
})
// age字段是可选的,默认为0
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
Person
.
Struct<{ name: typeof NonEmptyString; age: PropertySignature<":", number, never, ":", number, true, never>; }>.make(props: {
readonly name: string;
readonly age?: number;
}, options?: Schema.MakeOptions): {
readonly name: string;
readonly age: number;
}
make
({
name: string
name
: "John" }))
/*
Output:
{ name: 'John', age: 0 }
*/
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
Person
.
Struct<{ name: typeof NonEmptyString; age: PropertySignature<":", number, never, ":", number, true, never>; }>.make(props: {
readonly name: string;
readonly age?: number;
}, options?: Schema.MakeOptions): {
readonly name: string;
readonly age: number;
}
make
({
name: string
name
: "John",
age?: number
age
: 30 }))
/*
Output:
{ name: 'John', age: 30 }
*/

模式中的默认值是浅层的,这意味着在嵌套结构中定义的默认值不会自动传播到顶级构造器。

示例(嵌套结构中的浅默认值)

import {
import Schema
Schema
} from "effect"
const
const Config: Schema.Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>
Config
=
import Schema
Schema
.
function Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>(fields: {
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}): Schema.Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}> (+1 overload)

@since3.10.0

Struct
({
// 定义具有默认值的嵌套结构
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>
web
:
import Schema
Schema
.
function Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>(fields: {
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}): Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>
application_url
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.propertySignature<typeof Schema.String>, Schema.PropertySignature<":", string, never, ":", string, true, never>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.propertySignature<typeof Schema.String>, bc: (_: Schema.propertySignature<typeof Schema.String>) => Schema.PropertySignature<":", string, never, ":", string, true, never>): Schema.PropertySignature<":", string, never, ":", string, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <string>(defaultValue: () => string) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, string, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, string, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => "http://localhost")
),
application_port: typeof Schema.Number
application_port
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
})
// 这将导致类型错误,因为嵌套结构中缺少`application_url`
const Config: Schema.Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>
Config
.
Struct<{ web: Struct<{ application_url: PropertySignature<":", string, never, ":", string, true, never>; application_port: typeof Number$; }>; }>.make(props: {
readonly web: {
readonly application_url: string;
readonly application_port: number;
};
}, options?: Schema.MakeOptions): {
readonly web: {
readonly application_url: string;
readonly application_port: number;
};
}
make
({ web: {
application_port: number
application_port
: 3000 } })
Error ts(2741) ― Property 'application_url' is missing in type '{ application_port: number; }' but required in type '{ readonly application_url: string; readonly application_port: number; }'.

出现这种行为是因为Schema接口不包含从嵌套结构传递默认构造器类型的类型参数。

要解决此限制,请提取嵌套结构的构造器并将其直接应用于其字段。这确保嵌套默认值得到尊重。

示例(使用嵌套结构构造器)

import {
import Schema
Schema
} from "effect"
const
const Config: Schema.Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>
Config
=
import Schema
Schema
.
function Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>(fields: {
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}): Schema.Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}> (+1 overload)

@since3.10.0

Struct
({
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>
web
:
import Schema
Schema
.
function Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>(fields: {
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}): Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}> (+1 overload)

@since3.10.0

Struct
({
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>
application_url
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
.
Pipeable.pipe<typeof Schema.String, Schema.propertySignature<typeof Schema.String>, Schema.PropertySignature<":", string, never, ":", string, true, never>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.propertySignature<typeof Schema.String>, bc: (_: Schema.propertySignature<typeof Schema.String>) => Schema.PropertySignature<":", string, never, ":", string, true, never>): Schema.PropertySignature<":", string, never, ":", string, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <string>(defaultValue: () => string) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, string, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, string, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => "http://localhost")
),
application_port: typeof Schema.Number
application_port
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
})
// 提取嵌套结构构造器
const {
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>
web
:
const Web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>
Web
} =
const Config: Schema.Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>
Config
.
Struct<{ web: Struct<{ application_url: PropertySignature<":", string, never, ":", string, true, never>; application_port: typeof Number$; }>; }>.fields: Readonly<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>
fields
// 使用嵌套结构的构造器
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const Config: Schema.Struct<{
web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>;
}>
Config
.
Struct<{ web: Struct<{ application_url: PropertySignature<":", string, never, ":", string, true, never>; application_port: typeof Number$; }>; }>.make(props: {
readonly web: {
readonly application_url: string;
readonly application_port: number;
};
}, options?: Schema.MakeOptions): {
readonly web: {
readonly application_url: string;
readonly application_port: number;
};
}
make
({
web: {
readonly application_url: string;
readonly application_port: number;
}
web
:
const Web: Schema.Struct<{
application_url: Schema.PropertySignature<":", string, never, ":", string, true, never>;
application_port: typeof Schema.Number;
}>
Web
.
Struct<{ application_url: PropertySignature<":", string, never, ":", string, true, never>; application_port: typeof Number$; }>.make(props: {
readonly application_url?: string;
readonly application_port: number;
}, options?: Schema.MakeOptions): {
readonly application_url: string;
readonly application_port: number;
}
make
({
application_port: number
application_port
: 3000 }) }))
/*
Output:
{
web: {
application_url: 'http://localhost',
application_port: 3000
}
}
*/

默认值是延迟求值的,这意味着每次调用构造器时都会生成默认值的新实例:

示例(默认值的延迟求值)

在此示例中,timestamp字段为每个实例生成新值。

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

@since3.10.0

Struct
({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
,
age: Schema.PropertySignature<":", number, never, ":", number, true, never>
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.propertySignature<typeof Schema.Number>, Schema.PropertySignature<":", number, never, ":", number, true, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.propertySignature<typeof Schema.Number>, bc: (_: Schema.propertySignature<typeof Schema.Number>) => Schema.PropertySignature<":", number, never, ":", number, true, never>): Schema.PropertySignature<":", number, never, ":", number, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <number>(defaultValue: () => number) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => 0)
),
timestamp: Schema.PropertySignature<":", number, never, ":", number, true, never>
timestamp
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.propertySignature<typeof Schema.Number>, Schema.PropertySignature<":", number, never, ":", number, true, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.propertySignature<typeof Schema.Number>, bc: (_: Schema.propertySignature<typeof Schema.Number>) => Schema.PropertySignature<":", number, never, ":", number, true, never>): Schema.PropertySignature<":", number, never, ":", number, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <number>(defaultValue: () => number) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => new
var Date: DateConstructor
new () => Date (+3 overloads)
Date
().
Date.getTime(): number

Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.

getTime
())
)
})
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
timestamp: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
Person
.
Struct<{ name: typeof NonEmptyString; age: PropertySignature<":", number, never, ":", number, true, never>; timestamp: PropertySignature<":", number, never, ":", number, true, never>; }>.make(props: {
readonly name: string;
readonly age?: number;
readonly timestamp?: number;
}, options?: Schema.MakeOptions): {
readonly name: string;
readonly age: number;
readonly timestamp: number;
}
make
({
name: string
name
: "name1" }))
/*
Example Output:
{ age: 0, timestamp: 1714232909221, name: 'name1' }
*/
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
timestamp: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
Person
.
Struct<{ name: typeof NonEmptyString; age: PropertySignature<":", number, never, ":", number, true, never>; timestamp: PropertySignature<":", number, never, ":", number, true, never>; }>.make(props: {
readonly name: string;
readonly age?: number;
readonly timestamp?: number;
}, options?: Schema.MakeOptions): {
readonly name: string;
readonly age: number;
readonly timestamp: number;
}
make
({
name: string
name
: "name2" }))
/*
Example Output:
{ age: 0, timestamp: 1714232909227, name: 'name2' }
*/

默认值也是”可移植的”,这意味着如果您在另一个模式中重用相同的属性签名,默认值会被传递:

示例(在另一个模式中重用默认值)

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

@since3.10.0

Struct
({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
,
age: Schema.PropertySignature<":", number, never, ":", number, true, never>
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.propertySignature<typeof Schema.Number>, Schema.PropertySignature<":", number, never, ":", number, true, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.propertySignature<typeof Schema.Number>, bc: (_: Schema.propertySignature<typeof Schema.Number>) => Schema.PropertySignature<":", number, never, ":", number, true, never>): Schema.PropertySignature<":", number, never, ":", number, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <number>(defaultValue: () => number) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => 0)
),
timestamp: Schema.PropertySignature<":", number, never, ":", number, true, never>
timestamp
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.propertySignature<typeof Schema.Number>, Schema.PropertySignature<":", number, never, ":", number, true, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.propertySignature<typeof Schema.Number>, bc: (_: Schema.propertySignature<typeof Schema.Number>) => Schema.PropertySignature<":", number, never, ":", number, true, never>): Schema.PropertySignature<":", number, never, ":", number, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <number>(defaultValue: () => number) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => new
var Date: DateConstructor
new () => Date (+3 overloads)
Date
().
Date.getTime(): number

Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.

getTime
())
)
})
const
const AnotherSchema: Schema.Struct<{
foo: typeof Schema.String;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
AnotherSchema
=
import Schema
Schema
.
function Struct<{
foo: typeof Schema.String;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>(fields: {
foo: typeof Schema.String;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}): Schema.Struct<{
foo: typeof Schema.String;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}> (+1 overload)

@since3.10.0

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

@since3.10.0

String
,
age: Schema.PropertySignature<":", number, never, ":", number, true, never>
age
:
const Person: Schema.Struct<{
name: typeof Schema.NonEmptyString;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
timestamp: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
Person
.
Struct<{ name: typeof NonEmptyString; age: PropertySignature<":", number, never, ":", number, true, never>; timestamp: PropertySignature<":", number, never, ":", number, true, never>; }>.fields: Readonly<{
name: typeof Schema.NonEmptyString;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
timestamp: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
fields
.
age: Schema.PropertySignature<":", number, never, ":", number, true, never>
age
})
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(
const AnotherSchema: Schema.Struct<{
foo: typeof Schema.String;
age: Schema.PropertySignature<":", number, never, ":", number, true, never>;
}>
AnotherSchema
.
Struct<{ foo: typeof String$; age: PropertySignature<":", number, never, ":", number, true, never>; }>.make(props: {
readonly age?: number;
readonly foo: string;
}, options?: Schema.MakeOptions): {
readonly age: number;
readonly foo: string;
}
make
({
foo: string
foo
: "bar" }))
/*
Output:
{ foo: 'bar', age: 0 }
*/

在使用Class API时也可以应用默认值,确保基于类的模式的一致性。

示例(类中的默认值)

import {
import Schema
Schema
} from "effect"
class
class Person
Person
extends
import Schema
Schema
.
const Class: <Person>(identifier: string) => <Fields>(fieldsOr: Fields | HasFields<Fields>, annotations?: ClassAnnotations<Person, { [K in keyof Schema.Struct<Fields extends Schema.Struct.Fields>.Type<Fields>]: Schema.Struct.Type<Fields>[K]; }> | undefined) => Schema.Class<Person, Fields, Schema.Struct.Encoded<Fields>, Schema.Schema<in out A, in out I = A, out R = never>.Context<Fields[keyof Fields]>, Schema.Struct.Constructor<Fields>, {}, {}>

@example

import { Schema } from "effect"
class MyClass extends Schema.Class<MyClass>("MyClass")({
someField: Schema.String
}) {
someMethod() {
return this.someField + "bar"
}
}

@since3.10.0

Class
<
class Person
Person
>("Person")({
name: typeof Schema.NonEmptyString
name
:
import Schema
Schema
.
class NonEmptyString

@since3.10.0

NonEmptyString
,
age: Schema.PropertySignature<":", number, never, ":", number, true, never>
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.propertySignature<typeof Schema.Number>, Schema.PropertySignature<":", number, never, ":", number, true, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.propertySignature<typeof Schema.Number>, bc: (_: Schema.propertySignature<typeof Schema.Number>) => Schema.PropertySignature<":", number, never, ":", number, true, never>): Schema.PropertySignature<":", number, never, ":", number, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <number>(defaultValue: () => number) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => 0)
),
timestamp: Schema.PropertySignature<":", number, never, ":", number, true, never>
timestamp
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
.
Pipeable.pipe<typeof Schema.Number, Schema.propertySignature<typeof Schema.Number>, Schema.PropertySignature<":", number, never, ":", number, true, never>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.propertySignature<typeof Schema.Number>, bc: (_: Schema.propertySignature<typeof Schema.Number>) => Schema.PropertySignature<":", number, never, ":", number, true, never>): Schema.PropertySignature<":", number, never, ":", number, true, never> (+21 overloads)
pipe
(
import Schema
Schema
.
const propertySignature: <S extends Schema.Schema.All>(self: S) => Schema.propertySignature<S>

Lifts a Schema into a PropertySignature.

@since1.0.0

@since3.10.0

propertySignature
,
import Schema
Schema
.
const withConstructorDefault: <number>(defaultValue: () => number) => <TypeToken, Key, EncodedToken, Encoded, R>(self: Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, boolean, R>) => Schema.PropertySignature<TypeToken, number, Key, EncodedToken, Encoded, true, R> (+1 overload)

Enhances a property signature with a default constructor value.

@since3.10.0

withConstructorDefault
(() => new
var Date: DateConstructor
new () => Date (+3 overloads)
Date
().
Date.getTime(): number

Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.

getTime
())
)
}) {}
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(new
constructor Person(props: {
readonly name: string;
readonly age?: number;
readonly timestamp?: number;
}, options?: Schema.MakeOptions): Person
Person
({
name: string
name
: "name1" }))
/*
Example Output:
Person { age: 0, timestamp: 1714400867208, name: 'name1' }
*/
var console: Console

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

The module exports two specific components:

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

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

Example using the global console:

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

Example using the Console class:

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

@seesource

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

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

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

See util.format() for more information.

@sincev0.1.100

log
(new
constructor Person(props: {
readonly name: string;
readonly age?: number;
readonly timestamp?: number;
}, options?: Schema.MakeOptions): Person
Person
({
name: string
name
: "name2" }))
/*
Example Output:
Person { age: 0, timestamp: 1714400867215, name: 'name2' }
*/