From 78550867ac89a627f80da4d7e94be65cbe0c908c Mon Sep 17 00:00:00 2001 From: sinclair Date: Tue, 7 Nov 2023 23:49:57 +0900 Subject: [PATCH] Update Inference for Required and Partial --- examples/index.ts | 25 ++++++++++++++----------- readme.md | 2 +- src/typebox.ts | 12 ++++++++++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/examples/index.ts b/examples/index.ts index 2934278c4..13bf41e77 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -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(schema: T, value: unknown): StaticDecode { - 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 +type B = Static +type C = Static \ No newline at end of file diff --git a/readme.md b/readme.md index 1bba93b13..0d5fde897 100644 --- a/readme.md +++ b/readme.md @@ -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 | diff --git a/src/typebox.ts b/src/typebox.ts index e105237f6..64bb30df1 100644 --- a/src/typebox.ts +++ b/src/typebox.ts @@ -608,7 +608,11 @@ export type TPartialObjectArray = AssertRest<{ [K in keyof export type TPartialRest = AssertRest<{ [K in keyof T]: TPartial> }> // prettier-ignore export type TPartialProperties = Evaluate + [K in keyof T]: + T[K] extends (TReadonlyOptional) ? TReadonlyOptional : + T[K] extends (TReadonly) ? TReadonlyOptional : + T[K] extends (TOptional) ? TOptional : + TOptional }>> // prettier-ignore export type TPartial = @@ -722,7 +726,11 @@ export type TReturnType = T['returns'] export type TRequiredRest = AssertRest<{ [K in keyof T]: TRequired> }> // prettier-ignore export type TRequiredProperties = Evaluate ? S : T[K] + [K in keyof T]: + T[K] extends (TReadonlyOptional) ? TReadonly : + T[K] extends (TReadonly) ? TReadonly : + T[K] extends (TOptional) ? S : + T[K] }>> // prettier-ignore export type TRequired =