Giter VIP home page Giter VIP logo

Comments (2)

sinclairzx81 avatar sinclairzx81 commented on July 1, 2024

@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.

TypeScript Link Here

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.

sinclairzx81 avatar sinclairzx81 commented on July 1, 2024

@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)

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.