Effect Data Types
Schemas for Effect's built-in containers validate runtime instances on both sides and transform their contents. Derive a canonical JSON codec (Schema.toCodecJson) when crossing a plain JSON boundary. Plain builders follow the call rules.
Configuration codecs
const DispatchConfig = Config.schema(Schema.Struct({
depot: Schema.String,
capacity: Schema.Finite
}), "dispatch")
effect function configurationExample() {
const provider = ConfigProvider.fromUnknown({
dispatch: { depot: "north", capacity: 24 }
})
return try DispatchConfig.parse(provider)
}
// Run in an effect function:
// {
// const jsonValue = try configurationExample()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }{"depot":"north","capacity":24}Config.schema builds a Config value without reading it; run try DispatchConfig against the ambient provider or try DispatchConfig.parse(provider) against a specific one. String-backed providers (such as environment variables) require string-accepting schemas like FiniteFromString or toCodecStringTree. Failures report through Config's error type.
Option, Result, and Exit
const OptionalCount = Schema.Option(Schema.FiniteFromString)
const CountResult = Schema.Result(Schema.FiniteFromString, Schema.Trim)
const CountExit = Schema.Exit(Schema.FiniteFromString, Schema.String, Schema.Defect())
effect function decodeContainers() {
const option = try Schema.decodeUnknownEffect(OptionalCount)(Option.some("9"))
const result = try Schema.decodeUnknownEffect(CountResult)(Result.fail(" delayed "))
const exit = try Schema.decodeUnknownEffect(CountExit)(Exit.succeed("9"))
return { option, result, exit }
}
// Run in an effect function:
// const values = try decodeContainers()
// try Console.log(values.option.isSome() ? values.option.value : "none")
// try Console.log(values.result.isFailure() ? values.result.failure : "success")
// try Console.log(values.exit._tag)
// try Console.log(try values.exit)9
delayed
Success
9Option, Result(success, failure), and Exit(success, failure, defect) transform their payloads while keeping the runtime container intact. A decoded Exit.Failure remains ordinary data until executed with try.
effect function encodeResult() {
const codec = Schema.toCodecJson(CountResult)
return try Schema.encodeEffect(codec)(Result.succeed(9))
}
// { _tag: "Success", success: "9" }
// Run in an effect function:
// {
// const jsonValue = try encodeResult()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }{"_tag":"Success","success":"9"}Canonical JSON encodes Option as { _tag: "Some", value } | { _tag: "None" }, Result with Success/success and Failure/failure, and Exit with Success/value and Failure/cause. Schema.Defect() preserves error name, message, and optional stack.
| Option codec | Decodes as None | Default None encoding |
|---|---|---|
OptionFromUndefinedOr | undefined | undefined |
OptionFromNullOr | null | null |
OptionFromNullishOr | null or undefined | undefined (or onNoneEncoding: null) |
OptionFromOptionalKey | missing key | omitted key |
OptionFromOptional | missing key or undefined | omitted key |
OptionFromOptionalNullOr | missing key, null, or undefined | omitted key ("omit", null, or undefined) |
Native and persistent collections
const NativeSet = Schema.ReadonlySet(Schema.FiniteFromString)
const NativeMap = Schema.ReadonlyMap(Schema.String, Schema.FiniteFromString)
const PersistentSet = Schema.HashSet(Schema.FiniteFromString)
const PersistentMap = Schema.HashMap(Schema.String, Schema.FiniteFromString)
effect function collectionsFromJson() {
const setCodec = Schema.toCodecJson(PersistentSet)
const mapCodec = Schema.toCodecJson(PersistentMap)
const set = try Schema.decodeUnknownEffect(setCodec)(["4", "7"])
const map = try Schema.decodeUnknownEffect(mapCodec)([["cold", "4"]])
return { set, map }
}
// Run in an effect function:
// const values = try collectionsFromJson()
// try Console.log(try Effect.try(() => JSON.stringify(Array.from(values.set))))
// try Console.log(try Effect.try(() => JSON.stringify(Array.from(values.map))))
// {
// const jsonValue = Array.from(try Schema.decodeUnknownEffect(NativeSet)(new Set(["4", "7"])))
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// {
// const jsonValue = Array.from(try Schema.decodeUnknownEffect(NativeMap)(new Map([["cold", "4"]])))
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }[4,7]
[["cold",4]]
[4,7]
[["cold",4]]ReadonlySet / ReadonlyMap wrap native Set / Map; HashSet / HashMap wrap Effect's persistent collections. Their canonical JSON codecs serialize sets as arrays and maps as [key, value] entry arrays while preserving element codecs.
Duration and Redacted
effect function durations() {
const hold = try Schema.decodeEffect(Schema.DurationFromString)("3 seconds")
const milliseconds = try Schema.encodeEffect(Schema.DurationFromMillis)(hold)
const nanoseconds = try Schema.decodeEffect(Schema.DurationFromNanos)(3_000_000_000n)
const jsonCodec = Schema.toCodecJson(Schema.Duration)
const json = try Schema.encodeEffect(jsonCodec)(Duration.seconds(3))
return { milliseconds, nanoseconds, json }
}
// json: { _tag: "Millis", value: 3000 }
// Run in an effect function:
// const value = try durations()
// try Console.log(value.milliseconds, Duration.toMillis(value.nanoseconds))
// try Console.log(try Effect.try(() => JSON.stringify(value.json)))3000 3000
{"_tag":"Millis","value":3000}Schema.Duration validates existing Duration values; DurationFromString, DurationFromMillis, and DurationFromNanos convert from text, milliseconds, or bigint nanoseconds. Canonical JSON uses tagged objects that preserve precision and infinity.
const AccessToken = Schema.RedactedFromValue(Schema.NonEmptyString)
const NonSerializableToken = Schema.Redacted(Schema.String, {
label: "DispatchToken",
disallowJsonEncode: true
})
effect function readToken(input: unknown) {
return try Schema.decodeUnknownEffect(AccessToken)(input)
}
// Run in an effect function:
// const value = try readToken("example-token")
// try Console.log(String(value))
// const codec = Schema.toCodecJson(NonSerializableToken)
// try Effect.catch(Schema.encodeEffect(codec)(value), (error) => Console.log(error._tag))<redacted>
SchemaErrorRedactedFromValue wraps a raw input; Redacted validates an existing Redacted container. Redaction hides values in display formatting only—encoding a raw-value or default JSON codec still emits the secret unless disallowJsonEncode: true is set.
The matching Effect page: effect.website/docs/v4/schema/effect-data-types