Comments (2)
@ayazhafiz Hi,
Yeah, this is expected behavior (at least under the current design). Transforms work by using a 2-phase approach that will Map() and Check() in different orders depending on if you call Decode() or Encode() (and where Map() means running your Transform callback)
Decode() -> Check() -> Map()
Encode() -> Map() -> Check()
These phases are run after one another as distinct stages. TypeBox tries to avoid calls to Check() during the Map() phases and vice versa (so it's difficult to detect an invalid value during the Map() phase). Instead, it just trusts the user is submitting the correct value or it will throw either a TransformEncodeError
for Map() or TransformEncodeCheckError
for Check().
ExactOptionalPropertyTypes
So just on the following line...
const x: O = { a: undefined }; // this should be an error
Just keep in mind that { a: undefined }
is actually an invalid value despite the type being optional. The type + optional means "the "a" key can be omitted, but if specified, it must be Date". TypeScript is a bit loose by default when it comes to this, but does provide the exactOptionalPropertyTypes
configuration that will catch these kind of errors. Quick example at the link below with the option enabled.
import { Type, StaticDecode } from '@sinclair/typebox'
const TDate = Type.Transform(Type.String())
.Decode((value) => new Date(value))
.Encode((value) => value.toISOString());
const TO = Type.Object({ a: Type.Optional(TDate) })
type O = StaticDecode<typeof TO>;
const x: O = { a: undefined }; // caught with TS exactOptionalPropertyTypes: true
So, just mentioning this as it can have some consequences with respect to implementing Transforms (and looks to be the source of this issue). The best practice for writing Transforms is to try and write them defensively and to be a bit mindful about some of the nuance of end user TS configurations. For local codebases, you can switch on the exactOptionalPropertyTypes
to catch this kind of error early. If you decide to do this, TypeBox provides a configuration option to get things lining up at runtime.
https://github.com/sinclairzx81/typebox?tab=readme-ov-file#policies
import { TypeSystemPolicy } from '@sinclair/typebox/system'
// Disallow undefined values for optional properties (default is false)
//
// const A: { x?: number } = { x: undefined } - disallowed when enabled
TypeSystemPolicy.ExactOptionalPropertyTypes = true
Hope this helps!
S
from typebox.
@ayazhafiz Heya,
Might close off this issue as things are generally working as expected. If you have any follow up questions though, feel free to ping on this thread.
All the best!
S
from typebox.
Related Issues (20)
- Cannot reference a recursive type HOT 7
- Cannot use .map() inside Union with Literal HOT 2
- Nested `Type.Intersect` Errors HOT 1
- Incorrect inferred type for the empty object `Type.Object({})`. HOT 3
- `unevaluatedProperties` failed verification when combined with `Type.Intersect` and `Type.Union` HOT 2
- Types for both CJS and ESM cause ambiguity and prevent correct type resolution. HOT 8
- unique id for schemas generated with Type.Object() HOT 6
- Custom paths for custom validation errors HOT 2
- Composite Union type does not work HOT 3
- Type.Recursive does not work with Type.Transform for StaticDecode
- Issue regarding nested objects when using Value.Default HOT 5
- Enum type inferred as never when using Type.Mapped() HOT 2
- Should `IsValueType` guard consider `Date`? HOT 1
- ESNext target HOT 3
- Trying to do codegen from Drizzle schemas HOT 1
- Make `default` and `examples` schema options properties type-safe HOT 2
- Indicate which files don't have "sideEffects" in package.json for improved tree shaking HOT 3
- Type.String().Optional() is accepted on ts-hint / ts-lint, and even build successfully; but of course, "Optional()" does not exists HOT 2
- Dynamic Template Literals can't be Mapped HOT 2
- How should TypeBox types be used with TS type predicates? HOT 2
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.