Skip to content

Commit

Permalink
Update Inference for Required and Partial
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 7, 2023
1 parent 4dadbe8 commit 7855086
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
25 changes: 14 additions & 11 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValueGuard } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema, KeyResolver, TObject, TypeRegistry, StaticDecode } from '@sinclair/typebox'
import { Benchmark } from './benchmark'

// Todo: Investigate Union Default (initialize interior types)
// Todo: Implement Value.Clean() Tests - Done

export function Parse<T extends TSchema>(schema: T, value: unknown): StaticDecode<T> {
const converted = Value.Convert(schema, value)
const defaulted = Value.Default(schema, converted)
const decoded = Value.Decode(schema, defaulted)
return Value.Clean(schema, decoded)
}
import { Type, Static } from '@sinclair/typebox';

export const A = Type.Object({
a: Type.ReadonlyOptional(Type.Number()),
b: Type.Readonly(Type.Number()),
c: Type.Optional(Type.Number()),
d: Type.Number()
})
const B = Type.Required(A)
const C = Type.Partial(A)

type A = Static<typeof A>
type B = Static<typeof B>
type C = Static<typeof C>
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1663,10 +1663,10 @@ The following is a list of community packages that offer general tooling, extend
| [feathersjs](https://github.com/feathersjs/feathers) | The API and real-time application framework |
| [fetch-typebox](https://github.com/erfanium/fetch-typebox) | Drop-in replacement for fetch that brings easy integration with TypeBox |
| [h3-typebox](https://github.com/kevinmarrec/h3-typebox) | Schema validation utilities for h3 using TypeBox & Ajv |
| [http-wizard](https://github.com/flodlc/http-wizard) | Type safe http client library for Fastify |
| [openapi-box](https://github.com/geut/openapi-box) | Generate TypeBox types from OpenApi IDL + Http client library |
| [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas |
| [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
| [http-wizard](https://github.com/flodlc/http-wizard) | Type safe http client library for Fastify |
| [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
| [typebox-validators](https://github.com/jtlapp/typebox-validators) | Advanced validators supporting discriminated and heterogeneous unions |
Expand Down
12 changes: 10 additions & 2 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,11 @@ export type TPartialObjectArray<T extends TObject[]> = AssertRest<{ [K in keyof
export type TPartialRest<T extends TSchema[]> = AssertRest<{ [K in keyof T]: TPartial<AssertType<T[K]>> }>
// prettier-ignore
export type TPartialProperties<T extends TProperties> = Evaluate<AssertProperties<{
[K in keyof T]: TOptional<T[K]>
[K in keyof T]:
T[K] extends (TReadonlyOptional<infer S>) ? TReadonlyOptional<S> :
T[K] extends (TReadonly<infer S>) ? TReadonlyOptional<S> :
T[K] extends (TOptional<infer S>) ? TOptional<S> :
TOptional<T[K]>
}>>
// prettier-ignore
export type TPartial<T extends TSchema> =
Expand Down Expand Up @@ -722,7 +726,11 @@ export type TReturnType<T extends TFunction> = T['returns']
export type TRequiredRest<T extends TSchema[]> = AssertRest<{ [K in keyof T]: TRequired<AssertType<T[K]>> }>
// prettier-ignore
export type TRequiredProperties<T extends TProperties> = Evaluate<AssertProperties<{
[K in keyof T]: T[K] extends TOptional<infer S> ? S : T[K]
[K in keyof T]:
T[K] extends (TReadonlyOptional<infer S>) ? TReadonly<S> :
T[K] extends (TReadonly<infer S>) ? TReadonly<S> :
T[K] extends (TOptional<infer S>) ? S :
T[K]
}>>
// prettier-ignore
export type TRequired<T extends TSchema> =
Expand Down

0 comments on commit 7855086

Please sign in to comment.