GithubHelp home page GithubHelp logo

Type.String().Optional() is accepted on ts-hint / ts-lint, and even build successfully; but of course, "Optional()" does not exists about typebox HOT 2 CLOSED

kevinfaveri avatar kevinfaveri commented on July 2, 2024
Type.String().Optional() is accepted on ts-hint / ts-lint, and even build successfully; but of course, "Optional()" does not exists

from typebox.

Comments (2)

sinclairzx81 avatar sinclairzx81 commented on July 2, 2024

@kevinfaveri Hi,


Is this by design?

Yeah, this is a consequence of the design. The reason this is possible is because TypeBox types extend the following open interface (which permits any additional extra properties). TypeBox gives hints for the known additional properties (as given by the SchemaOptions interface), but for anything else, it just trusts you.

export interface SchemaOptions {
  $schema?: string
  $id?: string
  title?: string
  description?: string
  default?: any
  examples?: any
  readOnly?: boolean
  writeOnly?: boolean
  [prop: string]: any // meaning T.Optional and by extension T.Optional() (call expression) is allowed.
}

Note: The reason TypeBox uses any vs unknown is largely historical. The additional properties of any are mostly used for property hooks in downstream tooling, so changing to unknown has a potential to cause a lot of breakage (so reluctant to change in the near term). The ability to .chain() function calls off the end of schemas is very unfortunate though (and may look to revise things here in future)


Is there any way to enforce those wildcards / any deep field are not acceptable at build time?

Unfortunately no. Because the SchemaOptions is fully open to extension, it's very difficult to constrain without additional wrapping of the types. The appropriate solution here would be to have SchemaOptions constrain to unknown. This would at least warn of attempted member expression / call expression on the types. But as this can't be done for other reasons, things are largely at a impasse (at least for now)


Will close off this issue for now, but hopefully the above provides some insights into why this is possible. TypeBox is getting some fairly significant internal updates done to it's inference infrastructure for the next release (scheduled sometime near the end of this year). I will mark this issue as an aspect for review ... but for now, things are working as per design.

All this best
S

from typebox.

kevinfaveri avatar kevinfaveri commented on July 2, 2024

Thanks sir!

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.