Effect CommunityEC
Effect Community•3y ago•
35 replies
Gibbs

Creating a Schema with Explicit Undefined in Type Signature

Hi! I'm struggling with creating a Schema of the following type signature:
type MySchema = Schema.Schema<
  { a?: string | undefined; b?: string | undefined },
  { a?: string; b: Option<string> }
>;

=> I want to be able to explicitly pass undefined to the decode function of this schema.

I tried this
const MySchema = Schema.struct({
  a: Schema.optional(Schema.string),
  b: Schema.optional(Schema.string).toOption(),
});

but it creates a Schema of shape
Schema.Schema<{
    readonly a?: string;
    readonly b?: string;
}, {
  readonly a?: string;
  readonly b: Option<string>;
}>

and then I cannot explicitly pass undefined to the decode method:
const decoded = decode({ a: undefined });
//                      ^? Type 'undefined' is not assignable to type 'string'.


I also tried:
const MySchema = Schema.struct({
  a: Schema.optional(Schema.union(Schema.string, Schema.undefined)),
  b: Schema.optional(Schema.union(Schema.string, Schema.undefined)).toOption(),
});

It works well for the property
a
but for b I will get an Option<string | undefined> as output, instead of Option<string>.

Does anyone know how to build such Schema ? Thanks!
Was this page helpful?