Effect CommunityEC
Effect Community3y ago
35 replies
imagio

Creating a Transformation for Reading Annotations in TypeScript

How would I go about creating a transformation that reads annotations to determine its behavior? For example if I create an observable annotation I want to transform a struct using members that have that annotation.

const ObservableAnnotationId = "schema/observable"
const observableProp = <A>(self: S.Schema<A>): S.Schema<A> =>
    S.make(AST.setAnnotation(self.ast, ObservableAnnotationId, true))

const myStruct = S.struct({
    foo: S.string.pipe(observableProp),
    bar: S.number
})


I'd like to create a transform that collects all members of the struct having my annotation.

import {makeObservable, toJS, observable} from "mobx"

export const observableStruct = <T extends object>(
    schema: S.Schema<T>,
) =>
    S.transform(
        schema,
        schema,
        // how do I build the arguments to makeObservable based on 
        // which properties have the observableProp annotation?
        v => makeObservable(v, { foo: observable }),
        v => toJS(v),
    )
Was this page helpful?