Comments (4)
@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.
@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.
@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.
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)
- Reduce unnecessary cloning in `Value.Convert` HOT 2
- [typebox-codegen] Option to emit types wrapped in arrow function? HOT 1
- Type.Mapped does not handle optional properties the same as Typescript HOT 2
- Add changelog or create a new release for each tag HOT 3
- TypeCheck.Decode return type inferred incorrectly? HOT 12
- Feature request: functions as defaults HOT 3
- Composite doesn't check additionalProperties HOT 2
- Custom TypeRegistry and TypeFormat HOT 1
- [feat] describe a object with pattern key, and literal key at the same time HOT 1
- Easier/general way to implement Typescript's "key remapping via `as`" approach HOT 1
- Overriding Schema Title HOT 1
- Cannot find module "./assert/index.mjs" HOT 1
- Encode is called twice on Type.Transform when inner type is Type.Intersection HOT 1
- StaticDecode cannot handle union of literals, while Static can HOT 1
- How to validate raw json schema HOT 2
- Why is Nullable not a first class Type? HOT 4
- Value.Convert does not seem to convert sometimes HOT 3
- `Value.Default` corrupts data in unions
- Is there a way to coerce when creating a schema? HOT 2
- Avoid losing precision when converting to bigints HOT 6
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.