Comments (4)
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.
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.
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.
Gotcha, thanks for the clear explanation!
from effect.
Related Issues (20)
- From Discord: Schema template literal issue after upgrading to @effect/[email protected]
- JSR computability HOT 1
- From Discord: Setting response cookies from RPC calls HOT 2
- From Discord: Clarification on `onExcessProperty: preserve` Behavior: Key Order Not Maintained
- `Cause` pretty printing is broken for multi-line error messages
- Strict pattern matching HOT 7
- From Discord: Request for `Chunk.lastNonEmpty` Method in Code Library
- From Discord: Ensure `Chunk.reverse` Preserves NonEmpty Status in Output
- `Struct.evolve` no compile time error for non function transformers HOT 2
- From Discord: Request to Add `Schema.NonEmptyChunk` HOT 1
- From Discord: Assigning Error Messages to Specific Inputs in Effect/Schema with Hook Resolvers HOT 1
- More precise `dual` signature (prevent type errors/issues) HOT 16
- From Discord: Detecting ParseError in `decode*Sync` for Error Retry Handling in `@tanstack/react-query`
- From Discord: (bug?) TypeError when using discard option with Effect.all HOT 1
- Implement `at` method for tuples
- Cli: --log-level flag HOT 3
- Requires DOM lib HOT 2
- Standard type extraction HOT 20
- From Discord: Pattern matching with typeguard - different array sizes
- Add "recursive" option to `FileSystem.watch`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from effect.