Code Monkey home page Code Monkey logo

Comments (4)

gcanti avatar gcanti commented on August 23, 2024

The issue arises from the fact that const DocId = Doc.pipe(Schema.pick("id")); returns a transformation (namely a ComposeTransformation) that isn't supported by extend. However, I think we can resolve it by not returning a transformation in the first place if none of the picked keys are related to a property signature transformation (as in this case).

p.s.
I'm not sure if the repro is intentionally contrived to showcase the problem, but if it's not, for your information, you can achieve the same result like this:

const DocIdAndVersion = Schema.Struct({
  id: Doc.fields.id,
  version: Schema.Number
})

from effect.

giacomoran avatar giacomoran commented on August 23, 2024

Thanks for the explanation and the fix.

I operate under the mental model that Schema closely maps to TypeScript. The model breaks in this case because, in TypeScript, I can Pick<Doc, "id"> & { version: number }.

In the past, I've encountered a similar issue when applying Schema.partial to a struct containing optional properties. I understand this might be harder to support since both Schema.partial and Schema.optional deal with optional properties.

from effect.

gcanti avatar gcanti commented on August 23, 2024

Your mental model is correct and works well as long as we're talking about schemas representing a TypeScript type.

In TypeScript, I can use Pick<Doc, "id"> & { version: number }.

You actually can't, but the reason is because in TypeScript you can't represent Doc since it contains a transformation.

This definition

const Doc = Schema.Struct({
  id: Schema.String,
  desc: Schema.optional(Schema.String, { as: "Option" }),
});

is nothing more than a shortcut for this transformation:

const DocAsExplicitTransformation = Schema.transform(
  Schema.Struct({
    id: Schema.String,
    desc: Schema.optional(Schema.String)
  }),
  Schema.Struct({
    id: Schema.String,
    desc: Schema.OptionFromSelf(Schema.String)
  }),
  {
    decode: (input) => ({ id: input.id, desc: input.desc === undefined ? Option.none() : Option.some(input.desc) }),
    encode: (input) => Option.isSome(input.desc) ? { id: input.id, desc: input.desc.value } : { id: input.id }
  }
)

which cannot be represented in TypeScript's type system, and therefore you can't even use Pick on it.

You can consider Schema.pick as an expanded version of TypeScript's Pick. It functions effectively for schemas representing a TypeScript type and for certain types of transformations (though not all).

So optional properties themselves are not problematic, the issue arises when dealing with transformations. Schema.optional has some options (such as { as: "Option" }) that turn the corresponding field into a transformation.

from effect.

giacomoran avatar giacomoran commented on August 23, 2024

Gotcha, thanks for the clear explanation!

from effect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.