Comments (3)
@eygsoft Hi! Thanks for reporting!
Yes, this looks like a bug. At this stage, I don't think I'll be able to take a look into the issue until the weekend at the earliest (as I am currently tied up with some other work), however you may be able to workaround this issue if you can reshape the schematics into the following.
const A = Type.Object({
a: Type.String(),
b: Type.String()
}, { additionalProperties: false })
const B = Type.Object({
a: Type.String(),
c: Type.String()
}, { additionalProperties: false })
const T = Type.Union([A, B]) // T = { a: string, b: string } | { a: string, c: string }
This issue actually crosses over with some other areas of the library where I'm looking to implement a distributive evaluate similar to the following....
type T = (
{ x: number }
) & (
{ y: number } |
{ z: number }
)
// non-distributive: where passing y | z is observed as unevaluated properties
type A = {[K in keyof T]: T[K]} & {} // type A = { x: number }
// distributive: where y | z are distributed with outer property x
type E<T> = {[K in keyof T]: T[K]} & {}
type B = E<T> // type B = { x: number; y: number; } |
// { x: number; z: number; }
Note that TypeBox observes the validating schematic as A
, where it should be observed as B
. The validator should apply distributive union rules to the schematics (which has been challenging at a type level, but may be more tenable at a validation level)
Will keep you posted on any changes here.
Cheers
S
from typebox.
@eygsoft Hi! Thanks for reporting!
Yes, this looks like a bug. At this stage, I don't think I'll be able to take a look into the issue until the weekend at the earliest (as I am currently tied up with some other work), however you may be able to workaround this issue if you can reshape the schematics into the following.
const A = Type.Object({ a: Type.String(), b: Type.String() }, { additionalProperties: false }) const B = Type.Object({ a: Type.String(), c: Type.String() }, { additionalProperties: false }) const T = Type.Union([A, B]) // T = { a: string, b: string } | { a: string, c: string }This issue actually crosses over with some other areas of the library where I'm looking to implement a distributive evaluate similar to the following....
type T = ( { x: number } ) & ( { y: number } | { z: number } ) // non-distributive: where passing y | z is observed as unevaluated properties type A = {[K in keyof T]: T[K]} & {} // type A = { x: number } // distributive: where y | z are distributed with outer property x type E<T> = {[K in keyof T]: T[K]} & {} type B = E<T> // type B = { x: number; y: number; } | // { x: number; z: number; }Note that TypeBox observes the validating schematic as
A
, where it should be observed asB
. The validator should apply distributive union rules to the schematics (which has been challenging at a type level, but may be more tenable at a validation level)Will keep you posted on any changes here. Cheers S
Thank you. The method I used is exactly the solution you mentioned, and it works very well. I just discovered this issue and I hope this great project can be more perfect. Thank you again!
from typebox.
@eygsoft Hiya,
Hey, going to close this one out as the workaround sounded like it solved the issue. A lot of this work fits under the umbrella of evaluated types which is due before the end of the year (hopefully). This should provide better options for distributive union types inline with the types mentioned on this issue. Will be good when it lands :)
Will close up for now.
All the best!
S
from typebox.
Related Issues (20)
- Transform type keys in Record HOT 2
- Is it possible to add support to the Type.String additional properties for the enum keyword HOT 2
- Support for async validations? HOT 1
- Transform + Map/Index does not work HOT 4
- Update the error message for required properties HOT 3
- Covariance issue with `TypeCheck<TSchema>` and `TypeCheck<TObject<{}>>` HOT 1
- Default values in nested objects are not generated HOT 2
- Doubt on Record HOT 2
- TypeCompiler compile on Type.Strict(T) - Preflight validation check failed to guard for the given schema HOT 2
- Max call stack size exceeded HOT 1
- Value.Convert() doesn't work with nullable record HOT 1
- Variadic Any Typed function parameters HOT 2
- How to do a cross field validation? HOT 3
- Undefined is incorrectly handled in Diff HOT 3
- QoL helper: `TypeCheck.Ensure` HOT 3
- Usage of `Type.Composite` with `Type.TemplateLiteral` HOT 3
- `Check` doesn't validate strings with enums HOT 4
- `Type.Omit` mixed with `Type.Union` is not preserving the `additionalProperties` attribute HOT 2
- Advice to compose types HOT 3
- Access to schema properties for non TObject schemas HOT 5
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 typebox.