Giter VIP home page Giter VIP logo

Comments (4)

sinclairzx81 avatar sinclairzx81 commented on July 18, 2024

@dswbx Hi,

Unfortunately, the [Kind] symbol is a requirement to use the TypeBox Check and Compiler functions (as this property is used both for internal runtime optimizations and type composition checks). In saying this, you can still use raw schematics with TypeBox, but you will need to map them into TypeBox types first (as you mentioned)


I am tempted to believe that I could "simply" write a recursive function that adds the [Kind] prop based on type to make this work. Would this work? Or is there a better way to do it?

So, there's actually been some work done recently to provide better support for raw schematics with a prototype FromSchema function prepared at the link below.

https://github.com/sinclairzx81/typebox/blob/master/example/prototypes/from-schema.ts

FromSchema

The FromSchema function will attempt to map raw schematics into TypeBox types if an appropriate transformation exists. This function has a computed return type of TSchema that can be used for type inference and additional type composition. Note that any unrecognized raw schematics will result in TUnknown.

Reference TypeScript Link Here

import { FromSchema } from './prototypes/from-schema' // copy to your project
import { Type, type Static } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

const T = FromSchema({               // const T: TObject<{
  type: 'object',                    //   foo: TString,
  properties: {                      //   bar: TString,
     foo: { type: 'string' },        //   baz: TString
     bar: { type: 'string' },        // }>
     baz: { type: 'string' },
  },
  required: ['foo', 'bar', 'baz'],
} as const)

const P = Type.Partial(T)            // const P: TObject<{
                                     //  foo: TOptional<TString>,
                                     //  bar: TOptional<TString>,
                                     //  baz: TOptional<TString>,
                                     // }>

console.log(Value.Check(T, { foo: 'hello', bar: 'world' })) // true

So FromSchema is currently the recommended implementation provided by TypeBox, but isn't ready for inclusion in the library yet (but will likely be included when TB drops support for TS 4.0). For now, you can copy and paste the from-schema.ts module into your project and modify the code based on your requirements.

Hope this helps!
S

from typebox.

dswbx avatar dswbx commented on July 18, 2024

@sinclairzx81 thank you for your response, this works perfectly fine – exactly what I was looking for!

Out of curiosity, do you have a timeframe in mind for when you'll drop support for TS 4.0?

from typebox.

sinclairzx81 avatar sinclairzx81 commented on July 18, 2024

@dswbx Hey, good to hear :)

Out of curiosity, do you have a timeframe in mind for when you'll drop support for TS 4.0?

Nothing formal as of yet. TS 4.9.5 is still pulling around 20% of all TS downloads (currently 9,765,927 weekly) so will want to see that download figure go down a bit more before dropping support in TB.

Note when 4.x support is dropped, I don't anticipate any major changes in TypeBox when that happens. I'm mostly looking to transition over to Const Type Parameters where appropriate which should be a non-breaking change for anyone on TS 5.x. Unfortunately I can use it today without potentially breaking 4.x users :(

With this said, you can use this feature on FromSchema if you update the signature to.

export function FromSchema<const T>(T: T): TFromSchema<T>
//                         ^ add this

As a rough estimate on when 4.x will be dropped tho, probably sometime nearer the end of this year (as a guess) and after I've had some time to upgrade some of my build tooling (which is currently on older versions of esbuild that also doesn't support const generics).

Hope this brings some insight! Will close of this issue for now.
Cheers
S

from typebox.

dswbx avatar dswbx commented on July 18, 2024

Ah, I see, makes total sense. Also older 4.x versions seem to have many downloads still.. Let's hope that those numbers drop.

Thanks again for your assistance, explanation and creating this awesome library 🎉

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.