Effect CommunityEC
Effect Community17mo ago
5 replies
kiliancs

Handling Custom Annotations in Schema Structs with Extend and Union

Another question. I'm trying to do what I'd call pretty advanced stuff with Schema without fully understanding it.

I have some custom annotations in some fields in some of my schema structs. I want to traverse the schema (across extend, union and struct). FWIW, I don't need to handle nested structs. It is a flat object, that may be built with extend and union.

There may be a way to do this in an idiomatic way, but I ended up creating a function that walks the AST pretty manually, and that seems to work.

However, at the type level I haven't been successful. I can handle a struct and unions, but I fail to handle the case of extend.

type ExcludeAnnotatedInner<S extends Schema.Schema.Any, AnnotationId extends SootsTypeId> =
  S extends Schema.Struct<infer Fields>
    ? {
        [K in keyof Fields as Fields[K] extends HasAnnotation<AnnotationId> ? never : K]: Schema.Schema.Type<
          Fields[K]
        >;
      }
    : S extends Schema.Union<infer Members>
      ? {
          [K in keyof Members]: ExcludeAnnotatedInner<Members[K], AnnotationId>;
        }[number]
      : Schema.Schema.Type<S>;
Was this page helpful?