DateFromMillisType-level representation of DateFromMillis.
export interface DateFromMillis extends interface decodeTo<To extends Constraint, From extends Constraint, RD = never, RE = never>Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
Type-level representation returned by
decodeTo
.
decodeTo<Date, Number> {
readonly "Rebuild": DateFromMillis
}
/**
* Schema that decodes epoch milliseconds into a JavaScript `Date`.
*
* **When to use**
*
* Use to model numeric millisecond timestamps that decode to JavaScript `Date`
* objects and encode back to numbers.
*
* **Details**
*
* Decoding:
* A number of milliseconds since the Unix epoch is decoded as a `Date`.
*
* Encoding:
* A `Date` is encoded as its millisecond timestamp.
*
* **Gotchas**
*
* This schema accepts any number, including `NaN`, `Infinity`, and `-Infinity`.
* Those values decode to invalid `Date` instances.
*
* @see {@link DateFromString} for decoding string-encoded dates
* @see {@link DateTimeUtcFromMillis} for decoding epoch milliseconds into UTC values
*
* @category Date
* @since 4.0.0
*/
export const const DateFromMillis: DateFromMillisconst DateFromMillis: {
Rebuild: DateFromMillis;
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
ast: Ast;
annotate: (annotations: Annotations.Bottom<globalThis.Date, readonly []>) => DateFromMillis;
annotateKey: (annotations: Annotations.Key<globalThis.Date>) => DateFromMillis;
check: (checks_0: SchemaAST.Check<globalThis.Date>, ...checks: Array<SchemaAST.Check<globalThis.Date>>) => DateFromMillis;
rebuild: (ast: SchemaAST.Declaration) => DateFromMillis;
make: (input: globalThis.Date, options?: MakeOptions) => globalThis.Date;
makeOption: (input: globalThis.Date, options?: MakeOptions) => Option_.Option<globalThis.Date>;
makeEffect: (input: globalThis.Date, options?: MakeOptions) => Effect.Effect<globalThis.Date, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
DateFromMillis
.
Schema that decodes epoch milliseconds into a JavaScript Date.
When to use
Use to model numeric millisecond timestamps that decode to JavaScript Date
objects and encode back to numbers.
Details
Decoding:
A number of milliseconds since the Unix epoch is decoded as a Date.
Encoding:
A Date is encoded as its millisecond timestamp.
Gotchas
This schema accepts any number, including NaN, Infinity, and -Infinity.
Those values decode to invalid Date instances.
DateFromMillis: DateFromMillis = const Number: Numberconst Number: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<number, readonly []>) => Number;
annotateKey: (annotations: Annotations.Key<number>) => Number;
check: (checks_0: SchemaAST.Check<number>, ...checks: Array<SchemaAST.Check<number>>) => Number;
rebuild: (ast: SchemaAST.Number) => Number;
make: (input: number, options?: MakeOptions) => number;
makeOption: (input: number, options?: MakeOptions) => Option_.Option<number>;
makeEffect: (input: number, options?: MakeOptions) => Effect.Effect<number, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
Number
.
Schema for number values, including NaN, Infinity, and -Infinity.
Details
Default JSON serializer:
- Finite numbers are serialized as numbers.
- Non-finite values are serialized as strings (
"NaN", "Infinity", "-Infinity").
Number.pipe(
function decodeTo<Date, Constraint, never, never>(to: Date, transformation: {
readonly decode: SchemaGetter.Getter<NoInfer<globalThis.Date>, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, NoInfer<globalThis.Date>, never>;
}): (from: Constraint) => decodeTo<Date, Constraint, never, never> (+1 overload)
Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
decodeTo(const Date: Dateconst Date: {
Rebuild: Date;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<globalThis.Date, readonly []>) => Date;
annotateKey: (annotations: Annotations.Key<globalThis.Date>) => Date;
check: (checks_0: SchemaAST.Check<globalThis.Date>, ...checks: Array<SchemaAST.Check<globalThis.Date>>) => Date;
rebuild: (ast: SchemaAST.Declaration) => Date;
make: (input: globalThis.Date, options?: MakeOptions) => globalThis.Date;
makeOption: (input: globalThis.Date, options?: MakeOptions) => Option_.Option<globalThis.Date>;
makeEffect: (input: globalThis.Date, options?: MakeOptions) => Effect.Effect<globalThis.Date, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
Date
.
Schema for JavaScript Date objects.
When to use
Use to validate in-memory values that must already be JavaScript date
objects.
Details
This schema accepts any Date instance, including invalid dates. The default
JSON serializer encodes valid dates as ISO 8601 strings; invalid dates encode
as "Invalid Date".
Example (Defining a Date schema)
import { Schema } from "effect"
Schema.decodeUnknownSync(Schema.Date)(new Date("2024-01-01"))
// => Date { 2024-01-01T00:00:00.000Z }
Date, import SchemaTransformationSchemaTransformation.const dateFromMillis: Transformation<
globalThis.Date,
number
>
const dateFromMillis: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => SchemaTransformation.Transformation<number, globalThis.Date, never, never>;
compose: (other: SchemaTransformation.Transformation<T2, globalThis.Date, RD2, RE2>) => SchemaTransformation.Transformation<T2, number, RD2, RE2>;
}
Decodes epoch milliseconds into a Date and encodes a Date back to epoch
milliseconds.
When to use
Use when you need a schema transformation for numeric timestamps represented
as milliseconds since the Unix epoch.
Details
Decoding creates a Date from the number like new Date(ms). Encoding
returns the Date timestamp like date.getTime().
Gotchas
This transformation does not validate date validity. NaN, Infinity, and
-Infinity decode to invalid Date instances.
Example (Converting milliseconds to a Date)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.Number.pipe(
Schema.decodeTo(Schema.Date, SchemaTransformation.dateFromMillis)
)
dateFromMillis)
)