decodeUnknownOption

Avatar of Hemanta SundarayHemanta Sundaray

decodeUnknownOption is a synchronous decoder that returns an Option value instead of throwing.

Option is an Effect data structure for optional outcomes:

  • Option.some(value) means decoding succeeded.
  • Option.none() means decoding failed.

Unlike decodeUnknownExit, this function does not return error details. It only tells you whether decoding was a success or failure.

schema.ts
import { Option, Schema } from "effect";
const schema = Schema.Struct({
name: Schema.String,
age: Schema.Number,
});
const decode = Schema.decodeUnknownOption(schema);
// Happy path
const ok = decode({ name: "Alice", age: 30 });
if (Option.isSome(ok)) {
console.log("Decoded Value:", ok.value);
}
// Error path
const bad = decode({ name: 42, age: "30" }, { errors: "all" });
if (Option.isNone(bad)) {
console.log("Decode Result:", "None (invalid input)");
}

Use decodeUnknownOption when you only care whether decoding succeeded, not why it failed.

Sign in to save progress

Stay in the loop

Get notified when new chapters are added and when this course is complete.