Handling Schema.Class vs Struct in Effect Typescript Library

We've recently started leaning more on Schema.Class vs Struct and are slowly discovering some gotchas due to Schema class not being a drop in replacement for Struct. For example, when decoding into a Class it's ok to pass a POJO, however when decoding typeSchema(MyClass) the input is expected to already be instance of given class. Is there a better way to do this?

import { Schema } from 'effect'

export class MyClass extends Schema.Class<MyClass>('MyClass')({
  createdAt: Schema.Date,
}) {}

const rawData = { createdAt: new Date() }

// ParseError: Expected MyClass, actual {"createdAt":2025-02-03T11:14:58.181Z}
Schema.decodeUnknownSync(
  Schema.typeSchema(MyClass),
)(rawData)

// No error, but result is not instanceof MyClass
Schema.decodeUnknownSync(
  Schema.typeSchema(Schema.Struct({ ...MyClass.fields })),
)(rawData)

// No, error, and result is instanceof of MyClass
// but is there a better way to do this?
MyClass.make(
  Schema.decodeUnknownSync(
    Schema.typeSchema(Schema.Struct({ ...MyClass.fields })),
  )(rawData),
)
Was this page helpful?