Effect CommunityEC
Effect Community17mo ago
3 replies
tyko

Applying Default Values for Record with String Union Keys in TypeScript

Is there a way to apply defaults for a Record with a string union as the key?

const LiteralSchema = Schema.Literal("prop1", "prop2", "prop2");

// works, but requires manual enumeration of literals
const StructSchema = Schema.Struct({
  prop1: Schema.optionalWith(Schema.Boolean, { default: () => false }),
  prop2: Schema.optionalWith(Schema.Boolean, { default: () => false }),
  prop3: Schema.optionalWith(Schema.Boolean, { default: () => false }),
});

// typescript error
const RecordSchema = Schema.Record({
  key: LiteralSchema,
  value: Schema.optionalWith(Schema.Boolean, { default: () => false }),
});


I'm basically looking for this result:

const encoded = {
  prop2: true,
};

const decoded = {
  prop1: false,
  prop2: true,
  prop3: false,
};


(version ^0.70.4)
Was this page helpful?